question_id
int64
0
499
db_id
stringclasses
11 values
question
stringlengths
54
1.57k
evidence
stringlengths
0
418
SQL
stringlengths
41
2.22k
difficulty
stringclasses
3 values
200
financial
For each account-disposition type other than OWNER found in districts with average salary above 8000 and at most 9000, give the type and the number of distinct associated accounts.
SELECT d.type, COUNT(DISTINCT a.account_id) FROM district AS x JOIN account AS a ON a.district_id = x.district_id JOIN disp AS d ON d.account_id = a.account_id WHERE d.type <> 'OWNER' AND x.A11 > 8000 AND x.A11 <= 9000 GROUP BY d.type ORDER BY d.type
challenging
201
financial
For loans dated in 1996 and 1997 across accounts owned by male clients, what was the percentage growth in total loan amount from 1996 to 1997?
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.ac...
challenging
202
financial
How often does account 3 receive statements, and what is the recorded payment-purpose code for its scheduled payments totaling 3,539?
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
challenging
203
financial
We're reviewing our first month of operations. Looking at the districts where we opened at least one account during that first month, which district had the largest population, and how many inhabitants does it have? If several districts tie for the largest population, list all of them with their populations.
The bank's first month of operation is the calendar month in which its earliest account was opened.
WITH first_month AS (SELECT strftime('%Y-%m', MIN(date)) AS ym FROM account), first_month_districts AS (SELECT DISTINCT district_id FROM account WHERE strftime('%Y-%m', date) = (SELECT ym FROM first_month)), cohort AS (SELECT d.A2 AS district_name, CAST(d.A4 AS INTEGER) AS inhabitants FROM district d JOIN first_month_d...
challenging
204
financial
As part of a data-quality review for a segment report, I need to know how much of the recorded transaction activity for one of our segments has no payment purpose noted. How many transactions are there, over the whole recorded history, on the accounts held by male customers born between 1974 and 1976 (birth years 1974,...
Payment purpose: the stated reason or note recorded for a transaction.
SELECT COUNT(*) AS txn_count FROM trans AS t WHERE (t.k_symbol IS NULL OR TRIM(t.k_symbol) = '') AND t.account_id IN (SELECT d.account_id FROM disp AS d INNER JOIN client AS c ON d.client_id = c.client_id WHERE c.gender = 'M' AND STRFTIME('%Y', c.birth_date) BETWEEN '1974' AND '1976')
challenging
205
financial
Among the bank's five most populous districts (the five with the largest numbers of inhabitants), which carries the heaviest active-lending burden relative to the account base it serves? For each of those five districts, please give me the district name and the average active loan per account, rounded to the nearest wh...
WITH populous AS (SELECT district_id FROM district ORDER BY CAST(A4 AS INTEGER) DESC LIMIT 5), per_district AS (SELECT d.A2 AS district_name, SUM(CASE WHEN l.status IN ('C','D') THEN l.amount ELSE 0 END) * 1.0 / COUNT(a.account_id) AS raw_avg FROM district d JOIN account a ON a.district_id = d.district_id LEFT JOIN loa...
challenging
206
financial
For our 1995 year-end loan review: which of the bank's regions has the largest total approved amount among loans that were still being repaid on 31 December 1995, and what is that total? Only count loans approved on or before that day whose repayment term still covers that day; loans approved after it don't count yet. ...
A loan is repaid over a term that starts on its approval day and lasts the number of months recorded for the loan; the loan is being repaid on every day of its term. Status 'C' means a running contract, OK so far; status 'D' means a running contract, client in debt. The regions are the bank's district regions.
WITH ends AS (SELECT d.A3 AS region, l.amount, date(CAST(CAST(substr(l.date, 1, 4) AS INTEGER) + (CAST(substr(l.date, 6, 2) AS INTEGER) - 1 + l.duration) / 12 AS TEXT) || '-' || substr('0' || ((CAST(substr(l.date, 6, 2) AS INTEGER) - 1 + l.duration) % 12 + 1), -2) || '-' || substr(l.date, 9, 2)) AS end_date FROM loan l...
challenging
207
financial
I'd like to know how many customer accounts based in North Bohemia as of the end of 1995 have had at least one transaction with partner bank AB at some point in their history. Please give me just that count as a single integer.
'End of 1995' means the state on 31 December 1995, and the AB transaction may date from any time, before or after that date; 'AB' is the code identifying the partner bank of the transaction.
SELECT COUNT(DISTINCT a.account_id) AS qualifying_accounts FROM district AS d JOIN account AS a ON d.district_id = a.district_id JOIN trans AS t ON a.account_id = t.account_id WHERE d.A3 = 'north Bohemia' AND a.date <= '1995-12-31' AND t.bank = 'AB'
challenging
208
financial
I'm profiling our East Bohemia branch districts that serve younger clients for a youth-product campaign. A district counts as serving a client when the client holds an account at a branch located in that district - it's the branch location that matters, not the client's home district. For the East Bohemia districts tha...
East Bohemia refers to the districts in the East Bohemia region of the Czech Republic.
SELECT ROUND(AVG(x.A11), 2) AS avg_salary, COUNT(*) AS district_count FROM (SELECT DISTINCT T1.district_id, T1.A11 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 INNER JOIN client AS T4 ON T3.client_id = T4.client_id WHERE T1.A3 = '...
challenging
209
financial
I'm planning a card-offer mailing and want to gauge how affluent the home districts of certain clients are. Please give me one number: the average, over the districts where these clients live, of each district's average salary, rounded to two decimal places. The clients are account owners in central Bohemia who hold no...
OWNER is the disposition type marking an account's owner; DISPONENT marks other account users.
WITH qualified AS (SELECT DISTINCT c.district_id FROM disp d JOIN client c ON c.client_id = d.client_id JOIN district dist ON c.district_id = dist.district_id WHERE d.type = 'OWNER' AND dist.A3 = 'central Bohemia' AND NOT EXISTS (SELECT 1 FROM card ca JOIN disp cd ON ca.disp_id = cd.disp_id WHERE cd.account_id = d.acco...
challenging
210
financial
I'm preparing for a client review and want to gauge how indebted our older customers' accounts are, including accounts that never took any loans. Could you give me the average number of loans per account among accounts whose owner was born before 1950? By 'born before 1950' I mean a birth date earlier than 1 January 19...
The owner of an account is the client with the OWNER disposition; a DISPONENT is a co-holder who does not own it. A loan is a credit granted on the account, recorded with the date it was approved.
WITH owner AS (SELECT T2.account_id, T1.birth_date FROM client T1 INNER JOIN disp T2 ON T1.client_id = T2.client_id WHERE T2.type = 'OWNER') SELECT ROUND(CAST(COUNT(T3.loan_id) AS REAL) / COUNT(DISTINCT T4.account_id), 2) AS average_loans_per_account FROM owner T4 LEFT JOIN loan T3 ON T4.account_id = T3.account_id WHER...
challenging
211
financial
I'm putting together a periodic report on large payments made by a specific customer cohort, split by what the payment was for. Among payments over $4000 (strictly more than $4000) made from the accounts of male customers born between 1974 and 1976, I need two figures: how many were household payments, and how many wer...
SIPO stands for household payment.
SELECT COUNT(DISTINCT CASE WHEN t.k_symbol = 'SIPO' THEN t.trans_id END) AS household_payments, COUNT(DISTINCT CASE WHEN t.k_symbol IS NOT NULL AND TRIM(t.k_symbol) <> '' AND t.k_symbol <> 'SIPO' THEN t.trans_id END) AS other_purpose_payments FROM trans AS t JOIN disp AS d ON d.account_id = t.account_id JOIN client AS ...
challenging
212
financial
I want to find out whether our borrowing households from larger population centres transact differently from local ones. For every account at the Prachatice branch that has taken at least one loan, please give me: the account number, the account opening date, the total amount ever borrowed, how many outside bank counte...
An outside bank counterparty is a bank account at another institution that money was sent to or received from on a transfer transaction. District population is the number of inhabitants of the district.
WITH borrowers AS (SELECT a.account_id, a.date AS opening_date, (SELECT SUM(l.amount) FROM loan l WHERE l.account_id = a.account_id) AS total_borrowed FROM account a WHERE a.district_id = (SELECT district_id FROM district WHERE A2 = 'Prachatice') AND a.account_id IN (SELECT account_id FROM loan)), owners AS (SELECT d.a...
challenging
213
financial
I'm profiling our gold-card customers for a regional campaign, split by gender. For women first, then men, look at gold-card holders whose card's account has an average balance over 1,000. Among the branch districts holding those accounts, keep only the districts with over 100,000 inhabitants. For each gender, tell me:...
A gold card is a card of type 'gold', and its holder is the client linked to its account as type 'OWNER'. The district of an account is the district of the branch that holds it; a district's inhabitants, salary and urban-inhabitant ratio are the district's own figures.
WITH gold_holder AS (SELECT d.account_id, cl.gender FROM card c JOIN disp d ON c.disp_id = d.disp_id JOIN client cl ON d.client_id = cl.client_id WHERE c.type = 'gold' AND d.type = 'OWNER'), acct_bal AS (SELECT account_id, AVG(balance) AS avg_balance FROM trans GROUP BY account_id), holder_district AS (SELECT DISTINCT ...
challenging
214
financial
I'm preparing a partner-bank negotiation and want to focus on our big-city clients. Please tell me, for 1998 (1 January through 31 December 1998), where the money went from gold credit card clients' accounts held at branches in large districts. Specifically: for accounts whose owner has a gold credit card, and whose br...
Gold credit card means card type 'gold'; account owner means disposition type 'OWNER'; the counter-account is the partner account at another bank to which a payment is sent; the district is the one where the account's branch is located.
WITH gold_accounts AS (SELECT d.account_id FROM card c JOIN disp d ON c.disp_id = d.disp_id WHERE c.type = 'gold' AND d.type = 'OWNER'), outgoing AS (SELECT t.bank AS bank, t.account AS account, COUNT(*) AS n_payments, SUM(t.amount) AS total_amount FROM trans t JOIN account a ON a.account_id = t.account_id JOIN distric...
challenging
215
financial
I'm reviewing what the money leaving our Pisek district branch's accounts is actually used for. What percentage of the total amount of outgoing payments that state a payment purpose went to household bill payments (SIPO)? Please give me a single percentage value, rounded to two decimals. Note this should be based only ...
VYDAJ marks a transaction where money leaves the account. SIPO is the code for household bill payments. A payment states its purpose through its transaction characterization.
SELECT ROUND(SUM(CASE WHEN t.k_symbol = 'SIPO' THEN t.amount ELSE 0 END) * 100.0 / SUM(t.amount), 2) AS sipo_percentage_of_purposed_outgoing_payments FROM trans t JOIN account a ON t.account_id = a.account_id JOIN district d ON a.district_id = d.district_id WHERE d.A2 = 'Pisek' AND t.type = 'VYDAJ' AND t.k_symbol IS NO...
challenging
216
financial
I'm planning a regional campaign for female customers who hold a credit card and a loan, and I need a demographic profile per region. For each region, please give me: the region, the average district salary of the districts where these female clients live, and the average 1996 district unemployment rate of those same d...
Disposition type OWNER means the client owns the account. Unemployment rate in 1996 is the district's unemployment rate for 1996; average salary is the district's average salary.
WITH cohort AS ( SELECT DISTINCT c.district_id FROM client c JOIN disp d ON d.client_id = c.client_id AND d.type = 'OWNER' WHERE c.gender = 'F' AND d.disp_id IN (SELECT disp_id FROM card) AND d.account_id IN (SELECT account_id FROM loan) ) SELECT di.A3 AS region, ROUND(AVG(di.A11), 2) AS average_district_salary, ROUND(...
challenging
217
financial
I'm reviewing our presence in central Bohemia. Please tell me: the names of the three most populous districts of central Bohemia (largest by inhabitants), the number of crimes committed in 1996 in each of them, the average 1996 crime count across those three districts, the average 1996 crime count across all districts ...
Central Bohemia is the region of the Czech Republic surrounding Prague.
WITH ranked AS ( SELECT d.A2 AS district_name, CAST(d.A4 AS INTEGER) AS inhabitants, d.A16 AS crimes_1996 FROM district d WHERE d.A3 = 'central Bohemia' ORDER BY CAST(d.A4 AS INTEGER) DESC, d.A2 LIMIT 3 ) SELECT r.district_name, r.crimes_1996, ROUND((SELECT AVG(crimes_1996) FROM ranked), 2) AS avg_crimes_top3_1996, ROU...
challenging
218
financial
For planning region-targeted card offers, I need to look at the smallest markets behind each credit card type. For each credit card type (gold, classic and junior), which home district of the clients holding that type has the fewest inhabitants, and how many inhabitants does that district have? Please include all three...
By "home district" I mean the district a client lives in, and a district's inhabitants are its number of inhabitants.
WITH holder_districts AS (SELECT DISTINCT cd.type AS card_type, cl.district_id FROM card cd JOIN disp dp ON dp.disp_id = cd.disp_id JOIN client cl ON cl.client_id = dp.client_id), ranked AS (SELECT hd.card_type, ds.A2 AS district_name, CAST(ds.A4 AS INTEGER) AS inhabitants, RANK() OVER (PARTITION BY hd.card_type ORDER ...
challenging
219
financial
I'm reviewing how our branches performed in 1996, and I'd like a picture of the money standing in accounts opened during that year, across the region that Litomerice belongs to (the region as given in the district table's region field). For each district in that region where at least one account was opened in 1996 (op...
Balance means the balance after a transaction.
WITH region AS (SELECT A3 AS r FROM district WHERE A2 = 'Litomerice'), op AS (SELECT a.account_id, a.district_id FROM account a JOIN district d ON d.district_id = a.district_id WHERE d.A3 = (SELECT r FROM region) AND a.date >= '1996-01-01' AND a.date < '1997-01-01'), lastd AS (SELECT t.account_id, MAX(t.date) AS md FRO...
challenging
220
financial
I want to check whether our biggest short-term loans cluster in populous branch districts and how well-off those areas are. Looking at 24-month loans on accounts with weekly statement issuance, take the ten largest loans ranked by approved amount from highest to lowest — if two loans have the same amount, the one with ...
Weekly statement issuance means account issuance frequency 'POPLATEK TYDNE'. A district's inhabitants are its number of inhabitants, and its average salary is its average yearly salary.
WITH ranked AS ( SELECT l.loan_id, a.district_id, ROW_NUMBER() OVER (ORDER BY l.amount DESC, l.loan_id ASC) AS rn FROM loan AS l JOIN account AS a ON a.account_id = l.account_id WHERE l.duration = 24 AND a.frequency = 'POPLATEK TYDNE' ), top10 AS ( SELECT * FROM ranked WHERE rn <= 10 ), districts AS ( SELECT d.district...
challenging
221
financial
For every district with at least 100,000 inhabitants that has withdrawal transactions, can you show me the district name, region, population, total amount withdrawn in the district, withdrawals per inhabitant (rounded to 2 decimals), the district's rank by withdrawals per inhabitant (highest first), and the average sta...
Withdrawal transactions are those recorded with type = 'VYDAJ'; a standing order is a recurring payment order that debits the account.
WITH wd AS (SELECT a.district_id, SUM(t.amount) AS total FROM trans t JOIN account a ON t.account_id = a.account_id WHERE t.type = 'VYDAJ' GROUP BY a.district_id), acct AS (SELECT district_id, COUNT(*) AS n_accounts FROM account GROUP BY district_id), so AS (SELECT a.district_id, SUM(o.amount) AS so_total FROM account ...
challenging
222
financial
I'm preparing a client review for client 3541. For each account this client can access, I need one row with: the account id, the date the account was opened, its frequency, the name and region of the district where the account is held, whether that district is the client's home district (Yes/No), that district's averag...
PRIJEM = incoming transaction; VYDAJ = outgoing transaction; net balance = total income - total expense
WITH acct AS (SELECT d.account_id, a.date AS opened, a.frequency AS freq, a.district_id AS branch_district, c.district_id AS home_district FROM client c JOIN disp d ON d.client_id = c.client_id JOIN account a ON a.account_id = d.account_id WHERE c.client_id = 3541), stats AS (SELECT ac.account_id, ac.opened, ac.freq, a...
challenging
223
financial
I'm doing a lending review by account-opening year for my region. For accounts opened in the central Bohemia region from 1 January 1996 onward, broken down by the calendar year of the account creation date (as a text string like '1996', not an integer), I need: the number of accounts opened that year in the region, the...
Central Bohemia is the region value recorded for districts in the region data; the amount lent on an account is the total of the loans granted on that account.
WITH cba AS ( SELECT a.account_id, a.district_id, STRFTIME('%Y', a.date) AS y FROM account a INNER JOIN district d ON a.district_id = d.district_id WHERE d.A3 = 'central Bohemia' AND a.date >= '1996-01-01' ), branch AS ( SELECT cba.y, cba.district_id, SUM(COALESCE(l.amount, 0)) * 1.0 / COUNT(DISTINCT cba.account_id) AS...
challenging
224
financial
For a regional risk review, I need to look at our largest 24-month loans. Please find the ten loans with the largest approved amount among loans with a 24-month duration on accounts that have weekly statement issuance. For each of these ten loans, give me: the district of the branch servicing the account, the approved ...
Weekly statement issuance means statement frequency is 'POPLATEK TYDNE'; loan duration is stated in months.
WITH top_loans AS (SELECT l.loan_id, l.account_id, l.amount, l.date AS loan_date, d.A2 AS district_name FROM loan l JOIN account a ON a.account_id = l.account_id JOIN district d ON d.district_id = a.district_id WHERE l.duration = 24 AND a.frequency = 'POPLATEK TYDNE' ORDER BY l.amount DESC, l.date ASC, l.loan_id ASC LI...
challenging
225
financial
I'm putting together a review of our first credit card wave and need a profile of every client who was issued a credit card in 1994 (issue date falling in calendar year 1994). For each such client, please include: client id, gender, birth date, card type, age at card issue (the card's issue year minus the client's birt...
WITH cohort AS ( SELECT c.client_id, c.gender, c.birth_date, cd.type AS card_type, (CAST(STRFTIME('%Y', cd.issued) AS INTEGER) - CAST(STRFTIME('%Y', c.birth_date) AS INTEGER)) AS age_at_issue, d.account_id, di.A2 AS district_name, di.A11 AS district_avg_salary FROM card cd JOIN disp d ON cd.disp_id = d.disp_id JOIN cli...
challenging
226
financial
I'm reviewing branches in the north Moravia region ahead of a client meeting. For each branch there, how many transactions dated on or before 14 October 1998 have no characterization recorded? A branch is the district the account belongs to, and the count should cover all of the branch's accounts' transactions. Please ...
A transaction with no characterization recorded means the bank did not tag it with any characterization.
WITH branch_counts AS ( SELECT d.A2 AS branch_name, COUNT(*) AS unstated_transactions FROM trans t JOIN account a ON t.account_id = a.account_id JOIN district d ON a.district_id = d.district_id WHERE d.A3 = 'north Moravia' AND t.date <= '1998-10-14' AND (t.k_symbol IS NULL OR TRIM(t.k_symbol) = '') GROUP BY d.A2 ) SELE...
challenging
227
financial
The loan of 98832 approved on January 3rd, 1996 has one applicant account. For that account, list every counterparty it had paid before January 3rd, 1996. A counterparty means a specific account number at a specific bank, so give me one row per bank code and account number combination. For each counterparty I need the ...
VYDAJ denotes an outgoing payment (debit); PRIJEM denotes an incoming payment (credit).
SELECT t.bank AS bank_code, t.account AS counterparty_account, COUNT(*) AS payment_count, SUM(t.amount) AS total_paid FROM trans t WHERE t.account_id = (SELECT l.account_id FROM loan l WHERE l.date = '1996-01-03' AND l.amount = 98832) AND t.type = 'VYDAJ' AND t.date < '1996-01-03' AND t.account > 0 GROUP BY t.bank, t.a...
challenging
228
financial
For the year-end review of my branch at location 1: how many accounts that were on the branch's books as of 31 December 1995 hold an active loan contract? I want a single integer, nothing else. An account counts only if it belonged to the branch on that date, and only contracts that are still active by their repayment ...
Loan status C means an active contract in good standing; loan status D means an active contract with the client in debt. Both are active contracts.
SELECT COUNT(DISTINCT a.account_id) AS account_count FROM account AS a JOIN loan AS l ON l.account_id = a.account_id WHERE a.district_id = 1 AND a.date <= '1995-12-31' AND l.status IN ('C', 'D')
challenging
229
financial
I'm closing the 1998 books for client 2823's account and need to reconcile the interest. What was the net interest on that account for calendar year 1998 (1 January through 31 December 1998) – interest credited minus penalty interest charged, with penalty interest entering as negative – as a single exact amount in CZK,...
'UROK' is the bank's code for interest credited to an account; penalty (sanction) interest charged to an account uses the bank's sanction-interest code, which is built on the same interest code.
SELECT SUM(CASE WHEN t.k_symbol = 'UROK' THEN t.amount ELSE -t.amount END) AS net_interest FROM trans t WHERE t.date BETWEEN '1998-01-01' AND '1998-12-31' AND t.k_symbol LIKE '%UROK%' AND EXISTS (SELECT 1 FROM disp d WHERE d.account_id = t.account_id AND d.client_id = 2823)
challenging
230
financial
I need to profile our three largest loans held by female clients. Please report for each of these loans: the client id, loan amount, duration in months, total payments, interest paid, loan status, the district and region of the branch where the loan's account is held, the account's transaction count and total deposits,...
Female means client gender = 'F'. Deposits are transactions recorded as PRIJEM (credits to the account). District average salary is the district's average salary figure.
WITH female_loans AS (SELECT c.client_id, c.birth_date, d.account_id, l.loan_id, l.amount, l.duration, l.payments, l.status, a.district_id, RANK() OVER (ORDER BY l.amount DESC) AS rnk 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...
challenging
231
financial
I need to find out where our pre-1997 long-term mortgage exposure is concentrated geographically. Long-term means a loan duration of more than 24 months, and "opened before 1997" should exclude anything opened during 1997. Please tell me: for each region, which districts have at least one such account, and give me the ...
WITH cohort AS (SELECT l.account_id, a.district_id FROM loan AS l JOIN account AS a ON l.account_id = a.account_id WHERE l.duration > 24 AND STRFTIME('%Y', a.date) < '1997'), region_districts AS (SELECT DISTINCT d.district_id, d.A3 AS region, CAST(d.A4 AS INTEGER) AS population FROM district AS d JOIN cohort AS c ON d....
challenging
232
financial
I'm putting together a market-coverage report on how our 1997 rollout of monthly-statement accounts reached large urban districts. For districts with at least 100,000 inhabitants that had at least one monthly-statement account opened during calendar year 1997, please give me: the total number of monthly-statement accou...
Monthly statement issuance means the account's statement frequency is 'POPLATEK MESICNE'.
WITH qualifying_districts AS (SELECT d.district_id, d.A11 FROM district d WHERE CAST(d.A4 AS INTEGER) >= 100000 AND EXISTS (SELECT 1 FROM account a WHERE a.district_id = d.district_id AND a.frequency = 'POPLATEK MESICNE' AND a.date BETWEEN '1997-01-01' AND '1997-12-31')) SELECT (SELECT COUNT(*) FROM account a JOIN qual...
challenging
233
financial
I'd like to review the breadth of each gold-card account's external payment network. For every account that has a gold credit card, could you list: the account id, the district name and region of the branch, the total amount received, the total amount withdrawn, the number of loans taken, and the number of accounts at ...
An external counterparty account is an account at another bank that money was transferred to or received from. Amount received refers to credit transactions, which carry transaction type PRIJEM. Amount withdrawn refers to debit transactions, which carry transaction type VYDAJ.
WITH gold_accounts AS (SELECT d.account_id FROM disp d JOIN card c ON c.disp_id = d.disp_id WHERE c.type = 'gold' GROUP BY d.account_id), trans_stats AS (SELECT account_id, SUM(CASE WHEN type = 'PRIJEM' THEN amount ELSE 0 END) AS total_received, SUM(CASE WHEN type = 'VYDAJ' THEN amount ELSE 0 END) AS total_withdrawn, C...
challenging
234
financial
I'm preparing our branch's annual customer review. For every account held at the Tabor branch as of 31 December 1996, I need a one-row profile per account, ordered among tied ranks by ascending account id, showing: the account id; the date the account was opened; the owner's gender and their age at 31 December 1996 in ...
Loan repayment status codes: A means the contract finished and was paid off, B means the loan was not repaid (default), C means the loan is running and being repaid, D means the loan is running and the client is in debt on it. Transaction types: a credit transaction brings money into the account, a debit transaction ta...
WITH cohort AS (SELECT a.account_id, a.date AS open_date FROM account a JOIN district d ON d.district_id = a.district_id WHERE d.A2 = 'Tabor' AND a.date <= '1996-12-31'), own AS (SELECT co.account_id, co.open_date, c.gender, c.birth_date FROM cohort co JOIN disp dp ON dp.account_id = co.account_id AND dp.type = 'OWNER'...
challenging
235
financial
I'd like a district-by-district picture of where our classic-card-holding account owners send money outside the bank. For every district, give me: the district name, the number of clients who own an account and hold a classic credit card, the number of external accounts at other banks those clients paid through outgoin...
An account owner is the client recorded as 'OWNER' on the account. An outgoing payment is a transaction recorded with type 'VYDAJ' (money paid out of the account). An external account is an account held at another bank to which the account holder sends money; the receiving bank together with the receiving account numbe...
WITH holders AS ( SELECT dp.client_id, dp.account_id, a.district_id FROM card c JOIN disp dp ON dp.disp_id = c.disp_id AND dp.type = 'OWNER' JOIN account a ON a.account_id = dp.account_id WHERE c.type = 'classic' ) SELECT d.A2 AS district, COUNT(DISTINCT h.client_id) AS classic_card_holders, COUNT(DISTINCT t.account) A...
challenging
236
financial
I'm planning services for our older female customers. For the clients who are female and were born before 1930, please look at the branches that hold their accounts, and then the districts those branches are located in. What is the average salary of those districts? Round the result to 2 decimal places. Note: born befo...
Average salary refers to the district attribute describing average salary.
SELECT ROUND(AVG(d.A11), 2) AS average_salary FROM district AS d WHERE d.district_id IN (SELECT a.district_id FROM account AS a JOIN disp AS x ON x.account_id = a.account_id JOIN client AS c ON c.client_id = x.client_id WHERE c.gender = 'F' AND c.birth_date < '1930-01-01')
challenging
237
financial
I'm preparing a campaign for senior female clients and want a sense of the economic profile of their home districts. Could you tell me the average of the average salaries of the districts where our female clients born before 1950 live? Please give me just that single figure, rounded to two decimal places.
Average salary means the average yearly salary recorded for a district, in CZK.
SELECT ROUND(AVG(T1.A11), 2) AS average_salary FROM district AS T1 WHERE T1.district_id IN (SELECT T2.district_id FROM client AS T2 WHERE T2.gender = 'F' AND STRFTIME('%Y', T2.birth_date) < '1950')
challenging
238
financial
I want to know whether our small, healthy loans are concentrated in richer or poorer districts before we decide where to grow the small-loan business. Please give me one number: the average district salary, rounded to two decimals, across the districts where the accounts holding these loans are located. The cohort is l...
Loan status C means the contract is running with no problems so far. District average salary is the average salary recorded for a district.
SELECT ROUND(AVG(d.A11), 2) AS average_district_salary FROM district d WHERE d.district_id IN (SELECT a.district_id FROM account a WHERE a.account_id IN (SELECT l.account_id FROM loan l WHERE l.amount < 100000 AND l.status = 'C'))
challenging
239
financial
I'm studying how client age relates to branch location. For each of two client cohorts, I want the cohort label and the average salary across the districts where the branches holding that cohort's accounts are located, rounded to the nearest whole number. A client's cohort is based on birth date: born 1980 or later is ...
WITH cohort_districts AS (SELECT a.district_id, CASE WHEN c.birth_date >= '1980-01-01' THEN 'young' ELSE 'old' END AS cohort FROM client c JOIN disp d ON d.client_id = c.client_id JOIN account a ON a.account_id = d.account_id WHERE c.birth_date >= '1980-01-01' OR c.birth_date <= '1919-12-31' GROUP BY a.district_id, CAS...
challenging
240
formula_1
By what percentage was Paul di Resta's fastest-lap speed in race 853 higher than in race 854? Use the speed in race 854 as the baseline and round to two decimals.
SELECT ROUND(100.0 * (MAX(CASE WHEN r.raceId = 853 THEN CAST(r.fastestLapSpeed AS DECIMAL(12,4)) END) - MAX(CASE WHEN r.raceId = 854 THEN CAST(r.fastestLapSpeed AS DECIMAL(12,4)) END)) / NULLIF(MAX(CASE WHEN r.raceId = 854 THEN CAST(r.fastestLapSpeed AS DECIMAL(12,4)) END), 0), 2) FROM drivers AS d JOIN results AS r ON...
challenging
241
formula_1
What percentage of Lewis Hamilton's recorded race entries from 2010 onward were not victories? Round to two decimals.
SELECT ROUND(100.0 * SUM(CASE WHEN x.positionOrder <> 1 THEN 1 ELSE 0 END) / NULLIF(COUNT(*), 0), 2) FROM results AS x JOIN drivers AS d ON d.driverId = x.driverId JOIN races AS r ON r.raceId = x.raceId WHERE d.forename = 'Lewis' AND d.surname = 'Hamilton' AND r.year >= 2010
challenging
242
formula_1
Which driver won the most recorded races? Give their first name, last name, nationality, and highest recorded driver-standings points total.
WITH wins AS (SELECT driverId, COUNT(*) AS win_count FROM results WHERE positionOrder = 1 GROUP BY driverId), winner AS (SELECT driverId FROM wins ORDER BY win_count DESC, driverId LIMIT 1) SELECT d.forename, d.surname, d.nationality, (SELECT MAX(s.points) FROM driverStandings AS s WHERE s.driverId = d.driverId) FROM w...
moderate
243
formula_1
In the 1988 Australian Grand Prix, by what percentage was the winner's elapsed time shorter than the last full-distance finisher's? Use the last finisher's time as denominator and round to two decimals.
WITH finished AS ( SELECT x.positionOrder, x.milliseconds FROM results AS x JOIN races AS r ON r.raceId = x.raceId JOIN status AS s ON s.statusId = x.statusId WHERE r.name = 'Australian Grand Prix' AND r.year = 1988 AND s.status = 'Finished' AND x.milliseconds IS NOT NULL ), extremes AS ( SELECT MIN(CASE WHEN positionO...
challenging
244
formula_1
What percentage of Japanese-driver race entries from 2007 through 2009 matched their race winner's recorded lap count? Round to two decimals.
SELECT ROUND(100.0 * SUM(CASE WHEN x.laps = (SELECT y.laps FROM results AS y WHERE y.raceId = x.raceId AND y.positionOrder = 1 ORDER BY y.resultId LIMIT 1) THEN 1 ELSE 0 END) / NULLIF(COUNT(*), 0), 2) FROM results AS x JOIN races AS r ON r.raceId = x.raceId JOIN drivers AS d ON d.driverId = x.driverId WHERE d.nationali...
challenging
245
formula_1
For each year before 1975 with recorded winner times, what was the mean race winner elapsed time in seconds? Round the exact mean to two decimal places; if it is exactly halfway, round away from zero. Order by year.
SELECT r.year, FLOOR((SUM(x.milliseconds) + 5 * COUNT(*)) / (10 * COUNT(*))) / 100.0 FROM results AS x JOIN races AS r ON r.raceId = x.raceId WHERE r.year < 1975 AND x.positionOrder = 1 AND x.milliseconds IS NOT NULL GROUP BY r.year ORDER BY r.year
challenging
246
formula_1
From 2000 through 2005, what percentage of race results were by drivers born before 1985 who completed more than 50 laps?
SELECT CAST(SUM(IIF(STRFTIME('%Y', T3.dob) < '1985' AND T1.laps > 50, 1, 0)) AS REAL) * 100 / COUNT(*) FROM results AS T1 INNER JOIN races AS T2 ON T1.raceId = T2.raceId INNER JOIN drivers AS T3 ON T1.driverId = T3.driverId WHERE T2.year BETWEEN 2000 AND 2005
challenging
247
formula_1
Which constructor scored most points from Monaco Grand Prix between 1980 and 2010? List the score, name and nationality of this team.
SELECT SUM(T1.points), T2.name, T2.nationality FROM constructorResults AS T1 INNER JOIN constructors AS T2 ON T1.constructorId = T2.constructorId INNER JOIN races AS T3 ON T3.raceId = T1.raceId WHERE T3.name = 'Monaco Grand Prix' AND T3.year BETWEEN 1980 AND 2010 GROUP BY T2.name ORDER BY SUM(T1.points) DESC LIMIT 1
challenging
248
formula_1
Who recorded the fastest Q3 qualifying time at Marina Bay Street Circuit in 2008? Give the driver's first and last name.
SELECT T2.forename, T2.surname FROM qualifying AS T1 INNER JOIN drivers AS T2 ON T1.driverId = T2.driverId INNER JOIN races AS T3 ON T1.raceId = T3.raceId WHERE q3 IS NOT NULL AND T3.year = 2008 AND T3.circuitId IN ( SELECT circuitId FROM circuits WHERE name = 'Marina Bay Street Circuit' ) ORDER BY CAST(SUBSTR(q3, 1, I...
challenging
249
formula_1
Which 20 drivers recorded the shortest single lap times in Formula 1 races? Give their full names, shortest lap first.
WITH per_driver AS ( SELECT driverId, MIN(milliseconds) AS fastest_ms FROM lapTimes WHERE milliseconds > 0 GROUP BY driverId ) SELECT d.forename || ' ' || d.surname AS full_name FROM per_driver AS p JOIN drivers AS d ON d.driverId = p.driverId ORDER BY p.fastest_ms, d.driverId LIMIT 20
challenging
250
formula_1
For each Formula 1 circuit in Italy with recorded laps, give the circuit name and its fastest recorded lap time in minutes:seconds.milliseconds format.
WITH ranked_laps AS (SELECT c.circuitId, c.name AS circuit_name, l.time AS lap_record, ROW_NUMBER() OVER ( PARTITION BY c.circuitId ORDER BY l.milliseconds, l.raceId, l.driverId, l.lap ) AS rn FROM lapTimes AS l JOIN races AS r ON r.raceId = l.raceId JOIN circuits AS c ON c.circuitId = r.circuitId WHERE c.country = 'It...
challenging
251
formula_1
I'm comparing pit stop activity at the newer Grand Prix weekends. Among the 2017 races that were not hosted in 2000, what was the average number of pit stops per race? Please take the average of all of those races and round it to two decimal places.
Hosted in 2000 means the race was on the 2000 race calendar.
WITH cohort AS (SELECT raceId FROM races WHERE year = 2017 AND name NOT IN (SELECT name FROM races WHERE year = 2000)) SELECT ROUND(1.0 * (SELECT COUNT(*) FROM pitStops WHERE raceId IN (SELECT raceId FROM cohort)) / (SELECT COUNT(*) FROM cohort), 2) AS avg_pit_stops_per_race
challenging
252
formula_1
I'm writing a retrospective on German F1 drivers born between 1971 and 1985, and I'd like to quantify how often each of them reached the final part of qualifying. For every German driver born in 1971 through 1985 inclusive, over their entire career (every race on record), please give me their forename, surname, Wikiped...
Q3 is the third and final part of the qualifying session that sets the starting grid; a driver's Q3 time is the lap time recorded for them in that part.
SELECT d.forename, d.surname, d.url, d.dob, COUNT(NULLIF(q.q3, '')) AS q3_times FROM drivers d LEFT JOIN qualifying q ON q.driverId = d.driverId WHERE d.nationality = 'German' AND STRFTIME('%Y', d.dob) BETWEEN '1971' AND '1985' GROUP BY d.driverId ORDER BY q3_times DESC, d.dob ASC
challenging
253
formula_1
I'm writing a retrospective on the 1995 Japanese Grand Prix and want to highlight how far off the pace the slowest qualifier was. Could you tell me who set the slowest time in the first qualifying session (Q1) of that race? For that driver (or drivers, if two share the same slowest time, please list them all), I'd like...
By 'reference name' I mean the short driver code like 'hamilton'; by 'Q1' I mean the first of the three qualifying sessions; by 'birthday' I mean the driver's date of birth.
WITH t AS (SELECT d.driverRef AS driverRef, d.nationality AS nationality, d.dob AS dob, q.q1 AS q1 FROM qualifying q INNER JOIN races r ON q.raceId = r.raceId INNER JOIN drivers d ON q.driverId = d.driverId WHERE r.name = 'Japanese Grand Prix' AND r.year = 1995 AND q.q1 IS NOT NULL AND q.q1 <> '') SELECT a.driverRef, a...
challenging
254
formula_1
For qualifying at the 1995 Japanese Grand Prix, I need the nationality of the driver who set the slowest time in Q1. If two drivers tie for the slowest Q1 time, give me each of their nationalities. I only want the nationality value(s), nothing else.
WITH t AS (SELECT d.nationality, RANK() OVER (ORDER BY CAST(replace(substr(q.q1, 1, instr(q.q1, ':')), ':', '') AS INTEGER) DESC, CAST(substr(q.q1, instr(q.q1, ':') + 1) AS INTEGER) DESC) AS rnk FROM qualifying q JOIN drivers d ON d.driverId = q.driverId JOIN races r ON r.raceId = q.raceId WHERE r.year = 1995 AND r.nam...
challenging
255
formula_1
Who set the fastest time in Q1 at the 1995 Japanese Grand Prix, and what was that time? Please give me the driver's forename and surname, and the Q1 time. By '1995 Japanese Grand Prix' I mean the race of the 1995 season named 'Japanese Grand Prix'. If two drivers tied for the fastest Q1 time, list them all.
Q1: the time a driver set in the first qualifying session.
SELECT d.forename, d.surname, q.q1 FROM qualifying AS q JOIN drivers AS d ON d.driverId = q.driverId JOIN races AS r ON r.raceId = q.raceId WHERE r.year = 1995 AND r.name = 'Japanese Grand Prix' AND q.q1 IS NOT NULL ORDER BY CAST(SUBSTR(q.q1, 1, INSTR(q.q1, ':') - 1) AS INTEGER) ASC, CAST(SUBSTR(q.q1, INSTR(q.q1, ':') ...
challenging
256
formula_1
For the 2010 Formula 1 season, how many drivers did Lucas di Grassi qualify ahead of? I want a single number: the count of drivers he out-qualified over the whole season. By 'qualify ahead of' I mean recording a better qualifying position than that driver in the same race, with a lower position number being better. The...
qualifying position: the starting slot a driver earns in the qualifying session before a race
SELECT COUNT(DISTINCT T2.driverId) AS drivers_qualified_ahead_of FROM qualifying AS T1 JOIN qualifying AS T2 ON T2.raceId = T1.raceId AND T2.position > T1.position JOIN drivers AS D ON D.driverId = T1.driverId JOIN races AS R ON R.raceId = T1.raceId WHERE D.forename = 'Lucas' AND D.surname = 'di Grassi' AND R.year = 20...
challenging
257
formula_1
I'm writing a recap of the 2017 Italian Grand Prix and want to credit the country of the quickest driver in the second qualifying session. Can you give me the nationality of the driver who set the fastest Q2 lap time in qualifying for the 2017 edition of the Italian Grand Prix? If two or more drivers tied for the faste...
Q2: the second of the knockout qualifying sessions of a Grand Prix weekend.
SELECT T2.nationality FROM qualifying AS T1 INNER JOIN races AS T3 ON T3.raceId = T1.raceId INNER JOIN drivers AS T2 ON T2.driverId = T1.driverId WHERE T3.name = 'Italian Grand Prix' AND T3.year = 2017 AND T1.q2 IS NOT NULL AND T1.q2 <> '' AND T1.q2 = (SELECT MIN(q2) FROM qualifying WHERE raceId = T1.raceId AND q2 IS N...
challenging
258
formula_1
I'm writing an article about the Italian Grand Prix and want to know which first names recur among the drivers who finished second in that race. For every forename carried by two or more runners-up of the Italian Grand Prix across all its editions in the data, please give me one row with the forename and the number of ...
A runner-up is the driver who finished second in an edition of the Italian Grand Prix.
SELECT d.forename AS forename, COUNT(DISTINCT d.driverId) AS runner_ups FROM races AS r INNER JOIN results AS x ON x.raceId = r.raceId INNER JOIN drivers AS d ON d.driverId = x.driverId WHERE r.name = 'Italian Grand Prix' AND x.position = 2 GROUP BY d.forename HAVING COUNT(DISTINCT d.driverId) > 1 ORDER BY runner_ups D...
challenging
259
formula_1
Who won the 2017 Azerbaijan Grand Prix? I'd like the winner's forename and surname, the local clock time (hours:minutes:seconds.milliseconds) at the circuit at which he crossed the finish line, and the url of his information page, so I can compare the moment the race ended with the TV coverage.
A driver's race time is the duration from the start of the race to the moment he crosses the finish line.
SELECT d.forename, d.surname, time(r.time, '0' || res.time) || '.' || substr(res.time, -3) AS finish_time, d.url FROM races r JOIN results res ON res.raceId = r.raceId JOIN drivers d ON d.driverId = res.driverId WHERE r.year = 2017 AND r.name = 'Azerbaijan Grand Prix' AND res.positionOrder = 1
challenging
260
formula_1
For the 2017 Italian Grand Prix, I'd like to identify the drivers who completed the second qualifying session (Q2) but were knocked out and did not reach Q3. Please list every such driver - no ranking, cutoff or tie-break, just all of them - showing each driver's reference name, nationality and birthday, and nothing el...
Formula 1 qualifying is run in three timed sessions, Q1, Q2 and Q3; completing a session means the driver recorded a lap time in that session, and only drivers who complete a session move on to the next. The reference name is the driver's short identifier.
SELECT T2.driverRef, T2.nationality, T2.dob FROM qualifying AS T1 INNER JOIN drivers AS T2 ON T1.driverId = T2.driverId INNER JOIN races AS R ON T1.raceId = R.raceId WHERE R.year = 2017 AND R.name = 'Italian Grand Prix' AND T1.q2 IS NOT NULL AND T1.q2 != '' AND (T1.q3 IS NULL OR T1.q3 = '')
challenging
261
formula_1
For the 2017 Italian Grand Prix qualifying, what is the nationality of the driver with the second fastest Q2 time? Only drivers who took part in Q2 count, and by second fastest I mean the second smallest Q2 time. If several drivers share that time, list each of their nationalities.
Q2 is the second qualifying session of a Grand Prix weekend, after Q1.
WITH q2times AS (SELECT d.nationality, q.q2 FROM qualifying q JOIN drivers d ON d.driverId = q.driverId JOIN races r ON r.raceId = q.raceId WHERE r.name = 'Italian Grand Prix' AND r.year = 2017 AND q.q2 IS NOT NULL AND q.q2 <> '') SELECT DISTINCT T.nationality FROM q2times T WHERE T.q2 = (SELECT MIN(q2) FROM q2times WH...
challenging
262
formula_1
For qualifying at the 2017 Italian Grand Prix, how many drivers set a time in the second qualifying session (Q2) faster than 1:40.000? I just need the count of drivers as a single number.
SELECT COUNT(DISTINCT q.driverId) AS driver_count FROM qualifying AS q JOIN races AS r ON r.raceId = q.raceId WHERE r.year = 2017 AND r.name = 'Italian Grand Prix' AND q.q2 IS NOT NULL AND q.q2 <> '' AND q.q2 < '1:40.000'
challenging
263
formula_1
I'm reviewing the longest service pauses in races across the sport's whole history. Which racetrack saw the longest single pit stop ever? Please give me the racetrack name, location, country, the season year of that race, and the pit stop length. Consider all seasons in the data, and if several pit stops tie for the lo...
pit stop: a recorded pit-stop entry in a race; pit stop length: how long that pit stop lasted
SELECT c.name AS racetrack, c.location, c.country, r.year AS season_year, ps.duration AS pit_stop_length FROM pitStops ps JOIN races r ON r.raceId = ps.raceId JOIN circuits c ON c.circuitId = r.circuitId WHERE ps.milliseconds = (SELECT MAX(milliseconds) FROM pitStops) ORDER BY c.name
challenging
264
formula_1
I'm researching the generation of drivers born in the 1980s and want to know how much final-session qualifying that generation actually did as a whole cohort. How many Q3 lap times did a driver born between 1981 and 1991 set on average? Count every driver born in that period in the average, including any who never set ...
Q3: the third and final qualifying session of a race weekend, whose times decide the top starting grid positions.
SELECT ROUND(COUNT(q.q3 > '' OR NULL) * 1.0 / COUNT(DISTINCT d.driverId), 2) AS avg_q3_times_per_driver FROM drivers d LEFT JOIN qualifying q ON q.driverId = d.driverId WHERE STRFTIME('%Y', d.dob) BETWEEN '1981' AND '1991'
challenging
265
formula_1
I'm profiling drivers whose first name alone doesn't identify them. Across all races in the dataset throughout Formula 1 history, how many qualifying 1 times slower than two minutes were set by drivers who share their first name with at least one other driver and who have no code? I want just that single count.
A driver's code is their three-letter abbreviation (e.g. 'HAM'); having no code means no such abbreviation is on record for them. The qualifying 1 time is the time a driver set in the first qualifying session of a race, one per race they took part in.
SELECT COUNT(*) AS n FROM qualifying q JOIN drivers d ON d.driverId = q.driverId WHERE d.code IS NULL AND EXISTS (SELECT 1 FROM drivers o WHERE o.forename = d.forename AND o.driverId <> d.driverId) AND q.q1 IS NOT NULL AND (CAST(substr(q.q1, 1, instr(q.q1, ':') - 1) AS REAL) > 2 OR (CAST(substr(q.q1, 1, instr(q.q1, ':'...
challenging
266
formula_1
I'm checking how much of the championship points for drivers named Robert came from races covered by the lap-by-lap timing archive. For every driver whose forename is Robert, I need their reference name and the total points they scored in races where lap-by-lap timing data exists for that driver in that race. The total...
The reference name is the driver's short identifier, and points are championship points recorded for that driver's race result.
SELECT d.driverRef AS reference_name, COALESCE(SUM(r.points), 0) AS total_points FROM drivers d LEFT JOIN results r ON r.driverId = d.driverId AND EXISTS (SELECT 1 FROM lapTimes l WHERE l.raceId = r.raceId AND l.driverId = d.driverId) WHERE d.forename = 'Robert' GROUP BY d.driverId, d.driverRef ORDER BY total_points DE...
challenging
267
formula_1
For every edition of the French Grand Prix from the 1990 season through the most recent edition, I'd like to know which drivers set a fastest lap of the race slower than 90 seconds. For each such driver, give me the date of the edition, the driver's forename and surname, and their fastest lap time of that race in m:ss....
A driver's fastest lap in an edition means the shortest time they took to complete any single lap of that race, measured from the start of that lap; the 90-second threshold applies to that fastest lap, not to an average.
WITH editions AS (SELECT raceId, date FROM races WHERE name = 'French Grand Prix' AND year >= 1990), best AS (SELECT raceId, driverId, MIN(milliseconds) AS best FROM lapTimes GROUP BY raceId, driverId) SELECT e.date, d.forename, d.surname, lt.time FROM editions e LEFT JOIN best b ON b.raceId = e.raceId AND b.best > 90 ...
challenging
268
formula_1
I'm putting together a trivia fact about Italian drivers in Formula 1. Who holds the fastest lap time set in Q3 - the third and final part of qualifying - by an Italian driver, across the sport's entire history? Please give me the driver's forename, surname, and the fastest Q3 lap time exactly as it appears in the data...
Q3 is the third and final part of qualifying.
SELECT d.forename, d.surname, q.q3 AS q3_time FROM qualifying q JOIN drivers d ON d.driverId = q.driverId WHERE d.nationality = 'Italian' AND q.q3 <> '' AND q.q3 = (SELECT MIN(q2.q3) FROM qualifying q2 JOIN drivers d2 ON d2.driverId = q2.driverId WHERE d2.nationality = 'Italian' AND q2.q3 > '')
challenging
269
formula_1
I'm writing a piece on the new generation of British Formula 1 talent. Among British F1 drivers born after 1980, who set the fastest Q3 qualifying time, what was that time, and at which race and in which year was it set? Please give me the driver's full name, the race name, the race year, and the Q3 time. By 'born afte...
Q3 is the final part of the qualifying session that decides the front of the starting grid.
WITH cohort AS (SELECT q.driverId, q.raceId, q.q3 FROM drivers d JOIN qualifying q ON d.driverId = q.driverId WHERE d.nationality = 'British' AND CAST(STRFTIME('%Y', d.dob) AS INTEGER) > 1980 AND q.q3 IS NOT NULL AND TRIM(q.q3) != '') SELECT d.forename || ' ' || d.surname AS driver_name, r.name AS race_name, r.year AS ...
challenging
270
formula_1
We're writing a piece on F1's younger generation and want a number for it: how many drivers born in 1976 or later (born from 1 January 1976 onwards) have ever set the second-fastest lap of a race at any point in the history covered by the data? I just need the single count of such drivers.
By second-fastest lap of a race I mean the driver's own fastest lap in that race was the second quickest among all drivers in that race.
SELECT COUNT(DISTINCT T1.driverId) AS driver_count FROM results AS T1 INNER JOIN drivers AS T2 ON T1.driverId = T2.driverId WHERE STRFTIME('%Y', T2.dob) >= '1976' AND T1.rank = 2
challenging
271
formula_1
Who set the fastest time in Q3 (the final qualifying session) at the 2017 Italian Grand Prix? Please give me the driver's reference name (the short identifier like 'hamilton') as the result. If two drivers set the same fastest Q3 time, give me both of their reference names.
Q3: the final qualifying session of a Grand Prix weekend
SELECT d.driverRef FROM qualifying q JOIN drivers d ON d.driverId = q.driverId JOIN races r ON r.raceId = q.raceId WHERE r.year = 2017 AND r.name = 'Italian Grand Prix' AND q.q3 IS NOT NULL AND q.q3 != '' AND q.q3 = (SELECT MIN(q3) FROM qualifying WHERE raceId = q.raceId AND q3 IS NOT NULL AND q3 != '')
challenging
272
formula_1
Can you rank the Italian Formula 1 constructors by career points and give me the three highest-placed ones? For each, I'd like the constructor name, their career points, and their introduction website. Career points means the total of the points the constructor finished each season with (a season being one calendar yea...
WITH season AS ( SELECT cs.constructorId, cs.points, ROW_NUMBER() OVER (PARTITION BY cs.constructorId, r.year ORDER BY cs.raceId DESC) AS rn FROM constructorStandings cs JOIN races r ON r.raceId = cs.raceId ), career AS ( SELECT constructorId, SUM(points) AS pts FROM season WHERE rn =...
challenging
273
formula_1
I'm writing a piece about how the same common forenames recur across Formula 1 history. Could you tell me how many different drivers named Peter have ever won a Formula 1 race, and the same for drivers named John? Please give me one count per forename, so I can compare the two — if they turn out to be equal, just repor...
A race win means the driver finished first in that race.
SELECT T1.forename, COUNT(DISTINCT T1.driverId) AS winner_drivers FROM drivers AS T1 INNER JOIN results AS T2 ON T2.driverId = T1.driverId WHERE T2.positionOrder = 1 AND T1.forename IN ('John','Peter') GROUP BY T1.forename ORDER BY T1.forename
challenging
274
formula_1
I'm working on a story about qualifying records. Across all races, which driver set the fastest time in Q3 (the final part of qualifying)? I'd like the nationality of that driver - and if more than one driver shares the fastest Q3 time, please give me every such driver's nationality. I don't need anything else, just th...
Q3: the third and final part of the qualifying session.
SELECT DISTINCT d.nationality FROM qualifying q JOIN drivers d ON d.driverId = q.driverId WHERE TRIM(q.q3) = (SELECT MIN(q3) FROM qualifying WHERE q3 IS NOT NULL AND TRIM(q3) <> '')
challenging
275
formula_1
I'm putting together a historical note on constructor nationalities in Formula 1. For each constructor nationality, how many constructors from that nationality have recorded the fastest lap of a race? Please include every nationality in the result, with a count of 0 for nationalities where no constructor has done so. C...
Fastest lap: the constructor officially credited with the quickest single lap time in a race's recorded result.
SELECT c.nationality, COUNT(DISTINCT CASE WHEN r.rank = 1 THEN c.constructorId END) AS num_constructors FROM constructors c LEFT JOIN results r ON c.constructorId = r.constructorId GROUP BY c.nationality ORDER BY num_constructors DESC, c.nationality
challenging
276
formula_1
I'm studying how pit stop activity changed across the refuelling-rule change for Australia's F1 drivers. For each season from 2002 through 2013, can you give me the total number of pit stops made at the races of that season where, after the race, at least one Australian driver was placed better than 20th in the driver ...
Better than 20th in the driver standings means a position of 1 through 19 after that race. A pit stop is any recorded pit stop made by any driver at the race.
WITH oz_races AS (SELECT DISTINCT r.raceId, r.year FROM races AS r INNER JOIN driverStandings AS ds ON ds.raceId = r.raceId INNER JOIN drivers AS d ON d.driverId = ds.driverId WHERE d.nationality = 'Australian' AND ds.position <= 19 AND r.year BETWEEN 2002 AND 2013) SELECT oz.year AS season, COUNT(ps.stop) AS pit_stops...
challenging
277
formula_1
For the 1995 season (races held in the calendar year 1995), how many drivers set a Q1 lap time of more than two minutes - i.e., strictly longer than 2:00.000? I just need the number of drivers.
Q1: the first of the qualifying sessions; a Q1 time is the lap time a driver set in that session.
SELECT COUNT(DISTINCT T1.driverId) FROM qualifying AS T1 INNER JOIN races AS T2 ON T2.raceId = T1.raceId WHERE T2.year = 1995 AND (length(T1.q1) > length('2:00.000') OR T1.q1 > '2:00.000')
challenging
278
formula_1
For my monthly recap I need the fastest pit stop made during any F1 race held in September 2016. Please tell me the driver's full name, the pit stop time, the name of the race where it happened, the circuit name, and the circuit location. If two or more pit stops share the fastest time, list each of them with its own d...
By pit stop time I mean the elapsed time of a pit stop, from the car arriving at its pit to rejoining the race.
WITH scope AS (SELECT ps.driverId, ps.duration, ps.milliseconds, r.raceId FROM pitStops ps JOIN races r ON r.raceId = ps.raceId WHERE r.date >= '2016-09-01' AND r.date < '2016-10-01') SELECT d.forename || ' ' || d.surname AS driver_name, s.duration AS pit_stop_time, r.name AS race_name, c.name AS circuit_name, c.locati...
challenging
279
formula_1
I'm profiling the oldest driver ever to win a Formula 1 race. Please identify the winning driver with the earliest date of birth - if two winning drivers share that birth date, use the one whose full name (forename then surname) comes first alphabetically. For that driver, return his full name (forename then surname), ...
Grand Prix: any race event in the database, whatever its name is; some race names do not contain the words 'Grand Prix'.
WITH winner AS (SELECT d.driverId, d.forename, d.surname, d.nationality FROM drivers d WHERE EXISTS (SELECT 1 FROM results res WHERE res.driverId = d.driverId AND res.position = 1) ORDER BY d.dob ASC, d.forename ASC, d.surname ASC LIMIT 1) SELECT w.forename || ' ' || w.surname AS full_name, w.nationality, (SELECT r.nam...
challenging
280
formula_1
Across the history of the Canadian Grand Prix, how many runnings of the race have had at least one car retire with an accident? Please note that a crash in which the driver was killed is recorded as its own status and should not count as an accident here. I just need the single number of runnings, nothing else.
accident: a car's race ends because of a crash
SELECT COUNT(DISTINCT T1.raceId) FROM results AS T1 INNER JOIN races AS T2 ON T1.raceId = T2.raceId INNER JOIN status AS T3 ON T1.statusId = T3.statusId WHERE T3.status = 'Accident' AND T2.name = 'Canadian Grand Prix'
challenging
281
formula_1
For the 2017 Italian Grand Prix, which driver set the second-fastest time in the second qualifying session (Q2), and which constructor did he drive for that weekend? By second-fastest I mean the driver whose Q2 time comes next after the quickest one, counting every driver who took part in that session. If two drivers t...
Q2: the second of the weekend's qualifying sessions; its times are measured to the thousandth of a second.
WITH t AS (SELECT d.driverId, d.forename, d.surname, co.name AS constructor, q.q2, q.position, CAST(SUBSTR(q.q2, 1, INSTR(q.q2, ':') - 1) AS INTEGER) AS mins, CAST(SUBSTR(q.q2, INSTR(q.q2, ':') + 1) AS REAL) AS secs FROM qualifying q JOIN races r ON r.raceId = q.raceId JOIN drivers d ON d.driverId = q.driverId JOIN con...
challenging
282
formula_1
I'm writing a season review piece and need the outright quickest Q2 time of the 2017 Formula One season – the fastest single Q2 time set at any race that year. Please give me the surname of the driver who set it and the name of the race where it was set. If two drivers share that fastest Q2 time, list every one of them...
Q2: the second of the three qualifying sessions of a Grand Prix weekend.
WITH t AS (SELECT d.surname AS surname, r.name AS race_name, q.driverId AS driverId, q.q2 AS q2 FROM qualifying q JOIN drivers d ON d.driverId = q.driverId JOIN races r ON r.raceId = q.raceId WHERE r.year = 2017 AND q.q2 LIKE '%:%.%'), m AS (SELECT MIN(q2) AS q2 FROM t) SELECT t.surname, t.race_name FROM t JOIN m ON m....
challenging
283
formula_1
I'm writing a race report and need to name the pole-sitter by their permanent race number. Who set the fastest Q3 qualifying time at the 2017 Italian Grand Prix (the race named 'Italian Grand Prix' in the 2017 season)? Please give me just that driver's permanent number. If two or more drivers share that fastest time, g...
Q3 is the final part of qualifying, whose times decide the top starting positions; the fastest Q3 time earns pole position.
SELECT d.number FROM qualifying AS q JOIN races AS r ON r.raceId = q.raceId JOIN drivers AS d ON d.driverId = q.driverId WHERE r.name = 'Italian Grand Prix' AND r.year = 2017 AND q.q3 IS NOT NULL AND q.q3 <> '' AND q.q3 = (SELECT MIN(q2.q3) FROM qualifying AS q2 JOIN races AS r2 ON r2.raceId = q2.raceId WHERE r2.name =...
challenging
284
formula_1
For the 2017 Italian Grand Prix, who set the fastest lap time in Q3 (the final qualifying session), and what was that time? Give me the driver code along with the fastest Q3 time. If two or more drivers tie for the fastest Q3 time, list every tied driver.
Q3: the third and final qualifying session; a driver's Q3 time is the lap time that driver set in that session
SELECT T2.code, T1.q3 FROM qualifying AS T1 INNER JOIN drivers AS T2 ON T2.driverId = T1.driverId WHERE T1.raceId = (SELECT raceId FROM races WHERE year = 2017 AND name = 'Italian Grand Prix') AND T1.q3 <> '' AND T1.q3 = (SELECT MIN(q3) FROM qualifying WHERE raceId = (SELECT raceId FROM races WHERE year = 2017 AND name...
challenging
285
formula_1
I'm writing a report on the 2017 Italian Grand Prix (the one held in the 2017 season). Could you tell me the nationality of the driver who set the fastest time in Q3, the final part of qualifying? If several drivers share the same fastest Q3 time, please give each of their nationalities.
Q3 is the third and final part of the qualifying session.
SELECT d.nationality FROM qualifying q JOIN races r ON r.raceId = q.raceId JOIN drivers d ON d.driverId = q.driverId WHERE r.year = 2017 AND r.name = 'Italian Grand Prix' AND q.q3 IS NOT NULL AND q.q3 <> '' AND q.q3 = (SELECT MIN(q2.q3) FROM qualifying q2 JOIN races r2 ON r2.raceId = q2.raceId WHERE r2.year = 2017 AND ...
challenging
286
formula_1
Which Chinese Grand Prix edition from the 2000s (2000–2009 seasons) had the earliest scheduled local start time? Give me the season year of that edition and the runner-up's time gap to the winner (the runner-up being the driver classified second at the finish). If several editions share the earliest scheduled local sta...
Scheduled local start time: the time of day the race was scheduled to start, local to the circuit.
SELECT r.year, res.time AS runner_up_gap FROM races r JOIN results res ON res.raceId = r.raceId WHERE r.name = 'Chinese Grand Prix' AND r.year BETWEEN 2000 AND 2009 AND res.positionOrder = 2 AND r.time IS NOT NULL AND r.time = (SELECT MIN(r2.time) FROM races r2 WHERE r2.name = 'Chinese Grand Prix' AND r2.year BETWEEN 2...
challenging
287
formula_1
I'm writing a qualifying report. Can you give me the driver codes of all drivers who set a time in the third qualifying session at the 2017 Italian Grand Prix? One row per driver who set such a time, and please list the codes in alphabetical order. Nothing else needed.
The third qualifying session is the final segment of the race weekend's qualifying, in which only the fastest drivers take part.
SELECT d.code FROM qualifying q JOIN races r ON r.raceId = q.raceId JOIN drivers d ON d.driverId = q.driverId WHERE r.year = 2017 AND r.name = 'Italian Grand Prix' AND q.q3 IS NOT NULL AND q.q3 != '' ORDER BY d.code
challenging
288
formula_1
I'm writing about how the Canadian Grand Prix moved to early-evening starts. Could you find the editions of the Canadian Grand Prix from 2007 through 2017 (both years included) that began at 17:00 local time or earlier - a race that began exactly at 17:00 counts. For each one, give me the race year, the winner's forena...
An edition is one running of the Canadian Grand Prix in a given year; local start time is the clock time of day, local to the circuit, at which the race began, on the 24-hour clock.
SELECT r.year, d.forename, d.surname, res.time AS finish_time FROM races r JOIN results res ON res.raceId = r.raceId AND res.positionOrder = 1 JOIN drivers d ON d.driverId = res.driverId WHERE r.name = 'Canadian Grand Prix' AND r.year BETWEEN 2007 AND 2017 AND time(r.time) <= time('17:00') ORDER BY r.year
challenging
289
formula_1
I'm writing a retrospective on the 2017 Italian Grand Prix at Monza. For the Q3 session of that race's qualifying, can you give me the permanent career race number and surname of every driver whose Q3 lap time was strictly faster than 1 minute 38.000 seconds? I want all drivers under that mark, not just the fastest, an...
Q3 is the third and final qualifying session; a driver only has a Q3 time if he actually took part in that session.
WITH mark(t) AS (SELECT '1' || ':' || '38.000') SELECT d.number, d.surname FROM qualifying AS q INNER JOIN races AS r ON r.raceId = q.raceId INNER JOIN drivers AS d ON d.driverId = q.driverId WHERE r.year = 2017 AND r.name = 'Italian Grand Prix' AND q.q3 LIKE '%:%' AND q.q3 < (SELECT t FROM mark) ORDER BY d.number;
challenging
290
formula_1
Across all seasons in the database, which circuit's Grand Prix started earliest in the day, and which started latest? I'd like the circuit reference name of the earliest one, the circuit reference name of the latest one, and the gap between those two start times in seconds. Include every season, and if several circuits...
By "start time of the day" I mean the local clock time at the venue at which the race started.
WITH per_circuit AS (SELECT c.circuitRef AS circuitRef, MIN(CAST(strftime('%s', r.time) AS INTEGER)) AS earliest_start_seconds, MAX(CAST(strftime('%s', r.time) AS INTEGER)) AS latest_start_seconds FROM races r JOIN circuits c ON c.circuitId = r.circuitId WHERE r.time IS NOT NULL GROUP BY c.circuitRef) SELECT GROUP_CONC...
challenging
291
formula_1
I'm looking into fastest race laps recorded by drivers born in the 1980s. Can you tell me which three drivers born between 1980 and 1990 reached the highest single fastest-lap speed at any point in their career? I'd like their first and last names, ranked from the highest career-high speed down, and if two drivers are ...
Fastest lap speed: the average speed, in km/h, that a driver recorded on their single fastest lap of a race. A driver's career-high speed is based on that one lap, not an average across their laps.
SELECT d.forename, d.surname FROM drivers AS d JOIN results AS r ON r.driverId = d.driverId WHERE SUBSTR(d.dob, 1, 4) BETWEEN '1980' AND '1990' AND r.fastestLapSpeed IS NOT NULL GROUP BY d.driverId, d.forename, d.surname, d.dob ORDER BY MAX(CAST(r.fastestLapSpeed AS REAL)) DESC, d.dob, d.surname LIMIT 3
challenging
292
formula_1
For the 2017 Italian Grand Prix, I need the list of drivers who were eliminated after the first qualifying session. Give me one row per eliminated driver with their reference name, ordered by their first-segment time from slowest to fastest. If two eliminated drivers have the same first-segment time, list the one with ...
Eliminated after the first qualifying session means the driver took part in qualifying but did not go out in the second qualifying session.
SELECT d.driverRef FROM qualifying q JOIN races r ON r.raceId = q.raceId JOIN drivers d ON d.driverId = q.driverId WHERE r.name = 'Italian Grand Prix' AND r.year = 2017 AND q.q1 IS NOT NULL AND (q.q2 IS NULL OR q.q2 = '') ORDER BY q.q1 DESC, d.driverId
challenging
293
formula_1
For my retrospective on the 1962 British Grand Prix – the race named 'British Grand Prix' in the 1962 season – how many drivers withdrew from that race? I just need a single whole number.
A driver who withdrew from a race is recorded with the status 'Withdrew'.
SELECT COUNT(DISTINCT x.driverId) AS num_withdrawn_drivers FROM results AS x JOIN races AS r ON r.raceId = x.raceId JOIN status AS s ON s.statusId = x.statusId WHERE r.name = 'British Grand Prix' AND r.year = 1962 AND s.status = 'Withdrew'
challenging
294
formula_1
For the 1957 British Grand Prix, what percentage of the drivers who took part in the race were classified finishers, rounded to two decimals? By classified finisher I mean a driver whose race record shows them finishing the race, whether on the lead lap or one or more laps behind the winner. I just need the one percent...
classified finisher: a driver whose race record shows them finishing the race, whether on the lead lap or one or more laps behind the winner
SELECT ROUND(100.0 * COUNT(DISTINCT CASE WHEN x.position IS NOT NULL THEN x.driverId END) / COUNT(DISTINCT x.driverId), 2) AS finisher_percentage FROM races r JOIN results x ON x.raceId = r.raceId WHERE r.year = 1957 AND r.name = 'British Grand Prix'
challenging
295
student_club
What was Elijah Allen's mean total expense per event for expenses dated in September or October? Round to two decimals.
WITH per_event AS ( SELECT b.link_to_event, SUM(e.cost) AS event_spend FROM expense AS e JOIN member AS m ON m.member_id = e.link_to_member JOIN budget AS b ON b.budget_id = e.link_to_budget WHERE m.first_name = 'Elijah' AND m.last_name = 'Allen' AND SUBSTR(e.expense_date, 6, 2) IN ('09','10') AND b.link_to_event IS NO...
challenging
296
student_club
What is the total amount spent on Student Club events in 2019 minus the total spent on events in 2020?
SELECT SUM(CASE WHEN SUBSTR(T1.event_date, 1, 4) = '2019' THEN T2.spent ELSE 0 END) - SUM(CASE WHEN SUBSTR(T1.event_date, 1, 4) = '2020' THEN T2.spent ELSE 0 END) AS num FROM event AS T1 INNER JOIN budget AS T2 ON T1.event_id = T2.link_to_event
moderate
297
student_club
What note was recorded for the fundraising proceeds received on September 14, 2019?
SELECT notes FROM income WHERE source = 'Fundraising' AND date_received = '2019-09-14'
simple
298
student_club
What is the ratio of the Advertisement budget for the Yearly Kickoff meeting to that for the October Meeting?
SELECT CAST(SUM(CASE WHEN T2.event_name = 'Yearly Kickoff' THEN T1.amount ELSE 0 END) AS REAL) / SUM(CASE WHEN T2.event_name = 'October Meeting' THEN T1.amount ELSE 0 END) FROM budget AS T1 INNER JOIN event AS T2 ON T1.link_to_event = T2.event_id WHERE T1.category = 'Advertisement' AND T2.type = 'Meeting'
challenging
299
student_club
Which closed events are tied for the highest ratio of total spending to total budget amount?
WITH ratios AS ( SELECT e.event_id, e.event_name, CAST(SUM(b.spent) AS DECIMAL(20,4)) / NULLIF(SUM(b.amount), 0) AS ratio FROM event AS e JOIN budget AS b ON b.link_to_event = e.event_id WHERE e.status = 'Closed' GROUP BY e.event_id, e.event_name HAVING SUM(b.amount) > 0 ) SELECT event_name FROM ratios WHERE ratio = (S...
moderate