birdsql/bird_sql_dev_20251106 huggingface dataset:
Database: financial
Question: What is the average loan amount by male borrowers?
Evidence: Male refers to gender = 'M'
SQL:
SELECT AVG(T4.amount) 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.gender = 'M'
Database: financial
Question: In 1996, which districts have the highest unemployment rate? List their branch location and district name.
Evidence: A2 refers to district names; A13 refers to unemploymant rate in 1996
SQL:
SELECT district_id, A2 FROM district WHERE A13 = (SELECT A13 FROM district ORDER BY A13 DESC LIMIT 1)
Database: financial
Question: For the district with the highest number of crimes in 1996, provide details including the district name, region, population, number of crimes, percentage increase in crimes from 1995 to 1996, and how many accounts were opened there.
Evidence: A16 refers to the number of committed crimes in 1996; A15 refers to the number of committed crimes in 1995
SQL:
WITH CrimeStats AS (
    SELECT 
        district_id,
        A16 AS crimes_1996,
        RANK() OVER (ORDER BY A16 DESC) AS crime_rank
    FROM district
),
AccountsByDistrict AS (
    SELECT 
        a.district_id,
        COUNT(a.account_id) AS account_count,
        AVG(JULIANDAY('1996-12-31') - JULIANDAY(a.date))/365.25 AS avg_account_age_years
    FROM account a
    WHERE a.date <= '1996-12-31'
    GROUP BY a.district_id
),
ClientsByDistrict AS (
    SELECT 
        c.district_id,
        COUNT(c.client_id) AS client_count,
        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
    FROM client c
    GROUP BY c.district_id
),
DistrictInfo AS (
    SELECT 
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        CAST(d.A4 AS INTEGER) AS inhabitants,
        d.A10 AS urban_ratio,
        d.A11 AS avg_salary,
        d.A15 AS crimes_1995,
        d.A16 AS crimes_1996,
        (d.A16 - d.A15) * 100.0 / NULLIF(d.A15, 0) AS crime_increase_percent
    FROM district d
)
SELECT 
    di.district_name,
    di.region,
    di.inhabitants,
    di.crimes_1996,
    di.crime_increase_percent,
    ad.account_count AS accounts_opened
FROM CrimeStats cs
JOIN AccountsByDistrict ad ON cs.district_id = ad.district_id
JOIN ClientsByDistrict cd ON cs.district_id = cd.district_id
JOIN DistrictInfo di ON cs.district_id = di.district_id
WHERE cs.crime_rank = 1;
Database: financial
Question: For accounts with monthly issuance that went into negative balance after a credit card withdrawal, what are the statistics including average negative balance, maximum withdrawal amount, average number of cards per account, total gold cards, average owner age, and gender distribution of account owners?
Evidence: Negative balance means balance < 0; credit card withdrawal refers to operation = 'VYBER KARTOU'; monthly issuance refers to frequency = 'POPLATEK MESICNE'; owner age is calculated as of January 1, 2000
SQL:
WITH AccountWithNegativeBalance AS (
    SELECT 
        t.account_id,
        t.date AS transaction_date,
        t.balance,
        t.operation,
        t.amount,
        a.frequency,
        ROW_NUMBER() OVER (PARTITION BY t.account_id ORDER BY t.date DESC) AS rn
    FROM 
        trans t
    INNER JOIN 
        account a ON t.account_id = a.account_id
    WHERE 
        t.balance < 0 
        AND t.operation = 'VYBER KARTOU' 
        AND a.frequency = 'POPLATEK MESICNE'
),
CardUsageStats AS (
    SELECT 
        d.account_id,
        COUNT(c.card_id) AS num_cards,
        MIN(c.issued) AS first_card_issued,
        MAX(c.issued) AS latest_card_issued,
        SUM(CASE WHEN c.type = 'gold' THEN 1 ELSE 0 END) AS gold_cards,
        SUM(CASE WHEN c.type = 'classic' THEN 1 ELSE 0 END) AS classic_cards
    FROM 
        card c
    INNER JOIN 
        disp d ON c.disp_id = d.disp_id
    GROUP BY 
        d.account_id
),
AccountOwnerInfo AS (
    SELECT 
        d.account_id,
        COUNT(DISTINCT cl.client_id) AS num_owners,
        AVG(julianday('2000-01-01') - julianday(cl.birth_date))/365.25 AS avg_owner_age,
        SUM(CASE WHEN cl.gender = 'M' THEN 1 ELSE 0 END) AS male_owners,
        SUM(CASE WHEN cl.gender = 'F' THEN 1 ELSE 0 END) AS female_owners
    FROM 
        disp d
    INNER JOIN 
        client cl ON d.client_id = cl.client_id
    WHERE 
        d.type = 'OWNER'
    GROUP BY 
        d.account_id
)
SELECT 
    COUNT(DISTINCT a.account_id) AS total_accounts_with_negative_balance,
    AVG(a.balance) AS avg_negative_balance,
    MAX(a.amount) AS max_withdrawal_amount,
    AVG(COALESCE(c.num_cards, 0)) AS avg_cards_per_account,
    SUM(COALESCE(c.gold_cards, 0)) AS total_gold_cards,
    AVG(COALESCE(o.avg_owner_age, 0)) AS avg_account_owner_age,
    SUM(CASE WHEN o.male_owners > o.female_owners THEN 1 ELSE 0 END) AS accounts_with_more_male_owners,
    SUM(CASE WHEN o.female_owners > o.male_owners THEN 1 ELSE 0 END) AS accounts_with_more_female_owners,
    SUM(CASE WHEN o.female_owners = o.male_owners AND o.num_owners > 0 THEN 1 ELSE 0 END) AS accounts_with_equal_gender_distribution
FROM 
    AccountWithNegativeBalance a
LEFT JOIN 
    CardUsageStats c ON a.account_id = c.account_id
LEFT JOIN 
    AccountOwnerInfo o ON a.account_id = o.account_id
WHERE 
    a.rn = 1
Database: financial
Question: Between 1/1/1995 and 12/31/1997, how many loans in the amount of at least 250,000 per account that chose monthly statement issuance were approved?
Evidence: Frequency = 'POPLATEK MESICNE' stands for monthly issurance
SQL:
SELECT COUNT(T1.account_id)
FROM account AS T1
INNER JOIN loan AS T2 ON T1.account_id = T2.account_id
WHERE T2.date BETWEEN '1995-01-01' AND '1997-12-31'
  AND T1.frequency = 'POPLATEK MESICNE'
  AND T2.amount >= 250000;
Database: financial
Question: What are the demographics and financial statistics of account owners with running loan contracts in district 1, including their average remaining debt, gender distribution, age, card ownership, transaction activity, and account balances?
Evidence: Running loan contracts include status 'C' (running contract, OK so far) and status 'D' (running contract, client in debt). Remaining debt is calculated as loan amount minus total payments made (payments * duration).
SQL:
WITH AccountsWithRunningLoans AS (
    SELECT 
        a.account_id,
        a.district_id,
        l.status,
        l.amount,
        l.duration,
        l.payments,
        (l.amount - (l.payments * l.duration)) AS remaining_debt
    FROM account AS a
    INNER JOIN loan AS l ON a.account_id = l.account_id
    WHERE a.district_id = 1 AND (l.status = 'C' OR l.status = 'D')
),
ClientDetails AS (
    SELECT 
        d.account_id,
        c.client_id,
        c.gender,
        strftime('%Y', 'now') - strftime('%Y', c.birth_date) AS client_age,
        COUNT(card.card_id) AS num_cards
    FROM disp AS d
    INNER JOIN client AS c ON d.client_id = c.client_id
    LEFT JOIN card ON d.disp_id = card.disp_id
    WHERE d.type = 'OWNER'
    GROUP BY d.account_id, c.client_id, c.gender, c.birth_date
),
TransactionStats AS (
    SELECT 
        account_id,
        COUNT(*) AS transaction_count,
        SUM(CASE WHEN type = 'PRIJEM' THEN amount ELSE 0 END) AS total_income,
        SUM(CASE WHEN type = 'VYDAJ' THEN amount ELSE 0 END) AS total_expense,
        MAX(balance) AS max_balance
    FROM trans
    GROUP BY account_id
)
SELECT 
    COUNT(DISTINCT arl.account_id) AS total_accounts_with_running_contracts,
    ROUND(AVG(arl.remaining_debt), 2) AS avg_remaining_debt,
    SUM(CASE WHEN cd.gender = 'M' THEN 1 ELSE 0 END) AS male_account_owners,
    SUM(CASE WHEN cd.gender = 'F' THEN 1 ELSE 0 END) AS female_account_owners,
    ROUND(AVG(cd.client_age), 0) AS avg_client_age,
    ROUND(AVG(cd.num_cards), 1) AS avg_cards_per_account,
    ROUND(AVG(CASE WHEN ts.transaction_count IS NOT NULL THEN ts.transaction_count ELSE 0 END), 0) AS avg_transactions_per_account,
    ROUND(AVG(CASE WHEN ts.max_balance IS NOT NULL THEN ts.max_balance ELSE 0 END), 2) AS avg_max_balance,
    d.A2 AS district_name,
    d.A3 AS region,
    d.A11 AS avg_salary_in_district
FROM AccountsWithRunningLoans AS arl
LEFT JOIN ClientDetails AS cd ON arl.account_id = cd.account_id
LEFT JOIN TransactionStats AS ts ON arl.account_id = ts.account_id
JOIN district AS d ON arl.district_id = d.district_id
Database: financial
Question: For the district with the second-highest number of crimes committed in 1995, provide a comprehensive breakdown of all male clients including their total count, the crime count for that year, total accounts, loans, credit cards, average dispositions per client, number of clients with loans, and age distribution across young (under 30), middle-aged (30-50), and senior (over 50) categories.
Evidence: Male refers to gender = 'M'; A15 refers to number of committed crimes in 1995; Age is calculated from birth_date to current date
SQL:
WITH CrimeRanking AS (
    SELECT 
        district_id,
        A15,
        RANK() OVER (ORDER BY A15 DESC) as crime_rank
    FROM district
),
SecondHighestCrimeDistrict AS (
    SELECT district_id, A15
    FROM CrimeRanking
    WHERE crime_rank = 2
),
ClientStats AS (
    SELECT 
        c.client_id,
        c.gender,
        c.district_id,
        COUNT(DISTINCT d.disp_id) as num_dispositions,
        COUNT(DISTINCT a.account_id) as num_accounts,
        COUNT(DISTINCT l.loan_id) as num_loans,
        COUNT(DISTINCT card.card_id) as num_cards,
        CASE 
            WHEN strftime('%Y', 'now') - strftime('%Y', c.birth_date) - 
                (strftime('%m-%d', 'now') < strftime('%m-%d', c.birth_date)) < 30 THEN 'Young'
            WHEN strftime('%Y', 'now') - strftime('%Y', c.birth_date) - 
                (strftime('%m-%d', 'now') < strftime('%m-%d', c.birth_date)) BETWEEN 30 AND 50 THEN 'Middle-aged'
            ELSE 'Senior'
        END as age_category
    FROM client c
    LEFT JOIN disp d ON c.client_id = d.client_id
    LEFT JOIN account a ON d.account_id = a.account_id
    LEFT JOIN loan l ON a.account_id = l.account_id
    LEFT JOIN card ON d.disp_id = card.disp_id
    WHERE c.gender = 'M'
    GROUP BY c.client_id, c.gender, c.district_id
)
SELECT 
    COUNT(cs.client_id) as total_male_clients,
    shcd.A15 as crime_count_1995,
    SUM(cs.num_accounts) as total_accounts,
    SUM(cs.num_loans) as total_loans,
    SUM(cs.num_cards) as total_cards,
    ROUND(AVG(cs.num_dispositions), 2) as avg_dispositions_per_client,
    SUM(CASE WHEN cs.num_loans > 0 THEN 1 ELSE 0 END) as clients_with_loans,
    SUM(CASE WHEN cs.age_category = 'Young' THEN 1 ELSE 0 END) as young_clients,
    SUM(CASE WHEN cs.age_category = 'Middle-aged' THEN 1 ELSE 0 END) as middle_aged_clients,
    SUM(CASE WHEN cs.age_category = 'Senior' THEN 1 ELSE 0 END) as senior_clients
FROM ClientStats cs
JOIN SecondHighestCrimeDistrict shcd ON cs.district_id = shcd.district_id
JOIN district d ON cs.district_id = d.district_id
WHERE cs.gender = 'M'
GROUP BY cs.district_id, d.A2, shcd.A15
Database: financial
Question: What is the demographic and financial profile of gold credit card owners with account balances averaging over 1,000, broken down by gender?
Evidence: Gold credit card owners refer to cards with type = 'gold' and disposition type = 'OWNER'. High balance accounts refer to accounts where avg_balance > 1000.
SQL:
WITH gold_owner_cards AS (
    SELECT 
        c.card_id,
        c.disp_id,
        c.type AS card_type,
        c.issued,
        d.account_id,
        d.client_id,
        d.type AS disp_type
    FROM card AS c
    INNER JOIN disp AS d ON c.disp_id = d.disp_id
    WHERE c.type = 'gold' AND d.type = 'OWNER'
),
client_demographics AS (
    SELECT 
        cl.client_id,
        cl.gender,
        strftime('%Y', 'now') - strftime('%Y', cl.birth_date) - 
            (strftime('%m-%d', 'now') < strftime('%m-%d', cl.birth_date)) AS age,
        di.A2 AS district_name,
        di.A3 AS region,
        di.A10 AS urban_ratio,
        di.A11 AS avg_salary
    FROM client AS cl
    JOIN district AS di ON cl.district_id = di.district_id
),
account_activity AS (
    SELECT 
        a.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,
        AVG(t.balance) AS avg_balance,
        COUNT(DISTINCT l.loan_id) AS loan_count,
        SUM(l.amount) AS total_loan_amount
    FROM account AS a
    LEFT JOIN trans AS t ON a.account_id = t.account_id
    LEFT JOIN loan AS l ON a.account_id = l.account_id
    GROUP BY a.account_id
)

SELECT 
    COUNT(goc.card_id) AS total_gold_owner_cards,
    AVG(cd.age) AS avg_cardholder_age,
    SUM(CASE WHEN cd.gender = 'M' THEN 1 ELSE 0 END) AS male_cardholders,
    SUM(CASE WHEN cd.gender = 'F' THEN 1 ELSE 0 END) AS female_cardholders,
    AVG(cd.avg_salary) AS avg_district_salary,
    AVG(aa.transaction_count) AS avg_transactions_per_account,
    AVG(aa.total_income) AS avg_income_per_account,
    AVG(aa.total_expense) AS avg_expense_per_account,
    SUM(CASE WHEN aa.loan_count > 0 THEN 1 ELSE 0 END) AS accounts_with_loans,
    COUNT(DISTINCT cd.region) AS distinct_regions,
    (SELECT COUNT(*) FROM card WHERE type = 'gold') AS total_gold_cards,
    ROUND(COUNT(goc.card_id) * 100.0 / (SELECT COUNT(*) FROM card WHERE type = 'gold'), 2) AS gold_owner_percentage
FROM gold_owner_cards AS goc
JOIN client_demographics AS cd ON goc.client_id = cd.client_id
JOIN account_activity AS aa ON goc.account_id = aa.account_id
WHERE aa.avg_balance > 1000
GROUP BY cd.gender
ORDER BY avg_cardholder_age DESC;
Database: financial
Question: What is the comprehensive financial profile of all accounts in the Pisek district, including the total number of accounts, average transactions per account, total deposits and withdrawals, average maximum balance, total credit cards issued, total loans issued, percentage of active loan amounts, and number of unique account owners?
Evidence: Active loans refer to status = 'A'; PRIJEM refers to deposits; VYDAJ refers to withdrawals; account owners refer to disposition type = 'OWNER'
SQL:
WITH PisekAccounts AS (
    SELECT 
        a.account_id, 
        a.district_id,
        a.frequency,
        a.date,
        d.A2 AS district_name
    FROM account AS a
    INNER JOIN district AS d ON a.district_id = d.district_id
    WHERE d.A2 = 'Pisek'
),
AccountStats AS (
    SELECT 
        pa.account_id,
        COUNT(DISTINCT t.trans_id) AS transaction_count,
        SUM(CASE WHEN t.type = 'PRIJEM' THEN t.amount ELSE 0 END) AS total_deposits,
        SUM(CASE WHEN t.type = 'VYDAJ' THEN t.amount ELSE 0 END) AS total_withdrawals,
        MAX(t.balance) AS max_balance,
        COUNT(DISTINCT c.card_id) AS card_count
    FROM PisekAccounts pa
    LEFT JOIN trans t ON pa.account_id = t.account_id
    LEFT JOIN disp d ON pa.account_id = d.account_id
    LEFT JOIN card c ON d.disp_id = c.disp_id
    GROUP BY pa.account_id
),
LoanInfo AS (
    SELECT
        pa.account_id,
        COUNT(l.loan_id) AS loan_count,
        SUM(l.amount) AS total_loan_amount,
        SUM(CASE WHEN l.status = 'A' THEN l.amount ELSE 0 END) AS active_loan_amount,
        AVG(l.duration) AS avg_loan_duration
    FROM PisekAccounts pa
    LEFT JOIN loan l ON pa.account_id = l.account_id
    GROUP BY pa.account_id
)
SELECT 
    COUNT(pa.account_id) AS total_pisek_accounts,
    AVG(ast.transaction_count) AS avg_transactions_per_account,
    SUM(ast.total_deposits) AS total_deposits_all_accounts,
    SUM(ast.total_withdrawals) AS total_withdrawals_all_accounts,
    AVG(ast.max_balance) AS avg_max_balance,
    SUM(ast.card_count) AS total_cards_issued,
    SUM(li.loan_count) AS total_loans_issued,
    SUM(li.active_loan_amount) / CASE WHEN SUM(li.total_loan_amount) = 0 THEN 1 ELSE SUM(li.total_loan_amount) END * 100 AS percent_active_loans,
    (SELECT COUNT(DISTINCT c.client_id) 
     FROM PisekAccounts pa 
     JOIN disp d ON pa.account_id = d.account_id 
     JOIN client c ON d.client_id = c.client_id
     WHERE d.type = 'OWNER') AS unique_account_owners
FROM PisekAccounts pa
LEFT JOIN AccountStats ast ON pa.account_id = ast.account_id
LEFT JOIN LoanInfo li ON pa.account_id = li.account_id;
Database: financial
Question: Which districts have transactions greater than USD$10,000 in 1997?
SQL:
SELECT district_id
FROM account AS T1
INNER JOIN trans AS T3 ON T1.account_id = T3.account_id
WHERE STRFTIME('%Y', T3.date) = '1997'
GROUP BY district_id
HAVING SUM(T3.amount) > 10000
Database: financial
Question: Which accounts placed orders for household payment in Pisek?
Evidence: k_symbol = 'SIPO' refers to household payment
SQL:
SELECT DISTINCT T2.account_id FROM trans AS T1 INNER JOIN account AS T2 ON T1.account_id = T2.account_id INNER JOIN district AS T3 ON T2.district_id = T3.district_id WHERE T1.k_symbol = 'SIPO' AND T3.A2 = 'Pisek'
Database: financial
Question: For each account with gold credit cards, provide a comprehensive financial profile including the number of gold cards, location details, transaction history, balance statistics, loan information, and rank the accounts by their average balance.
Evidence: Transaction income refers to type = 'PRIJEM'; transaction expense refers to type = 'VYDAJ'; good loans refers to status = 'A'; bad loans refers to status = 'B'.
SQL:
WITH GoldCardAccounts AS (
    SELECT 
        T2.account_id,
        COUNT(T1.card_id) AS gold_card_count
    FROM disp AS T2  
    INNER JOIN card AS T1 ON T1.disp_id = T2.disp_id  
    WHERE T1.type = 'gold'
    GROUP BY T2.account_id
),
AccountDetails AS (
    SELECT 
        a.account_id,
        a.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        a.frequency,
        a.date AS account_opening_date,
        JULIANDAY('now') - JULIANDAY(a.date) AS account_age_days
    FROM account a
    JOIN district d ON a.district_id = d.district_id
),
AccountTransactions 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.balance) AS max_balance,
        AVG(t.balance) AS avg_balance
    FROM trans t
    GROUP BY t.account_id
),
AccountLoans AS (
    SELECT 
        l.account_id,
        COUNT(l.loan_id) AS loan_count,
        SUM(l.amount) AS total_loan_amount,
        SUM(CASE WHEN l.status = 'A' THEN l.amount ELSE 0 END) AS good_loans,
        SUM(CASE WHEN l.status = 'B' THEN l.amount ELSE 0 END) AS bad_loans
    FROM loan l
    GROUP BY l.account_id
)
SELECT 
    gca.account_id,
    gca.gold_card_count,
    ad.district_name,
    ad.region,
    ad.frequency,
    ad.account_opening_date,
    ROUND(ad.account_age_days/365.25, 2) AS account_age_years,
    COALESCE(at.transaction_count, 0) AS transaction_count,
    COALESCE(at.total_income, 0) AS total_income,
    COALESCE(at.total_expense, 0) AS total_expense,
    COALESCE(at.total_income, 0) - COALESCE(at.total_expense, 0) AS net_flow,
    COALESCE(at.max_balance, 0) AS max_balance,
    COALESCE(at.avg_balance, 0) AS avg_balance,
    COALESCE(al.loan_count, 0) AS loan_count,
    COALESCE(al.total_loan_amount, 0) AS total_loan_amount,
    COALESCE(al.good_loans, 0) AS good_loans,
    COALESCE(al.bad_loans, 0) AS bad_loans,
    CASE 
        WHEN COALESCE(al.loan_count, 0) = 0 THEN 'No Loans'
        WHEN COALESCE(al.bad_loans, 0) > 0 THEN 'Has Bad Loans'
        ELSE 'Good Standing'
    END AS loan_status,
    DENSE_RANK() OVER (ORDER BY COALESCE(at.avg_balance, 0) DESC) AS balance_rank
FROM GoldCardAccounts gca
LEFT JOIN AccountDetails ad ON gca.account_id = ad.account_id
LEFT JOIN AccountTransactions at ON gca.account_id = at.account_id
LEFT JOIN AccountLoans al ON gca.account_id = al.account_id
ORDER BY balance_rank, gca.account_id
Database: financial
Question: What is the breakdown of credit card transaction patterns in 1998 by region, district, gender, and card type, including average transaction amounts, total spending, and how these amounts compare to district average salaries?
Evidence: Credit card transactions refer to operation = 'VYBER KARTOU'. Only account owners with credit cards are considered.
SQL:
WITH CardHolders AS (
    SELECT 
        c.client_id,
        c.gender,
        d.disp_id,
        d.account_id,
        a.district_id,
        cd.type AS card_type,
        cd.issued AS card_issue_date
    FROM client c
    JOIN disp d ON c.client_id = d.client_id
    JOIN card cd ON d.disp_id = cd.disp_id
    JOIN account a ON d.account_id = a.account_id
    WHERE d.type = 'OWNER'
),
TransactionStats AS (
    SELECT 
        t.account_id,
        ch.client_id,
        ch.gender,
        ch.card_type,
        STRFTIME('%Y', t.date) AS trans_year,
        STRFTIME('%m', t.date) AS trans_month,
        t.amount,
        t.operation,
        t.balance,
        t.k_symbol,
        t.bank,
        ROW_NUMBER() OVER (PARTITION BY t.account_id ORDER BY t.date) AS transaction_sequence,
        AVG(t.amount) OVER (PARTITION BY t.account_id) AS avg_transaction_amount,
        SUM(t.amount) OVER (PARTITION BY t.account_id) AS total_spent,
        COUNT(*) OVER (PARTITION BY t.account_id) AS transaction_count
    FROM trans t
    JOIN CardHolders ch ON t.account_id = ch.account_id
    WHERE t.operation = 'VYBER KARTOU'
    AND STRFTIME('%Y', t.date) = '1998'
),
DistrictInfo AS (
    SELECT 
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        CAST(d.A11 AS INTEGER) AS avg_salary,
        CAST(d.A10 AS REAL) AS urban_ratio
    FROM district d
)

SELECT 
    di.region,
    di.district_name,
    ts.gender,
    ts.card_type,
    COUNT(DISTINCT ts.client_id) AS num_clients,
    COUNT(DISTINCT ts.account_id) AS num_accounts,
    ROUND(AVG(ts.amount), 2) AS avg_transaction_amount,
    ROUND(SUM(ts.amount), 2) AS total_amount,
    ROUND(MAX(ts.amount), 2) AS max_transaction,
    ROUND(MIN(ts.amount), 2) AS min_transaction,
    AVG(ts.transaction_count) AS avg_transactions_per_account,
    ROUND(AVG(CASE WHEN ts.gender = 'M' THEN ts.amount ELSE NULL END), 2) AS male_avg_amount,
    ROUND(AVG(CASE WHEN ts.gender = 'F' THEN ts.amount ELSE NULL END), 2) AS female_avg_amount,
    ROUND(AVG(di.avg_salary), 2) AS district_avg_salary,
    ROUND(AVG(ts.amount) / AVG(di.avg_salary) * 100, 2) AS pct_of_avg_salary
FROM TransactionStats ts
JOIN CardHolders ch ON ts.account_id = ch.account_id
JOIN DistrictInfo di ON ch.district_id = di.district_id
GROUP BY di.region, di.district_name, ts.gender, ts.card_type
HAVING COUNT(ts.amount) > 0
ORDER BY avg_transaction_amount DESC;
Database: financial
Question: Who are the account holder identification numbers whose who have transactions on the credit card with the amount is less than the average, in 1998?
Evidence: Operation = 'VYBER KARTOU' refers to credit card withdrawal
SQL:
SELECT DISTINCT T1.account_id FROM trans AS T1 INNER JOIN account AS T2 ON T1.account_id = T2.account_id WHERE STRFTIME('%Y', T1.date) = '1998' AND T1.operation = 'VYBER KARTOU' AND T1.amount < (SELECT AVG(amount) FROM trans WHERE STRFTIME('%Y', date) = '1998')
Database: financial
Question: What is the demographic and financial profile of female account owners in south Bohemia, broken down by district and age group, including their average transaction activity, net balance, loan amounts, and active loan rates?
Evidence: Female refers to gender = 'F'; south Bohemia refers to region A3 = 'south Bohemia'; age categories are Young (born after 1980), Middle-aged (born 1960-1980), and Senior (born before 1960); net balance = total income - total expense; active loans refer to loan status = 'A'
SQL:
WITH ClientAccounts AS (
    SELECT 
        c.client_id,
        c.gender,
        a.account_id,
        d.A3 AS region,
        d.A2 AS district_name,
        CASE 
            WHEN c.birth_date > '1980-01-01' THEN 'Young'
            WHEN c.birth_date BETWEEN '1960-01-01' AND '1980-01-01' THEN 'Middle-aged'
            ELSE 'Senior'
        END AS age_category,
        a.frequency
    FROM client c
    JOIN district d ON c.district_id = d.district_id
    JOIN disp dp ON c.client_id = dp.client_id
    JOIN account a ON dp.account_id = a.account_id
    WHERE c.gender = 'F' AND d.A3 = 'south Bohemia' AND dp.type = 'OWNER'
),
AccountTransactions AS (
    SELECT 
        ca.client_id,
        ca.account_id,
        ca.district_name,
        ca.age_category,
        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 ClientAccounts ca
    LEFT JOIN trans t ON ca.account_id = t.account_id
    GROUP BY ca.client_id, ca.account_id, ca.district_name, ca.age_category
),
LoanStatus AS (
    SELECT
        ca.client_id,
        ca.account_id,
        COUNT(l.loan_id) AS loan_count,
        SUM(l.amount) AS total_loan_amount,
        SUM(CASE WHEN l.status = 'A' THEN 1 ELSE 0 END) AS active_loans
    FROM ClientAccounts ca
    LEFT JOIN loan l ON ca.account_id = l.account_id
    GROUP BY ca.client_id, ca.account_id
)
SELECT 
    at.district_name,
    at.age_category,
    COUNT(DISTINCT at.client_id) AS client_count,
    COUNT(DISTINCT at.account_id) AS account_count,
    ROUND(AVG(at.transaction_count), 2) AS avg_transactions_per_account,
    ROUND(AVG(at.total_income - at.total_expense), 2) AS avg_net_balance,
    ROUND(AVG(CASE WHEN ls.loan_count > 0 THEN ls.total_loan_amount / ls.loan_count ELSE 0 END), 2) AS avg_loan_amount,
    SUM(ls.active_loans) AS total_active_loans,
    ROUND(SUM(ls.active_loans) * 100.0 / COUNT(DISTINCT at.client_id), 2) AS active_loans_per_100_clients
FROM AccountTransactions at
LEFT JOIN LoanStatus ls ON at.client_id = ls.client_id AND at.account_id = ls.account_id
GROUP BY at.district_name, at.age_category
ORDER BY at.district_name, 
    CASE 
        WHEN at.age_category = 'Young' THEN 1
        WHEN at.age_category = 'Middle-aged' THEN 2
        ELSE 3
    END;
Database: financial
Question: Please list the account types that are not eligible for loans, and the average income of residents in the district where the account is located exceeds $8000 but is no more than $9000.
Evidence: A11 represents the average salary; Salary and income share the similar meanings; when the account type = 'OWNER', it's eligible for loans
SQL:
SELECT DISTINCT T3.type FROM district AS T1 INNER JOIN account AS T2 ON T1.district_id = T2.district_id INNER JOIN disp AS T3 ON T2.account_id = T3.account_id WHERE T3.type != 'OWNER' AND T1.A11 BETWEEN 8000 AND 9000
Database: financial
Question: How many accounts in North Bohemia has made a transaction with the partner's bank being AB?
Evidence: A3 contains the region names; North Bohemia is a region.
SQL:
SELECT COUNT(T2.account_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 T3.bank = 'AB' AND T1.A3 = 'north Bohemia'
Database: financial
Question: For each district with withdrawal transactions, provide a comprehensive analysis including the total number and amount of withdrawals, demographic information, withdrawal per capita, and rank the districts by total withdrawal amount. Also categorize each district by unemployment level.
Evidence: Withdrawal transactions refer to type = 'VYDAJ'. District name refers to A2. Unemployment category is based on 1995 unemployment rate: High (>2.0), Medium (1.0-2.0), Low (<1.0).
SQL:
WITH district_withdrawal_counts AS (
    SELECT 
        T1.district_id,
        T1.A2 AS district_name,
        COUNT(DISTINCT T3.trans_id) AS withdrawal_count,
        SUM(T3.amount) AS total_withdrawal_amount,
        AVG(T3.amount) AS avg_withdrawal_amount
    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 T3.type = 'VYDAJ'
    GROUP BY T1.district_id, T1.A2
),
district_demographics AS (
    SELECT
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        CAST(d.A4 AS INTEGER) AS population,
        d.A10 AS urban_ratio,
        d.A11 AS avg_salary,
        d.A12 AS unemployment_rate_95,
        COUNT(DISTINCT c.client_id) AS client_count,
        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
    FROM district d
    LEFT JOIN client c ON d.district_id = c.district_id
    GROUP BY d.district_id, d.A2, d.A3, d.A4, d.A10, d.A11, d.A12
)
SELECT 
    dw.district_name,
    dw.withdrawal_count,
    dw.total_withdrawal_amount,
    dw.avg_withdrawal_amount,
    dd.region,
    dd.population,
    dd.urban_ratio,
    dd.avg_salary,
    dd.unemployment_rate_95,
    dd.client_count,
    dd.male_clients,
    dd.female_clients,
    ROUND(dw.total_withdrawal_amount / CAST(dd.population AS REAL), 2) AS withdrawal_per_capita,
    RANK() OVER (ORDER BY dw.total_withdrawal_amount DESC) AS district_rank_by_withdrawal,
    CASE 
        WHEN dd.unemployment_rate_95 > 2.0 THEN 'High Unemployment'
        WHEN dd.unemployment_rate_95 BETWEEN 1.0 AND 2.0 THEN 'Medium Unemployment'
        ELSE 'Low Unemployment'
    END AS unemployment_category
FROM district_withdrawal_counts dw
JOIN district_demographics dd ON dw.district_id = dd.district_id
ORDER BY dw.total_withdrawal_amount DESC;
Database: financial
Question: What is the average number of crimes committed in 1995 in regions where the number exceeds 4000 and the region has accounts that are opened starting from the year 1997?
Evidence: A15 stands for the average number of crimes committed in 1995.
SQL:
SELECT AVG(sub.A15) AS avg_crimes_1995
FROM (
  SELECT DISTINCT
    T1.district_id,
    T1.A15
  FROM district AS T1
  INNER JOIN account AS T2 ON T1.district_id = T2.district_id
  WHERE STRFTIME('%Y', T2.date) >= '1997'
    AND T1.A15 > 4000
) AS sub;
Database: financial
Question: For each district, how many classic credit card holders who are account owners are there, and what are their average loan amounts, account balances, loan performance, district salary, and unemployment rate?
Evidence: Account owners refer to disp.type = 'OWNER'. Good loans refer to status = 'A' and bad loans refer to status = 'B'. Account balance is calculated as total income minus total expense.
SQL:
WITH loan_stats AS (
    SELECT 
        l.account_id,
        COUNT(l.loan_id) AS loan_count,
        AVG(l.amount) AS avg_loan_amount,
        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
    FROM loan l
    GROUP BY l.account_id
),
account_activity AS (
    SELECT 
        a.account_id,
        COUNT(t.trans_id) AS transaction_count,
        MAX(t.date) AS last_transaction_date,
        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
    FROM account a
    LEFT JOIN trans t ON a.account_id = t.account_id
    GROUP BY a.account_id
),
district_metrics AS (
    SELECT 
        d.district_id,
        d.A2 AS district_name,
        d.A11 AS avg_salary,
        d.A12 AS unemployment_rate_95,
        RANK() OVER (ORDER BY d.A11 DESC) AS salary_rank,
        RANK() OVER (ORDER BY d.A12) AS unemployment_rank
    FROM district d
)

SELECT 
    COUNT(c.card_id) AS eligible_classic_cards,
    dm.district_name,
    ROUND(AVG(ls.avg_loan_amount), 2) AS avg_loan_amount,
    ROUND(AVG(aa.total_income - aa.total_expense), 2) AS avg_account_balance,
    SUM(ls.good_loans) AS total_good_loans,
    SUM(ls.bad_loans) AS total_bad_loans,
    ROUND(AVG(dm.avg_salary), 2) AS district_avg_salary,
    ROUND(AVG(dm.unemployment_rate_95), 2) AS district_unemployment_rate
FROM card c
INNER JOIN disp d ON c.disp_id = d.disp_id
INNER JOIN account a ON d.account_id = a.account_id
INNER JOIN district_metrics dm ON a.district_id = dm.district_id
LEFT JOIN loan_stats ls ON a.account_id = ls.account_id
LEFT JOIN account_activity aa ON a.account_id = aa.account_id
WHERE c.type = 'classic' 
AND d.type = 'OWNER'
GROUP BY dm.district_name
HAVING COUNT(c.card_id) > 0
ORDER BY eligible_classic_cards DESC, avg_loan_amount DESC;
Database: financial
Question: What is the financial profile and banking behavior of male clients in Prague district, broken down by age groups (Young, Middle-aged, and Senior)?
Evidence: Young clients are under 30 years old, Middle-aged clients are between 30 and 50 years old, and Senior clients are over 50 years old. Status 'A' indicates good loans and status 'B' indicates bad loans. PRIJEM represents income transactions and VYDAJ represents expense transactions.
SQL:
WITH PrahaClients AS (
    SELECT 
        c.client_id,
        c.gender,
        c.birth_date,
        d.A2 AS district_name,
        CASE 
            WHEN strftime('%Y', 'now') - strftime('%Y', c.birth_date) - (strftime('%m-%d', 'now') < strftime('%m-%d', c.birth_date)) < 30 THEN 'Young'
            WHEN strftime('%Y', 'now') - strftime('%Y', c.birth_date) - (strftime('%m-%d', 'now') < strftime('%m-%d', c.birth_date)) BETWEEN 30 AND 50 THEN 'Middle-aged'
            ELSE 'Senior'
        END AS age_category
    FROM client AS c
    INNER JOIN district AS d ON c.district_id = d.district_id
    WHERE c.gender = 'M' AND d.A2 = 'Hl.m. Praha'
),
ClientAccounts AS (
    SELECT 
        pc.client_id,
        pc.gender,
        pc.age_category,
        a.account_id,
        COUNT(DISTINCT 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,
        COUNT(DISTINCT c.card_id) AS card_count,
        COUNT(DISTINCT 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
    FROM PrahaClients pc
    INNER JOIN disp AS dp ON pc.client_id = dp.client_id AND dp.type = 'OWNER'
    INNER JOIN account AS a ON dp.account_id = a.account_id
    LEFT JOIN loan AS l ON a.account_id = l.account_id
    LEFT JOIN disp AS dp2 ON a.account_id = dp2.account_id
    LEFT JOIN card AS c ON dp2.disp_id = c.disp_id
    LEFT JOIN trans AS t ON a.account_id = t.account_id
    GROUP BY pc.client_id, pc.gender, pc.age_category, a.account_id
)
SELECT 
    age_category,
    COUNT(DISTINCT client_id) AS client_count,
    AVG(loan_count) AS avg_loans_per_client,
    SUM(good_loans) AS total_good_loans,
    SUM(bad_loans) AS total_bad_loans,
    AVG(card_count) AS avg_cards_per_client,
    AVG(transaction_count) AS avg_transactions_per_client,
    SUM(total_income) AS total_income_all_clients,
    SUM(total_expense) AS total_expense_all_clients,
    SUM(total_income - total_expense) AS net_balance_all_clients
FROM ClientAccounts
GROUP BY age_category
HAVING COUNT(DISTINCT client_id) > 0
ORDER BY 
    CASE 
        WHEN age_category = 'Young' THEN 1
        WHEN age_category = 'Middle-aged' THEN 2
        WHEN age_category = 'Senior' THEN 3
    END;
Database: financial
Question: What percentage of gold credit cards were issued before 1998, both overall and broken down by region?
Evidence: Percentage of gold cards issued before 1998 = count(gold cards issued before 1998) / count(all gold cards) * 100. Region refers to A3 in district table.
SQL:
WITH GoldCardStats AS (
    SELECT 
        c.card_id,
        STRFTIME('%Y', c.issued) AS issue_year,
        d.account_id,
        CASE WHEN STRFTIME('%Y', c.issued) < '1998' THEN 1 ELSE 0 END AS is_pre_1998
    FROM card c
    JOIN disp d ON c.disp_id = d.disp_id
    WHERE c.type = 'gold'
),
AccountDetails AS (
    SELECT 
        a.account_id,
        a.district_id,
        d.A2 AS district_name,
        d.A3 AS region
    FROM account a
    JOIN district d ON a.district_id = d.district_id
),
RegionalStats AS (
    SELECT 
        ad.region,
        COUNT(gcs.card_id) AS total_gold_cards,
        SUM(gcs.is_pre_1998) AS pre_1998_gold_cards,
        CAST(SUM(gcs.is_pre_1998) AS REAL) * 100.0 / NULLIF(COUNT(gcs.card_id), 0) AS percent_pre_1998
    FROM GoldCardStats gcs
    JOIN AccountDetails ad ON gcs.account_id = ad.account_id
    GROUP BY ad.region
)
SELECT 
    'Overall' AS category,
    COUNT(card_id) AS total_gold_cards,
    SUM(is_pre_1998) AS pre_1998_gold_cards,
    CAST(SUM(is_pre_1998) AS REAL) * 100.0 / NULLIF(COUNT(card_id), 0) AS percent_pre_1998
FROM GoldCardStats
UNION ALL
SELECT 
    'By Region: ' || region AS category,
    total_gold_cards,
    pre_1998_gold_cards,
    percent_pre_1998
FROM RegionalStats
ORDER BY percent_pre_1998 DESC;
Database: financial
Question: What are the demographic, financial, and transaction details of the account owner who has the largest loan amount, including their age category, savings rate, and the economic status of their district?
Evidence: Savings rate is calculated as (total income - total expense) / total income * 100. Age categories are: Young (under 30), Middle-aged (30-50), Senior (over 50). High unemployment area refers to districts with unemployment rate above 1.0%.
SQL:
WITH LoanRanking AS (
    SELECT 
        l.account_id,
        l.amount,
        l.loan_id,
        RANK() OVER (ORDER BY l.amount DESC) as loan_rank
    FROM loan l
),
AccountOwners AS (
    SELECT 
        d.client_id,
        d.account_id,
        c.gender,
        c.birth_date,
        CAST(strftime('%Y', 'now') - strftime('%Y', c.birth_date) AS INTEGER) - 
            CASE WHEN strftime('%m-%d', 'now') < strftime('%m-%d', c.birth_date) THEN 1 ELSE 0 END AS age
    FROM disp d
    JOIN client c ON d.client_id = c.client_id
    WHERE d.type = 'OWNER'
),
TransactionSummary AS (
    SELECT 
        account_id,
        COUNT(*) AS transaction_count,
        SUM(CASE WHEN type = 'PRIJEM' THEN amount ELSE 0 END) AS total_income,
        SUM(CASE WHEN type = 'VYDAJ' THEN amount ELSE 0 END) AS total_expense
    FROM trans
    GROUP BY account_id
),
DistrictInfo AS (
    SELECT 
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        d.A11 AS avg_salary,
        d.A12 AS unemployment_rate
    FROM district d
)
SELECT 
    ao.client_id,
    c.gender,
    ao.age,
    di.district_name,
    di.region,
    lr.amount AS loan_amount,
    lr.loan_id,
    ts.transaction_count,
    ts.total_income,
    ts.total_expense,
    ROUND((ts.total_income - ts.total_expense) * 1.0 / CASE WHEN ts.total_income = 0 THEN 1 ELSE ts.total_income END * 100, 2) AS savings_rate_pct,
    CASE 
        WHEN ao.age < 30 THEN 'Young'
        WHEN ao.age BETWEEN 30 AND 50 THEN 'Middle-aged'
        ELSE 'Senior'
    END AS age_category,
    CASE 
        WHEN di.unemployment_rate > 1.0 THEN 'High unemployment area'
        ELSE 'Low unemployment area'
    END AS area_status
FROM LoanRanking lr
JOIN AccountOwners ao ON lr.account_id = ao.account_id
JOIN client c ON ao.client_id = c.client_id
JOIN DistrictInfo di ON c.district_id = di.district_id
LEFT JOIN TransactionSummary ts ON ao.account_id = ts.account_id
WHERE lr.loan_rank = 1
ORDER BY lr.amount DESC, ao.client_id ASC
LIMIT 1
Database: financial
Question: For account 532, what is the crime trend and ranking of its district, including the percentage change in crimes between 1995 and 1996, how many loans and transactions the account has, and how it compares to the average crime rate across all districts?
Evidence: A15 refers to number of committed crimes in 1995; A16 refers to number of committed crimes in 1996; status = 'B' means bad loan
SQL:
WITH CrimesByDistrict AS (
    SELECT 
        d.district_id,
        d.A2 AS district_name,
        d.A15 AS crimes_1995,
        d.A16 AS crimes_1996,
        CASE 
            WHEN d.A16 > d.A15 THEN 'Increased'
            WHEN d.A16 < d.A15 THEN 'Decreased'
            ELSE 'Unchanged'
        END AS crime_trend,
        ROUND((d.A16 - d.A15) * 100.0 / d.A15, 2) AS percentage_change,
        RANK() OVER (ORDER BY d.A15 DESC) AS crime_rank_1995,
        RANK() OVER (ORDER BY d.A16 DESC) AS crime_rank_1996
    FROM district d
),
AccountInfo AS (
    SELECT 
        a.account_id,
        a.district_id,
        a.frequency,
        COUNT(DISTINCT l.loan_id) AS loan_count,
        COUNT(DISTINCT t.trans_id) AS transaction_count,
        SUM(CASE WHEN l.status = 'B' THEN 1 ELSE 0 END) AS bad_loans,
        COUNT(DISTINCT c.client_id) AS client_count
    FROM account a
    LEFT JOIN loan l ON a.account_id = l.account_id
    LEFT JOIN trans t ON a.account_id = t.account_id
    LEFT JOIN disp d ON a.account_id = d.account_id
    LEFT JOIN client c ON d.client_id = c.client_id
    WHERE a.account_id = 532
    GROUP BY a.account_id, a.district_id, a.frequency
)
SELECT 
    cd.district_name,
    cd.crimes_1995,
    cd.crimes_1996,
    cd.crime_trend,
    cd.percentage_change,
    cd.crime_rank_1995,
    cd.crime_rank_1996,
    ai.loan_count,
    ai.transaction_count,
    ai.bad_loans,
    ai.client_count,
    (SELECT AVG(d2.A15) FROM district d2) AS avg_crimes_across_districts,
    (SELECT COUNT(*) FROM district WHERE A15 > cd.crimes_1995) AS districts_with_more_crimes
FROM CrimesByDistrict cd
JOIN AccountInfo ai ON cd.district_id = ai.district_id
WHERE EXISTS (
    SELECT 1 
    FROM account a 
    WHERE a.account_id = 532 
    AND a.district_id = cd.district_id
)
Database: financial
Question: For the account that placed order 33333, provide a comprehensive profile including the district's economic ranking by salary, account transaction history, ownership details, loan status, and order information.
Evidence: PRIJEM refers to incoming transactions (credits), VYDAJ refers to outgoing transactions (debits). Loan status A, B, or C indicates active loans.
SQL:
WITH OrderInfo AS (
    SELECT 
        T1.order_id,
        T1.account_id,
        T1.amount AS order_amount,
        T1.k_symbol AS order_purpose,
        T2.district_id,
        T2.date AS account_creation_date
    FROM `order` AS T1
    INNER JOIN account AS T2 ON T1.account_id = T2.account_id
    WHERE T1.order_id = 33333
),
DistrictDetails AS (
    SELECT 
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        d.A4 AS inhabitants,
        d.A11 AS avg_salary,
        d.A12 AS unemployment_rate_95,
        RANK() OVER (ORDER BY d.A11 DESC) AS salary_rank
    FROM district d
),
AccountTransactions AS (
    SELECT 
        t.account_id,
        COUNT(*) 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
    WHERE t.account_id IN (SELECT account_id FROM OrderInfo)
    GROUP BY t.account_id
),
AccountOwners AS (
    SELECT 
        d.account_id,
        COUNT(DISTINCT d.client_id) AS owner_count,
        GROUP_CONCAT(DISTINCT c.gender) AS owner_genders
    FROM disp d
    JOIN client c ON d.client_id = c.client_id
    WHERE d.type = 'OWNER'
    GROUP BY d.account_id
)

SELECT 
    oi.district_id,
    dd.district_name,
    dd.region,
    dd.inhabitants,
    dd.avg_salary,
    dd.unemployment_rate_95,
    dd.salary_rank AS district_salary_rank,
    oi.account_creation_date,
    ROUND(JULIANDAY('now') - JULIANDAY(oi.account_creation_date)) AS account_age_days,
    at.transaction_count,
    at.total_income,
    at.total_expense,
    at.total_income - at.total_expense AS account_balance_change,
    at.last_transaction_date,
    ao.owner_count,
    ao.owner_genders,
    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,
    oi.order_amount,
    oi.order_purpose
FROM OrderInfo oi
LEFT JOIN DistrictDetails dd ON oi.district_id = dd.district_id
LEFT JOIN AccountTransactions at ON oi.account_id = at.account_id
LEFT JOIN AccountOwners ao ON oi.account_id = ao.account_id
LEFT JOIN loan l ON oi.account_id = l.account_id AND l.status IN ('A', 'B', 'C')
ORDER BY oi.district_id;
Database: financial
Question: List all the withdrawals in cash transactions that the client with the id 3356 makes.
Evidence: operation = 'VYBER' refers to withdrawal in cash
SQL:
SELECT T4.trans_id 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 = 3356 AND T4.operation = 'VYBER'
Database: financial
Question: Among the weekly issuance accounts, how many have a loan of under 200000?
Evidence: frequency = 'POPLATEK TYDNE' stands for weekly issuance
SQL:
SELECT COUNT(T1.account_id)
FROM loan AS T1
INNER JOIN account AS T2 ON T1.account_id = T2.account_id
WHERE T2.frequency = 'POPLATEK TYDNE' AND T1.amount < 200000;
Database: financial
Question: What is the complete customer profile for client 13539, including their demographics, credit card details, district information with salary ranking, transaction activity, customer segment classification, and loan count?
Evidence: Customer segment is classified as 'Premium' for gold card holders, 'Long-term Classic' for classic card holders with cards older than 5 years, and 'Standard' for all others. PRIJEM refers to income transactions and VYDAJ refers to expense transactions.
SQL:
WITH client_account_info AS (
    SELECT
        d.client_id,
        d.account_id,
        d.disp_id,
        c.gender,
        c.birth_date,
        CAST(strftime('%Y', 'now') AS INTEGER) - CAST(strftime('%Y', c.birth_date) AS INTEGER) AS client_age,
        a.district_id,
        a.frequency
    FROM disp d
    JOIN client c ON d.client_id = c.client_id
    JOIN account a ON d.account_id = a.account_id
    WHERE d.client_id = 13539
),
card_details AS (
    SELECT
        c.disp_id,
        c.type AS card_type,
        c.issued,
        CAST(strftime('%Y', 'now') AS INTEGER) - CAST(strftime('%Y', c.issued) AS INTEGER) AS card_age,
        ROW_NUMBER() OVER (PARTITION BY c.disp_id ORDER BY c.issued DESC) AS card_rank
    FROM card c
    JOIN client_account_info cai ON c.disp_id = cai.disp_id
),
district_stats AS (
    SELECT
        d.district_id,
        d.A2 AS district_name,
        d.A3 AS region,
        d.A11 AS avg_salary,
        RANK() OVER (ORDER BY d.A11 DESC) AS salary_rank
    FROM district d
    JOIN client_account_info cai ON d.district_id = cai.district_id
),
transaction_summary 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 client_account_info cai ON t.account_id = cai.account_id
    GROUP BY t.account_id
)

SELECT 
    cai.client_id,
    cai.gender,
    cai.client_age,
    cd.card_type,
    cd.issued AS card_issued_date,
    cd.card_age,
    ds.district_name,
    ds.region,
    ds.avg_salary,
    ds.salary_rank AS district_salary_rank,
    COALESCE(ts.transaction_count, 0) AS total_transactions,
    COALESCE(ts.total_income, 0) AS total_income,
    COALESCE(ts.total_expense, 0) AS total_expense,
    CASE 
        WHEN cd.card_type = 'gold' THEN 'Premium'
        WHEN cd.card_type = 'classic' AND cd.card_age > 5 THEN 'Long-term Classic'
        ELSE 'Standard'
    END AS customer_segment,
    (SELECT COUNT(*) FROM loan l WHERE l.account_id = cai.account_id) AS loan_count
FROM client_account_info cai
LEFT JOIN card_details cd ON cai.disp_id = cd.disp_id AND cd.card_rank = 1
LEFT JOIN district_stats ds ON cai.district_id = ds.district_id
LEFT JOIN transaction_summary ts ON cai.account_id = ts.account_id
WHERE cai.client_id = 13539
ORDER BY cd.card_age DESC