birdsql/bird_sql_dev_20251106 huggingface dataset:
Database: financial
Question: For client 3541, what are the complete financial details of all their accounts including region, loan information, transaction statistics, net balance, and how many other clients are in the same region, ordered by account activity?
Evidence: PRIJEM refers to incoming transactions; VYDAJ refers to outgoing transactions; net balance = total income - total expense; account activity is measured by transaction count
SQL:
WITH ClientRegionInfo AS (
    SELECT 
        c.client_id,
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        d.A11 AS avg_salary,
        d.A12 AS unemployment_rate_1995
    FROM 
        client AS c
    INNER JOIN 
        district AS d ON c.district_id = d.district_id
    WHERE 
        c.client_id = 3541
),
ClientAccounts AS (
    SELECT 
        cri.client_id,
        cri.region,
        cri.district_name,
        a.account_id,
        a.frequency,
        a.date AS account_opening_date,
        CASE 
            WHEN l.loan_id IS NOT NULL THEN 'Yes'
            ELSE 'No'
        END AS has_loan,
        COALESCE(l.amount, 0) AS loan_amount,
        COALESCE(l.status, 'N/A') AS loan_status
    FROM 
        ClientRegionInfo cri
    INNER JOIN 
        disp AS d ON cri.client_id = d.client_id
    INNER JOIN 
        account AS a ON d.account_id = a.account_id
    LEFT JOIN 
        loan AS l ON a.account_id = l.account_id
),
TransactionStats AS (
    SELECT 
        ca.client_id,
        ca.region,
        ca.district_name,
        ca.account_id,
        COUNT(t.trans_id) AS transaction_count,
        SUM(CASE WHEN t.type = 'PRIJEM' THEN t.amount ELSE 0 END) AS total_income,
        SUM(CASE WHEN t.type = 'VYDAJ' THEN t.amount ELSE 0 END) AS total_expense,
        MAX(t.date) AS last_transaction_date,
        AVG(t.amount) AS avg_transaction_amount,
        ROW_NUMBER() OVER (PARTITION BY ca.client_id ORDER BY COUNT(t.trans_id) DESC) AS account_rank_by_activity
    FROM 
        ClientAccounts ca
    LEFT JOIN 
        trans AS t ON ca.account_id = t.account_id
    GROUP BY 
        ca.client_id, ca.region, ca.district_name, ca.account_id
)
SELECT 
    ts.region,
    ts.district_name,
    ts.account_id,
    ca.account_opening_date,
    ca.has_loan,
    ca.loan_amount,
    ca.loan_status,
    ts.transaction_count,
    ts.total_income,
    ts.total_expense,
    ts.total_income - ts.total_expense AS net_balance,
    ts.last_transaction_date,
    ts.avg_transaction_amount,
    (SELECT COUNT(DISTINCT c2.client_id) 
     FROM client c2 
     INNER JOIN district d2 ON c2.district_id = d2.district_id 
     WHERE d2.A3 = ts.region) AS clients_in_same_region
FROM 
    TransactionStats ts
INNER JOIN 
    ClientAccounts ca ON ts.account_id = ca.account_id
WHERE 
    ts.client_id = 3541
ORDER BY 
    ts.account_rank_by_activity;
Database: financial
Question: Which district has the most accounts with loan contracts finished with no problems?
Evidence: status = 'A' refers to loan contracts finished with no problems
SQL:
SELECT T1.A2
FROM District AS T1
INNER JOIN Account AS T2 ON T1.District_id = T2.District_id
INNER JOIN Loan AS T3 ON T2.Account_id = T3.Account_id
WHERE T3.status = 'A'
GROUP BY T1.District_id
ORDER BY COUNT(T2.Account_id) DESC
LIMIT 1;
Database: financial
Question: For order 32423, provide a comprehensive profile of the account owner including their demographics, order details, loan status, transaction history, and number of credit cards.
Evidence: Account owner refers to disp.type = 'OWNER'; transaction history includes total income (type = 'PRIJEM'), total expense (type = 'VYDAJ'), and net balance.
SQL:
WITH client_info AS (
  SELECT 
    c.client_id,
    c.gender,
    CAST(strftime('%Y', 'now') - strftime('%Y', c.birth_date) AS INTEGER) AS age,
    d.A2 AS district_name,
    d.A3 AS region
  FROM client c
  JOIN district d ON c.district_id = d.district_id
),
order_details AS (
  SELECT 
    o.order_id,
    o.account_id,
    o.amount,
    o.k_symbol,
    a.district_id,
    a.frequency,
    RANK() OVER (PARTITION BY o.account_id ORDER BY o.amount DESC) AS amount_rank
  FROM `order` o
  JOIN account a ON o.account_id = a.account_id
  WHERE o.order_id = 32423
),
account_transactions AS (
  SELECT 
    t.account_id,
    COUNT(t.trans_id) AS transaction_count,
    SUM(CASE WHEN t.type = 'PRIJEM' THEN t.amount ELSE 0 END) AS total_income,
    SUM(CASE WHEN t.type = 'VYDAJ' THEN t.amount ELSE 0 END) AS total_expense,
    MAX(t.date) AS last_transaction_date
  FROM trans t
  JOIN order_details od ON t.account_id = od.account_id
  GROUP BY t.account_id
)

SELECT 
  ci.client_id,
  ci.gender,
  ci.age,
  ci.district_name,
  ci.region,
  od.amount AS order_amount,
  od.k_symbol AS order_type,
  CASE 
    WHEN l.loan_id IS NOT NULL THEN 'Yes'
    ELSE 'No'
  END AS has_loan,
  COALESCE(l.amount, 0) AS loan_amount,
  COALESCE(l.status, 'N/A') AS loan_status,
  at.transaction_count,
  at.total_income,
  at.total_expense,
  at.total_income - at.total_expense AS net_balance,
  at.last_transaction_date,
  (SELECT COUNT(card_id) FROM card c JOIN disp d ON c.disp_id = d.disp_id WHERE d.client_id = ci.client_id) AS card_count
FROM order_details od
JOIN disp d ON od.account_id = d.account_id
JOIN client_info ci ON d.client_id = ci.client_id
JOIN account_transactions at ON od.account_id = at.account_id
LEFT JOIN loan l ON od.account_id = l.account_id
WHERE d.type = 'OWNER'
ORDER BY ci.client_id;
Database: financial
Question: Please list all the transactions made by accounts from district 5.
SQL:
SELECT T3.trans_id FROM district AS T1 INNER JOIN account AS T2 ON T1.district_id = T2.district_id INNER JOIN trans AS T3 ON T2.account_id = T3.account_id WHERE T1.district_id = 5
Database: financial
Question: What is the comprehensive banking activity profile for accounts in Jesenik district, including the total number of accounts, transaction patterns, loan statistics with their repayment status, average balances, and the gender distribution of clients?
Evidence: Good loans refer to status = 'A'; bad loans refer to status = 'B'. PRIJEM represents incoming transactions and VYDAJ represents outgoing transactions.
SQL:
WITH district_accounts AS (
  SELECT 
    d.district_id,
    d.A2 AS district_name,
    a.account_id,
    a.date AS account_creation_date,
    COUNT(l.loan_id) AS loan_count,
    SUM(CASE WHEN l.status = 'A' THEN 1 ELSE 0 END) AS good_loans,
    SUM(CASE WHEN l.status = 'B' THEN 1 ELSE 0 END) AS bad_loans,
    AVG(l.amount) AS avg_loan_amount
  FROM district d
  JOIN account a ON d.district_id = a.district_id
  LEFT JOIN loan l ON a.account_id = l.account_id
  WHERE d.A2 = 'Jesenik'
  GROUP BY d.district_id, d.A2, a.account_id, a.date
),
transaction_stats AS (
  SELECT
    da.account_id,
    COUNT(t.trans_id) AS transaction_count,
    SUM(CASE WHEN t.type = 'PRIJEM' THEN t.amount ELSE 0 END) AS total_income,
    SUM(CASE WHEN t.type = 'VYDAJ' THEN t.amount ELSE 0 END) AS total_expense,
    MAX(t.balance) AS max_balance
  FROM district_accounts da
  LEFT JOIN trans t ON da.account_id = t.account_id
  GROUP BY da.account_id
)
SELECT 
  COUNT(da.account_id) AS total_accounts,
  SUM(CASE WHEN ts.transaction_count > 5 THEN 1 ELSE 0 END) AS accounts_with_many_transactions,
  AVG(ts.transaction_count) AS avg_transactions_per_account,
  SUM(da.loan_count) AS total_loans,
  ROUND(AVG(da.avg_loan_amount), 2) AS average_loan_amount,
  SUM(da.good_loans) AS total_good_loans,
  SUM(da.bad_loans) AS total_bad_loans,
  ROUND(AVG(ts.total_income), 2) AS avg_income_per_account,
  ROUND(AVG(ts.max_balance), 2) AS avg_max_balance,
  (SELECT COUNT(DISTINCT c.client_id) 
   FROM client c 
   JOIN disp d ON c.client_id = d.client_id
   JOIN district_accounts da ON d.account_id = da.account_id
   WHERE c.gender = 'F') AS female_clients,
  (SELECT COUNT(DISTINCT c.client_id) 
   FROM client c 
   JOIN disp d ON c.client_id = d.client_id
   JOIN district_accounts da ON d.account_id = da.account_id
   WHERE c.gender = 'M') AS male_clients
FROM district_accounts da
LEFT JOIN transaction_stats ts ON da.account_id = ts.account_id;
Database: financial
Question: List all the clients' IDs whose junior credit cards were issued after 1996.
Evidence: After 1996 means date > = '1997-01-01
SQL:
SELECT T2.client_id FROM card AS T1 INNER JOIN disp AS T2 ON T1.disp_id = T2.disp_id WHERE T1.type = 'junior' AND T1.issued >= '1997-01-01'
Database: financial
Question: What percentage of clients who opened their accounts in the district with an average salary of over 10000 are women?
Evidence: Female refers to gender = 'F'; Woman and female are closed; Average salary can be found in A11
SQL:
SELECT CAST(SUM(T2.gender = 'F') AS REAL) * 100 / COUNT(T2.client_id)
FROM district AS T1
INNER JOIN client AS T2 ON T1.district_id = T2.district_id
WHERE T1.A11 > 10000;
Database: financial
Question: What was the growth rate of the total amount of loans across all accounts for a male client between 1996 and 1997?
Evidence: Growth rate = (sum of amount_1997 - sum of amount_1996) / (sum of amount_1996) * 100%; Male refers to gender = 'M'
SQL:
SELECT CAST((SUM(CASE WHEN STRFTIME('%Y', T1.date) = '1997' THEN T1.amount ELSE 0 END) -
             SUM(CASE WHEN STRFTIME('%Y', T1.date) = '1996' THEN T1.amount ELSE 0 END)) AS REAL) * 100 /
       SUM(CASE WHEN STRFTIME('%Y', T1.date) = '1996' THEN T1.amount ELSE 0 END)
FROM loan AS T1
INNER JOIN account AS T2 ON T1.account_id = T2.account_id
INNER JOIN disp AS T3 ON T3.account_id = T2.account_id
INNER JOIN client AS T4 ON T4.client_id = T3.client_id
WHERE T4.gender = 'M' AND T3.type = 'OWNER';
Database: financial
Question: How many credit card withdrawals were recorded after 1995?
Evidence: Operation = 'VYBER KARTOU' means credit card withdrawals
SQL:
SELECT COUNT(account_id) FROM trans WHERE STRFTIME('%Y', date) > '1995' AND operation = 'VYBER KARTOU'
Database: financial
Question: What was the difference in the number of crimes committed in East and North Bohemia in 1996?
Evidence: Difference in no. of committed crimes between 2 regions = Total no. of committed crimes in 1996 in north Bohemia - Total no. of committed crimes in 1996 in e ast Bohemia. A3 refers to region. Data about no. of committed crimes 1996 appears in A16
SQL:
SELECT SUM(IIF(A3 = 'north Bohemia', A16, 0)) - SUM(IIF(A3 = 'east Bohemia', A16, 0)) FROM district
Database: financial
Question: How many owner and disponent dispositions are there from account number 1 to account number 10?
SQL:
SELECT SUM(type = 'OWNER') , SUM(type = 'DISPONENT') FROM disp WHERE account_id BETWEEN 1 AND 10
Database: financial
Question: How often does account number 3 request an account statement to be released? What was the aim of debiting 3539 in total?
Evidence: k_symbol refers to the purpose of payments
SQL:
SELECT T1.frequency, T2.k_symbol FROM account AS T1 INNER JOIN (SELECT account_id, k_symbol, SUM(amount) AS total_amount FROM `order` GROUP BY account_id, k_symbol) AS T2 ON T1.account_id = T2.account_id WHERE T1.account_id = 3 AND T2.total_amount = 3539
Database: financial
Question: What year was account owner number 130 born?
SQL:
SELECT STRFTIME('%Y', T1.birth_date) FROM client AS T1 INNER JOIN disp AS T3 ON T1.client_id = T3.client_id INNER JOIN account AS T2 ON T3.account_id = T2.account_id WHERE T2.account_id = 130
Database: financial
Question: For accounts with owner disposition that request statements after each transaction, what are the regional and district-level statistics including the number of accounts, average district salary, loan adoption rate, average loan amount, transaction activity, and account balances?
Evidence: Statements after transaction refers to frequency = 'POPLATEK PO OBRATU'; owner disposition refers to type = 'OWNER' in disp table
SQL:
WITH owner_accounts AS (
    SELECT DISTINCT a.account_id, a.district_id, a.frequency, a.date
    FROM account a
    JOIN disp d ON a.account_id = d.account_id
    WHERE d.type = 'OWNER' AND a.frequency = 'POPLATEK PO OBRATU'
),
district_stats AS (
    SELECT 
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        CAST(d.A4 AS INTEGER) AS population,
        CAST(d.A11 AS INTEGER) AS avg_salary,
        COUNT(DISTINCT oa.account_id) AS statement_accounts_count,
        AVG(CAST(STRFTIME('%Y', 'now') - STRFTIME('%Y', oa.date) AS INTEGER)) AS avg_account_age
    FROM district d
    LEFT JOIN owner_accounts oa ON d.district_id = oa.district_id
    GROUP BY d.district_id, d.A2, d.A3
),
loan_stats AS (
    SELECT 
        oa.account_id,
        COUNT(l.loan_id) AS loan_count,
        COALESCE(SUM(l.amount), 0) AS total_loan_amount,
        CASE 
            WHEN MAX(l.status) = 'A' THEN 'Running'
            WHEN MAX(l.status) = 'B' THEN 'Finished'
            WHEN MAX(l.status) = 'C' THEN 'Consolidated'
            WHEN MAX(l.status) = 'D' THEN 'Problem'
            ELSE 'No Loan'
        END AS loan_status
    FROM owner_accounts oa
    LEFT JOIN loan l ON oa.account_id = l.account_id
    GROUP BY oa.account_id
),
transaction_stats AS (
    SELECT 
        oa.account_id,
        COUNT(t.trans_id) AS transaction_count,
        COALESCE(SUM(CASE WHEN t.type = 'PRIJEM' THEN t.amount ELSE 0 END), 0) AS total_credits,
        COALESCE(SUM(CASE WHEN t.type = 'VYDAJ' THEN t.amount ELSE 0 END), 0) AS total_debits,
        COALESCE(MAX(t.balance), 0) AS last_balance
    FROM owner_accounts oa
    LEFT JOIN trans t ON oa.account_id = t.account_id
    GROUP BY oa.account_id
)
SELECT 
    COUNT(oa.account_id) AS total_accounts,
    ds.region,
    ds.district_name,
    ROUND(AVG(ds.avg_salary), 2) AS average_district_salary,
    COUNT(DISTINCT CASE WHEN ls.loan_count > 0 THEN oa.account_id END) AS accounts_with_loans,
    ROUND(AVG(ls.total_loan_amount), 2) AS avg_loan_amount,
    ROUND(AVG(ts.transaction_count), 2) AS avg_transaction_count,
    ROUND(AVG(ts.total_credits - ts.total_debits), 2) AS avg_net_flow,
    ROUND(AVG(ts.last_balance), 2) AS avg_balance
FROM owner_accounts oa
JOIN district_stats ds ON oa.district_id = ds.district_id
JOIN loan_stats ls ON oa.account_id = ls.account_id
JOIN transaction_stats ts ON oa.account_id = ts.account_id
GROUP BY ds.region, ds.district_name
HAVING COUNT(oa.account_id) > 0
ORDER BY total_accounts DESC, avg_loan_amount DESC
Database: financial
Question: What is the amount of debt that client number 992 has, and how is this client doing with payments?
SQL:
SELECT T4.amount, T4.status FROM client AS T1 INNER JOIN disp AS T2 ON T1.client_id = T2.client_id INNER JOIN account AS T3 on T2.account_id = T3.account_id INNER JOIN loan AS T4 ON T3.account_id = T4.account_id WHERE T1.client_id = 992
Database: financial
Question: What is the sum that client number 4's account has following transaction 851? Who owns this account, a man or a woman?
SQL:
SELECT T4.balance, T1.gender FROM client AS T1 INNER JOIN disp AS T2 ON T1.client_id = T2.client_id INNER JOIN account AS T3 ON T2.account_id =T3.account_id INNER JOIN trans AS T4 ON T3.account_id = T4.account_id WHERE T1.client_id = 4 AND T4.trans_id = 851
Database: financial
Question: What are the complete details of client number 9 including their gender, birth date, credit card type, when the card was issued, customer status, total number of accounts owned, and how long they've had their most recent card?
Evidence: Customer status is determined by card type: gold card holders are Premium Customers and classic card holders are Regular Customers.
SQL:
SELECT 
    cl.client_id,
    cl.gender,
    DATE(cl.birth_date) AS birth_date,
    cd.type AS card_type,
    DATE(cd.issued) AS card_issued_date,
    CASE 
        WHEN cd.type = 'gold' THEN 'Premium Customer'
        WHEN cd.type = 'classic' THEN 'Regular Customer'
        ELSE 'Other Customer Type'
    END AS customer_status,
    (SELECT COUNT(*) FROM disp WHERE client_id = 9) AS total_accounts_owned,
    (julianday('now') - julianday(cd.issued))/365.25 AS card_age_years
FROM client AS cl
INNER JOIN disp AS d ON cl.client_id = d.client_id
INNER JOIN card AS cd ON d.disp_id = cd.disp_id
WHERE cl.client_id = 9
ORDER BY cd.issued DESC
LIMIT 1;
Database: financial
Question: How much, in total, did client number 617 pay for all of the transactions in 1998?
SQL:
SELECT SUM(T2.amount) FROM disp AS T1 INNER JOIN trans AS T2 ON T2.account_id = T1.account_id WHERE STRFTIME('%Y', T2.date) = '1998' AND T1.client_id = 617
Database: financial
Question: Please provide a list of clients who were born between 1983 and 1987 and whose account branch is in East Bohemia, along with their account IDs.
SQL:
SELECT T1.client_id, T3.account_id
FROM client AS T1
INNER JOIN district AS T2 ON T1.district_id = T2.district_id
INNER JOIN disp AS T4 ON T1.client_id = T4.client_id
INNER JOIN account AS T3 ON T2.district_id = T3.district_id AND T4.account_id = T3.account_id
WHERE T2.A3 = 'east Bohemia' AND STRFTIME('%Y', T1.birth_date) BETWEEN '1983' AND '1987';
Database: financial
Question: For the top 3 female clients with the largest loans, what are their loan details including total payments, interest paid, loan status, location, transaction activity, income category, and age?
Evidence: Female refers to gender = 'F'; total payments = monthly payments * duration; interest paid = total payments - loan amount; income category is based on total income compared to loan amount (High: >2x loan, Medium: >1x loan, Low: otherwise)
SQL:
WITH client_loans AS (
    SELECT 
        c.client_id,
        c.gender,
        d.account_id,
        l.loan_id,
        l.amount,
        l.duration,
        l.payments,
        l.status,
        a.district_id,
        di.A2 AS district_name,
        di.A3 AS region,
        RANK() OVER (PARTITION BY c.gender ORDER BY l.amount DESC) AS loan_rank,
        ROUND(l.payments * l.duration, 2) AS total_payments,
        ROUND(l.payments * l.duration - l.amount, 2) AS interest_paid
    FROM 
        client c
        JOIN disp d ON c.client_id = d.client_id AND d.type = 'OWNER'
        JOIN loan l ON d.account_id = l.account_id
        JOIN account a ON l.account_id = a.account_id
        JOIN district di ON a.district_id = di.district_id
    WHERE 
        c.gender = 'F'
),
transaction_summary AS (
    SELECT 
        cl.client_id,
        cl.loan_id,
        COUNT(t.trans_id) AS transaction_count,
        SUM(CASE WHEN t.type = 'PRIJEM' THEN t.amount ELSE 0 END) AS total_income,
        SUM(CASE WHEN t.type = 'VYDAJ' THEN t.amount ELSE 0 END) AS total_expenses
    FROM 
        client_loans cl
        LEFT JOIN trans t ON cl.account_id = t.account_id
    WHERE 
        cl.loan_rank <= 3
    GROUP BY 
        cl.client_id, cl.loan_id
)
SELECT 
    cl.client_id,
    cl.amount AS loan_amount,
    cl.duration AS loan_duration_months,
    cl.total_payments,
    cl.interest_paid,
    cl.status AS loan_status,
    cl.district_name,
    cl.region,
    ts.transaction_count,
    ts.total_income,
    ts.total_expenses,
    CASE 
        WHEN ts.total_income > cl.amount * 2 THEN 'High Income'
        WHEN ts.total_income > cl.amount THEN 'Medium Income'
        ELSE 'Low Income'
    END AS income_category,
    STRFTIME('%Y', CURRENT_DATE) - STRFTIME('%Y', c.birth_date) - 
        CASE WHEN STRFTIME('%m-%d', CURRENT_DATE) < STRFTIME('%m-%d', c.birth_date) THEN 1 ELSE 0 END AS client_age
FROM 
    client_loans cl
    JOIN transaction_summary ts ON cl.client_id = ts.client_id AND cl.loan_id = ts.loan_id
    JOIN client c ON cl.client_id = c.client_id
WHERE 
    cl.loan_rank <= 3
ORDER BY 
    cl.amount DESC;
Database: financial
Question: How many male customers who were born between 1974 and 1976 have made a payment on their home in excess of $4000?
Evidence: Man and male refers to gender = 'M'; 'SIPO' stands for household payment
SQL:
SELECT COUNT(T1.account_id) FROM trans AS T1 INNER JOIN account AS T2 ON T1.account_id = T2.account_id INNER JOIN disp AS T4 ON T2.account_id = T4.account_id INNER JOIN client AS T3 ON T4.client_id = T3.client_id WHERE STRFTIME('%Y', T3.birth_date) BETWEEN '1974' AND '1976' AND T3.gender = 'M' AND T1.amount > 4000 AND T1.k_symbol = 'SIPO'
Database: financial
Question: How many female customers have a junior credit card?
Evidence: Female refers to gender = 'F'
SQL:
SELECT COUNT(T1.client_id) FROM client AS T1 INNER JOIN disp AS T2 ON T1.client_id = T2.client_id INNER JOIN card AS T3 ON T2.disp_id = T3.disp_id WHERE T1.gender = 'F' AND T3.type = 'junior'
Database: financial
Question: What proportion of customers who have accounts at the Prague branch are female?
Evidence: Female refers to gender = 'F'; Proportion = [number of female clients with accounts in the Prague region / number of clients with accounts in the Prague region] * 100%.
SQL:
SELECT CAST(SUM(T2.gender = 'F') AS REAL) / COUNT(T2.client_id) * 100
FROM district AS T1
INNER JOIN client AS T2 ON T1.district_id = T2.district_id
WHERE T1.A3 = 'Prague';
Database: financial
Question: What percentage of male clients request for weekly statements to be issued?
Evidence: Percentage of male clients = [count(male clients who requested weekly statements / count(clients who requested weekly statements)] * 100%; Male means gender = 'M'; 'POPLATEK TYDNE' stands for weekly issuance
SQL:
SELECT CAST(SUM(T1.gender = 'M') AS REAL) * 100 / COUNT(T1.client_id) FROM client AS T1 INNER JOIN account AS T2 ON T2.district_id = T1.district_id INNER JOIN disp as T3 on T1.client_id = T3.client_id AND T2.account_id = T3.account_id WHERE T2.frequency = 'POPLATEK TYDNE'
Database: financial
Question: What are the key statistics for account owners with weekly statement issuance, including how many owners there are, their average number of transactions, how many have credit cards, how many are high-volume clients with over 10,000 in total transactions, the number of districts they're located in, and their average account balance?
Evidence: Weekly statement issuance refers to frequency = 'POPLATEK TYDNE'; high-volume clients are those with total transaction amount > 10000
SQL:
WITH ClientTransactionStats AS (
    SELECT 
        T2.client_id,
        COUNT(DISTINCT T3.trans_id) AS transaction_count,
        SUM(T3.amount) AS total_transaction_amount,
        AVG(T3.balance) AS avg_balance
    FROM account AS T1
    JOIN disp AS T2 ON T2.account_id = T1.account_id
    JOIN trans AS T3 ON T3.account_id = T1.account_id
    WHERE T1.frequency = 'POPLATEK TYDNE'
      AND T2.type = 'OWNER'
    GROUP BY T2.client_id
),
ClientCards AS (
    SELECT 
        T2.client_id,
        COUNT(DISTINCT C.card_id) AS card_count,
        GROUP_CONCAT(DISTINCT C.type) AS card_types
    FROM account AS T1
    JOIN disp AS T2 ON T2.account_id = T1.account_id
    JOIN disp AS D ON D.client_id = T2.client_id
    LEFT JOIN card AS C ON C.disp_id = D.disp_id
    WHERE T1.frequency = 'POPLATEK TYDNE'
      AND T2.type = 'OWNER'
    GROUP BY T2.client_id
)
SELECT 
    COUNT(DISTINCT CTS.client_id) AS total_weekly_statement_owners,
    AVG(CTS.transaction_count) AS avg_transactions_per_client,
    SUM(CASE WHEN CC.card_count > 0 THEN 1 ELSE 0 END) AS clients_with_cards,
    SUM(CASE WHEN CTS.total_transaction_amount > 10000 THEN 1 ELSE 0 END) AS high_volume_clients,
    (SELECT COUNT(DISTINCT CL.district_id) 
     FROM client CL
     JOIN disp D ON D.client_id = CL.client_id
     JOIN account A ON A.account_id = D.account_id
     WHERE A.frequency = 'POPLATEK TYDNE' AND D.type = 'OWNER') AS districts_count,
    ROUND(AVG(CTS.avg_balance), 2) AS average_client_balance
FROM ClientTransactionStats CTS
LEFT JOIN ClientCards CC ON CC.client_id = CTS.client_id
WHERE CTS.transaction_count > 0
  OR CC.card_count > 0;
Database: financial
Question: Name the account numbers of female clients who are oldest and have lowest average salary?
Evidence: Female refers to 'F' in the gender; A11 contains information about average salary
SQL:
SELECT T3.account_id
FROM client AS T1
INNER JOIN district AS T2 ON T1.district_id = T2.district_id
INNER JOIN account AS T3 ON T2.district_id = T3.district_id
INNER JOIN disp AS T4 ON T1.client_id = T4.client_id AND T4.account_id = T3.account_id
WHERE T1.gender = 'F'
ORDER BY T1.birth_date ASC, T2.A11 ASC
LIMIT 1;
Database: financial
Question: What are the comprehensive statistics for clients born in 1920 who live in east Bohemia, including their account activity, loan information, transaction patterns, gender distribution, and most common district?
Evidence: East Bohemia refers to region A3 = 'east Bohemia'. PRIJEM transactions represent income while VYDAJ transactions represent expenses.
SQL:
WITH ClientsInEastBohemia AS (
    SELECT 
        c.client_id,
        c.birth_date,
        c.gender,
        d.A2 AS district_name,
        d.A3 AS region,
        STRFTIME('%Y', c.birth_date) AS birth_year,
        COUNT(DISTINCT a.account_id) AS num_accounts,
        COUNT(DISTINCT l.loan_id) AS num_loans
    FROM 
        client AS c
    INNER JOIN 
        district AS d ON c.district_id = d.district_id
    LEFT JOIN 
        disp AS dp ON c.client_id = dp.client_id
    LEFT JOIN 
        account AS a ON dp.account_id = a.account_id
    LEFT JOIN 
        loan AS l ON a.account_id = l.account_id
    WHERE 
        STRFTIME('%Y', c.birth_date) = '1920' 
        AND d.A3 = 'east Bohemia'
    GROUP BY 
        c.client_id, c.birth_date, c.gender, d.A2, d.A3
),
TransactionStats AS (
    SELECT 
        dp.client_id,
        COUNT(t.trans_id) AS total_transactions,
        SUM(CASE WHEN t.type = 'PRIJEM' THEN t.amount ELSE 0 END) AS total_income,
        SUM(CASE WHEN t.type = 'VYDAJ' THEN t.amount ELSE 0 END) AS total_expenses,
        AVG(t.amount) AS avg_transaction_amount
    FROM 
        disp AS dp
    INNER JOIN 
        account AS a ON dp.account_id = a.account_id
    LEFT JOIN 
        trans AS t ON a.account_id = t.account_id
    WHERE 
        dp.client_id IN (SELECT client_id FROM ClientsInEastBohemia)
    GROUP BY 
        dp.client_id
)
SELECT 
    COUNT(c.client_id) AS total_clients_born_1920_in_east_bohemia,
    AVG(c.num_accounts) AS avg_accounts_per_client,
    AVG(c.num_loans) AS avg_loans_per_client,
    SUM(CASE WHEN c.gender = 'M' THEN 1 ELSE 0 END) AS male_clients,
    SUM(CASE WHEN c.gender = 'F' THEN 1 ELSE 0 END) AS female_clients,
    AVG(COALESCE(ts.total_transactions, 0)) AS avg_transactions_per_client,
    AVG(COALESCE(ts.total_income, 0)) AS avg_income_per_client,
    AVG(COALESCE(ts.total_expenses, 0)) AS avg_expenses_per_client,
    (SELECT district_name FROM ClientsInEastBohemia 
     GROUP BY district_name 
     ORDER BY COUNT(*) DESC LIMIT 1) AS most_common_district
FROM 
    ClientsInEastBohemia c
LEFT JOIN 
    TransactionStats ts ON c.client_id = ts.client_id
Database: financial
Question: What are the statistics for the top 50 highest loan amounts among 24-month loans with weekly statement issuance that have an owner, including total count, average loan amount, payment details, interest rates, loan status distribution, and district economic indicators?
Evidence: Weekly statement issuance refers to frequency = 'POPLATEK TYDNE'. Loan status: A = contract finished no problems, B = contract finished loan not paid, C = running contract OK so far, D = running contract client in debt.
SQL:
WITH LoanAccountStats AS (
    SELECT 
        a.account_id,
        a.district_id,
        a.frequency,
        l.duration,
        l.amount,
        l.payments,
        l.status,
        d.A2 AS district_name,
        d.A3 AS region,
        d.A11 AS avg_salary,
        d.A12 AS unemployment_rate_1995,
        ROUND(l.amount / l.duration, 2) AS monthly_principal,
        ROUND(l.payments - (l.amount / l.duration), 2) AS monthly_interest,
        ROUND(((l.payments * l.duration) - l.amount) / l.amount * 100, 2) AS interest_rate_percent,
        ROW_NUMBER() OVER (PARTITION BY l.duration ORDER BY l.amount DESC) AS amount_rank
    FROM 
        account AS a
        INNER JOIN loan AS l ON a.account_id = l.account_id
        INNER JOIN district AS d ON a.district_id = d.district_id
    WHERE 
        l.duration = 24 
        AND a.frequency = 'POPLATEK TYDNE'
)
SELECT 
    COUNT(las.account_id) AS total_count,
    AVG(las.amount) AS avg_loan_amount,
    MAX(las.amount) AS max_loan_amount,
    MIN(las.amount) AS min_loan_amount,
    AVG(las.payments) AS avg_monthly_payment,
    AVG(las.monthly_interest) AS avg_monthly_interest,
    AVG(las.interest_rate_percent) AS avg_interest_rate,
    SUM(CASE WHEN las.status = 'A' THEN 1 ELSE 0 END) AS status_a_count,
    SUM(CASE WHEN las.status = 'B' THEN 1 ELSE 0 END) AS status_b_count,
    SUM(CASE WHEN las.status = 'C' THEN 1 ELSE 0 END) AS status_c_count,
    SUM(CASE WHEN las.status = 'D' THEN 1 ELSE 0 END) AS status_d_count,
    AVG(las.avg_salary) AS avg_district_salary,
    AVG(las.unemployment_rate_1995) AS avg_unemployment_rate,
    COUNT(DISTINCT las.district_id) AS unique_districts,
    COUNT(DISTINCT las.region) AS unique_regions,
    (SELECT region FROM LoanAccountStats GROUP BY region ORDER BY COUNT(*) DESC LIMIT 1) AS top_region
FROM 
    LoanAccountStats AS las
WHERE 
    las.amount_rank <= 50
    AND EXISTS (
        SELECT 1 
        FROM disp AS d
        JOIN client AS c ON d.client_id = c.client_id
        WHERE d.account_id = las.account_id AND d.type = 'OWNER'
    );
Database: financial
Question: What is the average amount of loan which are still on running contract with statement issuance after each transaction?
Evidence: status = 'C' stands for running contract, OK so far; status = 'D' stands for running contract, client in debt. 'POPLATEK PO OBRATU' stands for issuance after transaction
SQL:
SELECT AVG(T2.amount) FROM account AS T1 INNER JOIN loan AS T2 ON T1.account_id = T2.account_id WHERE T2.status IN ('C', 'D') AND T1.frequency = 'POPLATEK PO OBRATU'
Database: financial
Question: List all ID and district for clients that can only have the right to issue permanent orders or apply for loans.
Evidence: Only the owner accounts have the right to issue permanent orders or apply for loans
SQL:
SELECT T3.client_id, T2.district_id, T2.A2 FROM account AS T1 INNER JOIN district AS T2 ON T1.district_id = T2.district_id INNER JOIN disp AS T3 ON T1.account_id = T3.account_id WHERE T3.type = 'OWNER'
Database: financial
Question: Provide the IDs and age of the client with high level credit card, which is eligible for loans.
Evidence: the credit card is high-level refers to card.type = 'gold'; eligible for loans refers to disp.type = 'OWNER'
SQL:
SELECT T1.client_id, STRFTIME('%Y', CURRENT_TIMESTAMP) - STRFTIME('%Y', T3.birth_date) FROM disp AS T1 INNER JOIN card AS T2 ON T2.disp_id = T1.disp_id INNER JOIN client AS T3 ON T1.client_id = T3.client_id WHERE T2.type = 'gold' AND T1.type = 'OWNER'
Database: toxicology
Question: For the most common bond type in the database, provide comprehensive statistics including its total occurrences, how many molecules contain it, the average number of such bonds per molecule, the number of unique element pairs it connects, and which molecule labels (carcinogenic or non-carcinogenic) contain this bond type.
Evidence: most common bond type refers to the bond_type with MAX(COUNT(bond_id)); molecule label refers to '+' for carcinogenic and '-' for non-carcinogenic
SQL:
WITH BondCounts AS (
    SELECT 
        b.bond_type,
        COUNT(b.bond_id) AS bond_count,
        RANK() OVER (ORDER BY COUNT(b.bond_id) DESC) AS bond_rank
    FROM bond b
    GROUP BY b.bond_type
),
MoleculeStats AS (
    SELECT 
        b.molecule_id,
        b.bond_type,
        COUNT(b.bond_id) AS bonds_per_molecule,
        m.label AS molecule_label
    FROM bond b
    JOIN molecule m ON b.molecule_id = m.molecule_id
    GROUP BY b.molecule_id, b.bond_type, m.label
),
AtomConnections AS (
    SELECT 
        b.bond_type,
        a1.element AS element1,
        a2.element AS element2,
        COUNT(*) AS connection_count
    FROM connected c
    JOIN bond b ON c.bond_id = b.bond_id
    JOIN atom a1 ON c.atom_id = a1.atom_id
    JOIN atom a2 ON c.atom_id2 = a2.atom_id
    GROUP BY b.bond_type, a1.element, a2.element
)
SELECT 
    bc.bond_type,
    bc.bond_count AS total_occurrences,
    COUNT(DISTINCT ms.molecule_id) AS molecules_containing,
    ROUND(AVG(ms.bonds_per_molecule), 2) AS avg_bonds_per_molecule,
    (SELECT COUNT(DISTINCT element1 || '-' || element2) 
     FROM AtomConnections ac 
     WHERE ac.bond_type = bc.bond_type) AS unique_element_pairs,
    GROUP_CONCAT(DISTINCT ms.molecule_label) AS molecule_labels
FROM BondCounts bc
JOIN MoleculeStats ms ON bc.bond_type = ms.bond_type
WHERE bc.bond_rank = 1
GROUP BY bc.bond_type, bc.bond_count
ORDER BY bc.bond_count DESC
LIMIT 1;
Database: toxicology
Question: For non-carcinogenic molecules containing chlorine, what are the statistics including the count of such molecules, average and maximum number of chlorine atoms, average number of bonds, count of molecules with more single bonds than double bonds, and average number of bonds involving chlorine?
Evidence: non-carcinogenic molecules refers to label = '-'; chlorine atoms refers to element = 'cl'; single bonds refers to bond_type = '-'; double bonds refers to bond_type = '='
SQL:
WITH ChlorineAtomCounts AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(a.atom_id) AS chlorine_atoms
    FROM 
        molecule m
    INNER JOIN 
        atom a ON m.molecule_id = a.molecule_id
    WHERE 
        a.element = 'cl'
    GROUP BY 
        m.molecule_id, m.label
),
MoleculeBondStats AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT b.bond_id) AS total_bonds,
        SUM(CASE WHEN b.bond_type = '-' THEN 1 ELSE 0 END) AS single_bonds,
        SUM(CASE WHEN b.bond_type = '=' THEN 1 ELSE 0 END) AS double_bonds
    FROM 
        molecule m
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    GROUP BY 
        m.molecule_id, m.label
),
ChlorineBondInfo AS (
    SELECT 
        m.molecule_id,
        COUNT(DISTINCT c.bond_id) AS bonds_with_chlorine
    FROM 
        molecule m
    INNER JOIN 
        atom a ON m.molecule_id = a.molecule_id
    INNER JOIN 
        connected c ON a.atom_id = c.atom_id
    WHERE 
        a.element = 'cl'
    GROUP BY 
        m.molecule_id
)
SELECT 
    COUNT(DISTINCT cac.molecule_id) AS non_carcinogenic_molecules_with_chlorine,
    AVG(cac.chlorine_atoms) AS avg_chlorine_atoms_per_molecule,
    MAX(cac.chlorine_atoms) AS max_chlorine_atoms,
    AVG(mbs.total_bonds) AS avg_bonds_per_molecule,
    SUM(CASE WHEN mbs.single_bonds > mbs.double_bonds THEN 1 ELSE 0 END) AS molecules_with_more_single_bonds,
    AVG(COALESCE(cbi.bonds_with_chlorine, 0)) AS avg_chlorine_bonds
FROM 
    ChlorineAtomCounts cac
INNER JOIN 
    MoleculeBondStats mbs ON cac.molecule_id = mbs.molecule_id
LEFT JOIN 
    ChlorineBondInfo cbi ON cac.molecule_id = cbi.molecule_id
WHERE 
    cac.label = '-'
HAVING 
    COUNT(DISTINCT cac.molecule_id) > 0
Database: toxicology
Question: Calculate the average number of oxygen atoms in single-bonded molecules.
Evidence: single-bonded molecules refers to bond_type = '-' ; average number of oxygen atom = AVG(element = 'o')
SQL:
SELECT AVG(oxygen_count) FROM (SELECT T1.molecule_id, COUNT(T1.element) AS oxygen_count FROM atom AS T1 INNER JOIN bond AS T2 ON T1.molecule_id = T2.molecule_id  WHERE T2.bond_type = '-' AND T1.element = 'o'  GROUP BY T1.molecule_id) AS oxygen_counts
Database: toxicology
Question: On average how many carcinogenic molecules are single bonded?
Evidence: carcinogenic molecules refers to label = '+'; single-bonded refers to bond_type = '-'; average = DIVIDE(SUM(bond_type = '-'), COUNT(atom_id))
SQL:
SELECT AVG(single_bond_count) FROM (SELECT T3.molecule_id, COUNT(T1.bond_type) AS single_bond_count FROM bond AS T1  INNER JOIN atom AS T2 ON T1.molecule_id = T2.molecule_id INNER JOIN molecule AS T3 ON T3.molecule_id = T2.molecule_id WHERE T1.bond_type = '-' AND T3.label = '+' GROUP BY T3.molecule_id) AS subquery
Database: toxicology
Question: In the molecule containing sodium atoms, how many are non-carcinogenic?
Evidence: non-carcinogenic refers to label = '-'; sodium atoms refers to element = 'na'
SQL:
SELECT COUNT(DISTINCT T2.molecule_id) FROM atom AS T1 INNER JOIN molecule AS T2 ON T1.molecule_id = T2.molecule_id WHERE T1.element = 'na' AND T2.label = '-'
Database: toxicology
Question: For carcinogenic molecules that contain triple bonds, provide detailed statistics including atom counts, bond counts, carbon and nitrogen counts, and specifically identify how many nitrogen-to-nitrogen triple bonds and carbon-containing triple bonds exist in each molecule, categorizing them by their number of triple bonds.
Evidence: carcinogenic molecules refers to label = '+'; triple bonds refers to bond_type = '#'
SQL:
WITH MoleculeStats AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT a.atom_id) AS atom_count,
        COUNT(DISTINCT b.bond_id) AS bond_count,
        SUM(CASE WHEN a.element = 'c' THEN 1 ELSE 0 END) AS carbon_count,
        SUM(CASE WHEN a.element = 'n' THEN 1 ELSE 0 END) AS nitrogen_count,
        SUM(CASE WHEN b.bond_type = '#' THEN 1 ELSE 0 END) AS triple_bond_count
    FROM 
        molecule m
    LEFT JOIN 
        atom a ON m.molecule_id = a.molecule_id
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    GROUP BY 
        m.molecule_id, m.label
),
TripleBondedAtoms AS (
    SELECT 
        c.atom_id,
        c.atom_id2,
        a1.element AS element1,
        a2.element AS element2,
        b.molecule_id
    FROM 
        connected c
    JOIN 
        bond b ON c.bond_id = b.bond_id
    JOIN 
        atom a1 ON c.atom_id = a1.atom_id
    JOIN 
        atom a2 ON c.atom_id2 = a2.atom_id
    WHERE 
        b.bond_type = '#'
)
SELECT 
    ms.molecule_id,
    ms.atom_count,
    ms.bond_count,
    ms.carbon_count,
    ms.nitrogen_count,
    ms.triple_bond_count,
    (SELECT COUNT(*) FROM TripleBondedAtoms tba WHERE tba.molecule_id = ms.molecule_id AND tba.element1 = 'n' AND tba.element2 = 'n') AS nitrogen_triple_bonds,
    (SELECT COUNT(*) FROM TripleBondedAtoms tba WHERE tba.molecule_id = ms.molecule_id AND (tba.element1 = 'c' OR tba.element2 = 'c')) AS carbon_containing_triple_bonds,
    CASE 
        WHEN ms.triple_bond_count > 1 THEN 'Multiple Triple Bonds'
        WHEN ms.triple_bond_count = 1 THEN 'Single Triple Bond'
        ELSE 'No Triple Bonds'
    END AS bond_category
FROM 
    MoleculeStats ms
WHERE 
    ms.label = '+' 
    AND ms.triple_bond_count > 0
ORDER BY 
    ms.triple_bond_count DESC,
    ms.atom_count DESC;
Database: toxicology
Question: What is the percentage of carbon in double-bond molecules?
Evidence: carbon refers to element = 'c'; double-bond molecules refers to bond_type = '='; percentage = DIVIDE(SUM(element = 'c'), COUNT(atom_id))
SQL:
SELECT CAST(COUNT(DISTINCT CASE WHEN T1.element = 'c' THEN T1.atom_id ELSE NULL END) AS REAL) * 100 / COUNT(DISTINCT T1.atom_id) FROM atom AS T1 INNER JOIN bond AS T2 ON T1.molecule_id = T2.molecule_id WHERE T2.bond_type = '='
Database: toxicology
Question: How many triple type bonds are there?
Evidence: triple type bonds refers to bond_type = '#'
SQL:
SELECT COUNT(T.bond_id) FROM bond AS T WHERE T.bond_type = '#'
Database: toxicology
Question: What is the average number of connections per non-bromine atom across molecules that have bonds or multiple atoms, and how many of these non-bromine atoms have more than 2 connections?
Evidence: Non-bromine atoms refers to element != 'br'; connections refer to bonds connected to an atom
SQL:
WITH BromineCount AS (
    SELECT 
        molecule_id,
        SUM(CASE WHEN element = 'br' THEN 1 ELSE 0 END) AS bromine_atoms,
        COUNT(*) AS total_atoms
    FROM 
        atom
    GROUP BY 
        molecule_id
),
MoleculeBonds AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT b.bond_id) AS bond_count
    FROM 
        molecule m
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    GROUP BY 
        m.molecule_id, m.label
),
AtomConnections AS (
    SELECT 
        a.atom_id,
        a.molecule_id,
        a.element,
        COUNT(c.bond_id) AS connection_count
    FROM 
        atom a
    LEFT JOIN 
        connected c ON a.atom_id = c.atom_id
    GROUP BY 
        a.atom_id, a.molecule_id, a.element
)
SELECT 
    COUNT(*) AS non_bromine_atoms,
    SUM(CASE WHEN ac.connection_count > 2 THEN 1 ELSE 0 END) AS highly_connected_non_bromine_atoms,
    COUNT(DISTINCT ac.molecule_id) AS molecules_with_non_bromine_atoms,
    ROUND(AVG(ac.connection_count), 2) AS avg_connections_per_non_bromine_atom
FROM 
    AtomConnections ac
JOIN 
    MoleculeBonds mb ON ac.molecule_id = mb.molecule_id
JOIN 
    BromineCount bc ON ac.molecule_id = bc.molecule_id
WHERE 
    ac.element <> 'br'
    AND (mb.bond_count > 0 OR bc.total_atoms > 1)
Database: toxicology
Question: What are the average structural characteristics of carcinogenic molecules among the first 100 molecules, including the number of atoms, carbon atoms, bonds, double bonds, connected atoms, and the ratio of carbon-to-carbon connections?
Evidence: first 100 molecules refers to molecule_id between 'TR000' and 'TR099'; carcinogenic molecules refers to label = '+'; only molecules containing carbon or hydrogen are considered
SQL:
WITH MoleculeInfo AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT a.atom_id) AS atom_count,
        COUNT(DISTINCT b.bond_id) AS bond_count,
        SUM(CASE WHEN a.element = 'c' THEN 1 ELSE 0 END) AS carbon_count,
        SUM(CASE WHEN a.element = 'h' THEN 1 ELSE 0 END) AS hydrogen_count,
        SUM(CASE WHEN a.element = 'o' THEN 1 ELSE 0 END) AS oxygen_count,
        SUM(CASE WHEN a.element = 'n' THEN 1 ELSE 0 END) AS nitrogen_count,
        SUM(CASE WHEN a.element NOT IN ('c', 'h', 'o', 'n') THEN 1 ELSE 0 END) AS other_element_count,
        SUM(CASE WHEN b.bond_type = '=' THEN 1 ELSE 0 END) AS double_bond_count,
        ROW_NUMBER() OVER (ORDER BY m.molecule_id) AS molecule_rank
    FROM 
        molecule m
    LEFT JOIN 
        atom a ON m.molecule_id = a.molecule_id
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    WHERE 
        m.molecule_id BETWEEN 'TR000' AND 'TR099'
    GROUP BY 
        m.molecule_id, m.label
),
ConnectionStats AS (
    SELECT 
        m.molecule_id,
        COUNT(DISTINCT c.atom_id) AS connected_atoms,
        AVG(CASE WHEN a.element = 'c' AND a2.element = 'c' THEN 1 ELSE 0 END) AS carbon_carbon_ratio
    FROM 
        molecule m
    JOIN 
        atom a ON m.molecule_id = a.molecule_id
    JOIN 
        connected c ON a.atom_id = c.atom_id
    JOIN 
        atom a2 ON c.atom_id2 = a2.atom_id
    WHERE 
        m.molecule_id BETWEEN 'TR000' AND 'TR099'
    GROUP BY 
        m.molecule_id
)
SELECT 
    COUNT(mi.molecule_id) AS carcinogenic_count,
    AVG(mi.atom_count) AS avg_atoms_in_carcinogenic,
    AVG(mi.carbon_count) AS avg_carbon_in_carcinogenic,
    AVG(mi.bond_count) AS avg_bonds_in_carcinogenic,
    AVG(mi.double_bond_count) AS avg_double_bonds_in_carcinogenic,
    AVG(cs.connected_atoms) AS avg_connected_atoms,
    AVG(cs.carbon_carbon_ratio) AS avg_carbon_carbon_ratio
FROM 
    MoleculeInfo mi
LEFT JOIN 
    ConnectionStats cs ON mi.molecule_id = cs.molecule_id
WHERE 
    mi.label = '+' 
    AND mi.molecule_rank <= 100
    AND (mi.carbon_count > 0 OR mi.hydrogen_count > 0)
Database: toxicology
Question: For all molecules containing carbon, provide comprehensive statistics including the number of atoms, bonds, carbon atoms, chlorine atoms, and double bonds. Rank these molecules by their carbon content (highest first), and for ties, rank by total atom count. Also include the total number of atomic connections in each molecule.
Evidence: carbon refers to element = 'c'; chlorine refers to element = 'cl'; double bonds refers to bond_type = '='
SQL:
WITH CarbonMolecules AS (
    SELECT DISTINCT a.molecule_id
    FROM atom AS a
    WHERE a.element = 'c'
),
MoleculeStats AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT a.atom_id) AS atom_count,
        COUNT(DISTINCT b.bond_id) AS bond_count,
        SUM(CASE WHEN a.element = 'c' THEN 1 ELSE 0 END) AS carbon_count,
        SUM(CASE WHEN a.element = 'cl' THEN 1 ELSE 0 END) AS chlorine_count,
        SUM(CASE WHEN b.bond_type = '=' THEN 1 ELSE 0 END) AS double_bond_count
    FROM molecule m
    JOIN atom a ON m.molecule_id = a.molecule_id
    LEFT JOIN bond b ON m.molecule_id = b.molecule_id
    WHERE m.molecule_id IN (SELECT molecule_id FROM CarbonMolecules)
    GROUP BY m.molecule_id, m.label
),
RankedMolecules AS (
    SELECT
        molecule_id,
        label,
        atom_count,
        bond_count,
        carbon_count,
        chlorine_count,
        double_bond_count,
        RANK() OVER (ORDER BY carbon_count DESC, atom_count DESC) AS carbon_rank
    FROM MoleculeStats
)
SELECT 
    rm.molecule_id,
    rm.label,
    rm.atom_count,
    rm.bond_count,
    rm.carbon_count,
    rm.chlorine_count,
    rm.double_bond_count,
    rm.carbon_rank,
    (SELECT COUNT(DISTINCT c.atom_id2) 
     FROM connected c 
     JOIN atom a ON c.atom_id = a.atom_id 
     WHERE a.molecule_id = rm.molecule_id) AS connections_count
FROM RankedMolecules rm
ORDER BY rm.carbon_rank, rm.molecule_id;
Database: toxicology
Question: What elements are in the TR004_8_9 bond atoms?
Evidence: TR004_8_9 bond atoms refers to bond_id = 'TR004_8_9';
SQL:
SELECT DISTINCT T1.element FROM atom AS T1 INNER JOIN connected AS T2 ON T1.atom_id = T2.atom_id WHERE T2.bond_id = 'TR004_8_9'
Database: toxicology
Question: For each element that participates in double bonds, what is the count of atoms, the number of molecules containing them, their percentage of all atoms in the database, and examples of up to 3 molecules (with labels) where they appear in double bonds?
Evidence: Double bonds refer to bond_type = '='. Only consider molecules that contain at least one double bond.
SQL:
WITH ElementsInDoubleBonds AS (
    SELECT 
        T1.element,
        T1.atom_id,
        T1.molecule_id
    FROM 
        atom AS T1
    INNER JOIN 
        connected AS T3 ON T1.atom_id = T3.atom_id
    INNER JOIN 
        bond AS T2 ON T3.bond_id = T2.bond_id
    WHERE 
        T2.bond_type = '='
),
MoleculeStats AS (
    SELECT 
        m.molecule_id,
        m.label,
        COUNT(DISTINCT a.atom_id) AS total_atoms,
        COUNT(DISTINCT b.bond_id) AS total_bonds,
        SUM(CASE WHEN b.bond_type = '=' THEN 1 ELSE 0 END) AS double_bonds_count
    FROM 
        molecule m
    LEFT JOIN 
        atom a ON m.molecule_id = a.molecule_id
    LEFT JOIN 
        bond b ON m.molecule_id = b.molecule_id
    GROUP BY 
        m.molecule_id, m.label
    HAVING 
        double_bonds_count > 0
)
SELECT 
    e.element,
    COUNT(DISTINCT e.atom_id) AS atom_count,
    COUNT(DISTINCT e.molecule_id) AS molecule_count,
    ROUND(COUNT(DISTINCT e.atom_id) * 100.0 / (SELECT COUNT(*) FROM atom), 2) AS percentage_of_all_atoms,
    (
        SELECT GROUP_CONCAT(ms.molecule_id || ' (' || ms.label || ')', ', ')
        FROM MoleculeStats ms
        WHERE ms.molecule_id IN (SELECT DISTINCT edb.molecule_id FROM ElementsInDoubleBonds edb WHERE edb.element = e.element)
        LIMIT 3
    ) AS sample_molecules
FROM 
    ElementsInDoubleBonds e
GROUP BY 
    e.element
ORDER BY 
    atom_count DESC, 
    e.element ASC;