SQL comes up in almost every production support, DevOps and data-facing interview in banks. Interviewers usually test three things: whether you understand the core concepts, whether you can write a query under pressure, and whether you’d handle production data safely. This page covers all three, with a practice dataset you can run yourself.
Originally published January 2021 as my SQL learning notes, rewritten November 2026. For the day-to-day commands, see the companion page: SQL commands and utilities for application support.
Jump to
- Part 1: Core concepts (15 questions)
- Part 2: Practice queries on a trading dataset (10 questions)
- Part 3: Production scenarios (4 questions)
- Interview tips
Part 1: Core concepts
1. What’s the difference between OLTP and OLAP?
OLTP (online transaction processing) systems handle many small, fast inserts, updates and deletes: an order management system or a booking system. They’re highly normalised to keep writes fast and consistent. OLAP (online analytical processing) systems, such as data warehouses, are built for large read-heavy queries across history: risk reporting, P&L analysis. They’re often denormalised to make reads fast. Most databases start life as OLTP; as volumes grow, firms copy data into a separate OLAP store so reporting doesn’t slow down trading.
2. What is an index, and what’s the trade-off?
An index is a separate structure (usually a B-tree) that lets the database find rows without scanning the whole table, like the index at the back of a book. The trade-off: every INSERT, UPDATE and DELETE must also update each index, so too many indexes slow down writes and use extra space. On an OLTP system you index the columns you filter and join on most, and no more.
3. Clustered vs non-clustered index?
A clustered index defines the physical order of the rows in the table, so there can only be one (in SQL Server it’s usually the primary key). A non-clustered index is a separate structure that points back to the rows; a table can have many.
4. What does ACID stand for?
Atomicity: a transaction happens completely or not at all. Consistency: it moves the database from one valid state to another, respecting constraints. Isolation: concurrent transactions don’t see each other’s half-finished work. Durability: once committed, it survives a crash. In trading terms: a trade and its cash movement are booked together, or neither is.
5. What is normalisation? Explain 1NF, 2NF and 3NF.
Normalisation organises data to remove redundancy and ambiguity. 1NF: every column holds a single value and there are no repeating groups. 2NF: 1NF, and every non-key column depends on the whole primary key, not part of it. 3NF: 2NF, and non-key columns don’t depend on other non-key columns. For example, a counterparty’s country belongs in the counterparty table, not repeated on every trade. Denormalisation deliberately reverses this for read performance, typically in OLAP.
6. What do COMMIT, ROLLBACK and SAVEPOINT do?
COMMIT makes a transaction’s changes permanent. ROLLBACK undoes everything since the transaction started. SAVEPOINT name marks a point you can roll back to without abandoning the whole transaction.
7. What are DDL, DML, DCL and TCL?
DDL (data definition) changes structure: CREATE, ALTER, DROP, TRUNCATE. DML (data manipulation) works with rows: SELECT, INSERT, UPDATE, DELETE, MERGE. DCL (data control) manages access: GRANT, REVOKE. TCL (transaction control): COMMIT, ROLLBACK, SAVEPOINT.
8. DELETE vs TRUNCATE vs DROP?
DELETE removes chosen rows (with a WHERE clause), is logged row by row, fires triggers and can be rolled back. TRUNCATE removes all rows quickly by deallocating storage; it’s DDL, so in Oracle it can’t be rolled back. DROP removes the table itself, structure and all.
9. WHERE vs HAVING?
WHERE filters rows before grouping; HAVING filters groups after GROUP BY, so it can use aggregates like COUNT(*) > 1.
10. Explain the types of JOIN.
INNER: only rows that match in both tables. LEFT (OUTER): all rows from the left table, with NULLs where the right has no match. RIGHT: the reverse. FULL OUTER: all rows from both, matched where possible; useful for reconciliations. CROSS: every combination of rows.
11. UNION vs UNION ALL?
UNION removes duplicate rows, which costs a sort. UNION ALL keeps everything and is faster. Use UNION ALL unless you specifically need de-duplication.
12. How do NULLs behave?
NULL means unknown, so NULL = NULL is not true. Use IS NULL / IS NOT NULL. Aggregates like SUM and AVG ignore NULLs, and COUNT(column) skips them while COUNT(*) doesn’t. A common bug: NOT IN a list that contains a NULL returns no rows at all.
13. Primary key vs foreign key vs unique constraint?
A primary key uniquely identifies each row and can’t be NULL; one per table. A unique constraint also enforces uniqueness but allows a NULL (rules vary by database), and you can have several. A foreign key makes a column reference another table’s key, so you can’t book a trade against a counterparty that doesn’t exist.
14. View vs materialised view?
A view is a saved query: it runs every time you select from it and always shows current data. A materialised view stores the result physically and is refreshed on a schedule or on commit. It’s much faster to read, but it can be stale.
15. What causes a deadlock, and how does the database handle it?
Two transactions each hold a lock the other needs, so neither can proceed. The database detects the cycle and kills one of them as the “victim”, rolling it back with an error (ORA-00060 in Oracle, error 1205 in SQL Server). Prevention: access tables in a consistent order, keep transactions short, and index the columns used in WHERE clauses so fewer rows get locked.
Part 2: Practice queries on a trading dataset
Create these two tables and load the sample data in any database: PostgreSQL, SQL Server, MySQL, or SQLite for a quick start (sqlite3 practice.db). On Oracle versions before 23ai, use one INSERT per row. Bond prices are quoted per 100 nominal, so notional = quantity × price / 100. The data includes a few deliberate problems for you to find.
CREATE TABLE counterparties (
cpty_id INTEGER PRIMARY KEY,
name VARCHAR(50),
country CHAR(2)
);
CREATE TABLE trades (
trade_id INTEGER PRIMARY KEY,
trade_date DATE,
cpty_id INTEGER,
isin CHAR(12),
side CHAR(4),
quantity INTEGER,
price DECIMAL(10,4),
status VARCHAR(10)
);
INSERT INTO counterparties VALUES
(1,'Barclays','GB'),(2,'HSBC','GB'),(3,'JP Morgan','US'),(4,'Deutsche Bank','DE'),(5,'Nomura','JP');
INSERT INTO trades VALUES
(101,'2026-11-16',1,'GB00BYZJ7G42','BUY', 1000000,99.8750,'BOOKED'),
(102,'2026-11-16',2,'GB00BYZJ7G42','SELL', 500000,99.9000,'BOOKED'),
(103,'2026-11-16',3,'US91282CJL54','BUY', 2000000,98.2500,'BOOKED'),
(104,'2026-11-16',1,'GB00BYZJ7G42','BUY', 1000000,99.8750,'BOOKED'),
(105,'2026-11-17',2,'US91282CJL54','SELL', 750000,98.3000,'FAILED'),
(106,'2026-11-17',3,'GB00BYZJ7G42','BUY', 250000,99.9500,'BOOKED'),
(107,'2026-11-17',4,'DE0001102580','SELL',1500000,97.1000,'CANCELLED'),
(108,'2026-11-17',1,'DE0001102580','BUY', 300000,97.1500,'BOOKED'),
(109,'2026-11-17',9,'US91282CJL54','BUY', 100000,98.3500,'FAILED');
Q1. Count trades by status
Show how many trades are in each status, most common first.
Show answer
SELECT status, COUNT(*) AS trades
FROM trades
GROUP BY status
ORDER BY trades DESC;
Result:
status | trades
BOOKED | 6
FAILED | 2
CANCELLED | 1
The warm-up. Interviewers check you know GROUP BY and can sort by an aggregate alias.
Q2. Notional by counterparty
For BOOKED trades only, show each counterparty’s name, trade count and total notional, highest notional first.
Show answer
SELECT c.name,
COUNT(*) AS trades,
SUM(t.quantity * t.price / 100) AS notional
FROM trades t
JOIN counterparties c ON c.cpty_id = t.cpty_id
WHERE t.status = 'BOOKED'
GROUP BY c.name
ORDER BY notional DESC;
Result:
name | trades | notional
Barclays | 3 | 2,288,950.00
JP Morgan | 2 | 2,214,875.00
HSBC | 1 | 499,500.00
Filtering status in WHERE (before grouping) is correct here. Putting it in HAVING would be wrong or fail, because status isn’t grouped.
Q3. Counterparties with no trades
List counterparties that have never traded.
Show answer
SELECT c.name
FROM counterparties c
LEFT JOIN trades t ON t.cpty_id = c.cpty_id
WHERE t.trade_id IS NULL;
Result:
name
Nomura
The LEFT JOIN + IS NULL pattern is the classic answer. NOT EXISTS also works and is safer than NOT IN when NULLs are possible.
Q4. Find orphan trades
Find trades whose counterparty doesn’t exist in the counterparties table. (This is why foreign keys matter.)
Show answer
SELECT t.trade_id, t.cpty_id
FROM trades t
LEFT JOIN counterparties c ON c.cpty_id = t.cpty_id
WHERE c.cpty_id IS NULL;
Result:
trade_id | cpty_id
109 | 9
A real support scenario: static data loaded late, so trades reference an unknown counterparty and fail to book.
Q5. Find duplicate trades
Find trades that appear more than once with the same date, counterparty, ISIN, side, quantity and price.
Show answer
SELECT trade_date, cpty_id, isin, side, quantity, price,
COUNT(*) AS copies
FROM trades
GROUP BY trade_date, cpty_id, isin, side, quantity, price
HAVING COUNT(*) > 1;
Result:
trade_date | cpty_id | isin | side | quantity | price | copies
2026-11-16 | 1 | GB00BYZJ7G42 | BUY | 1000000 | 99.8750 | 2
Trades 101 and 104 are identical: possibly a duplicate booking after a retry. Follow-up question: how would you list the trade IDs? (Join back, or use COUNT(*) OVER (PARTITION BY ...).)
Q6. Second-largest trade
Find the BOOKED trade(s) with the second-highest notional.
Show answer
SELECT trade_id, notional
FROM (
SELECT trade_id,
quantity * price / 100 AS notional,
DENSE_RANK() OVER (ORDER BY quantity * price DESC) AS rnk
FROM trades
WHERE status = 'BOOKED'
) ranked
WHERE rnk = 2;
Result:
trade_id | notional
101 | 998,750.00
104 | 998,750.00
A favourite. DENSE_RANK handles ties correctly: two trades share second place. ROW_NUMBER would arbitrarily pick one, and LIMIT 1 OFFSET 1 would too. Explain the difference between ROW_NUMBER, RANK and DENSE_RANK.
Q7. Latest trade per counterparty
For each counterparty ID, show its most recent trade (latest date, then highest trade ID).
Show answer
SELECT cpty_id, trade_id, trade_date
FROM (
SELECT cpty_id, trade_id, trade_date,
ROW_NUMBER() OVER (PARTITION BY cpty_id
ORDER BY trade_date DESC, trade_id DESC) AS rn
FROM trades
) x
WHERE rn = 1
ORDER BY cpty_id;
Result:
cpty_id | trade_id | trade_date
1 | 108 | 2026-11-17
2 | 105 | 2026-11-17
3 | 106 | 2026-11-17
4 | 107 | 2026-11-17
9 | 109 | 2026-11-17
“Latest row per group” comes up constantly: latest price per instrument, last status per order. ROW_NUMBER() ... PARTITION BY is the standard answer.
Q8. Running total of notional
For BOOKED trades, show each trade’s notional and a running total that restarts each day.
Show answer
SELECT trade_date, trade_id,
quantity * price / 100 AS notional,
SUM(quantity * price / 100) OVER (PARTITION BY trade_date
ORDER BY trade_id) AS running_total
FROM trades
WHERE status = 'BOOKED'
ORDER BY trade_date, trade_id;
Result:
trade_date | trade_id | notional | running_total
2026-11-16 | 101 | 998,750.00 | 998,750.00
2026-11-16 | 102 | 499,500.00 | 1,498,250.00
2026-11-16 | 103 | 1,965,000.00 | 3,463,250.00
2026-11-16 | 104 | 998,750.00 | 4,462,000.00
2026-11-17 | 106 | 249,875.00 | 249,875.00
2026-11-17 | 108 | 291,450.00 | 541,325.00
Window functions keep every row while adding an aggregate, which GROUP BY can’t do. PARTITION BY resets the total each day.
Q9. Day-on-day change in volume
Show the number of trades per day and the change from the previous day.
Show answer
SELECT trade_date,
COUNT(*) AS trades,
COUNT(*) - LAG(COUNT(*)) OVER (ORDER BY trade_date) AS change
FROM trades
GROUP BY trade_date
ORDER BY trade_date;
Result:
trade_date | trades | change
2026-11-16 | 4 | NULL
2026-11-17 | 5 | 1
LAG looks at the previous row. It’s handy for spotting a sudden drop in volume, which often means a feed has stopped.
Q10. Net position by counterparty
For BOOKED trades, show quantity bought, quantity sold, and the net position (bought minus sold) per counterparty.
Show answer
SELECT c.name,
SUM(CASE WHEN t.side = 'BUY' THEN t.quantity ELSE 0 END) AS bought,
SUM(CASE WHEN t.side = 'SELL' THEN t.quantity ELSE 0 END) AS sold,
SUM(CASE WHEN t.side = 'BUY' THEN t.quantity
ELSE -t.quantity END) AS net
FROM trades t
JOIN counterparties c ON c.cpty_id = t.cpty_id
WHERE t.status = 'BOOKED'
GROUP BY c.name
ORDER BY c.name;
Result:
name | bought | sold | net
Barclays | 2300000 | 0 | 2300000
HSBC | 0 | 500000 | -500000
JP Morgan | 2250000 | 0 | 2250000
Conditional aggregation with CASE inside SUM is a technique interviewers love, because it pivots data in one pass.
Part 3: Production scenarios
1. A report query that used to take 5 seconds now takes 10 minutes. What do you check?
Start with what changed: data volume, a release, or new data. Look at the execution plan for a full table scan where there used to be an index lookup. Check whether table statistics are stale after a large data load, and whether an index was dropped or became unusable. Check for blocking at the time it runs, and compare the estimated row counts in the plan against the real ones. Fix the cause (gather statistics, restore the index) rather than just killing and re-running.
2. Users say the application is frozen. You suspect the database. What do you do?
Check for blocking sessions (v$session.blocking_session, sys.dm_exec_requests or pg_blocking_pids). Find the head blocker and what it’s doing: often a session left “idle in transaction” or a long batch job. Communicate on the incident, agree with the owner or DBA before killing anything, and remember that killing it triggers a rollback that can itself take time.
3. You’ve been asked to update 500 rows in production. Talk me through it.
Approved change ticket and peer-reviewed script. Count the target rows first with the exact WHERE clause. Back up those rows to a dated table. Run the update in a transaction, check the rows-affected count equals 500, verify a sample, then commit. If anything doesn’t match, roll back and investigate. Record everything in the ticket, and know the rollback plan before you start.
4. A DELETE was run without a WHERE clause. What now?
If the transaction isn’t committed yet, roll back immediately. If it was committed: stop further changes to the table, escalate to the DBA, and assess the recovery options. Oracle Flashback Query (AS OF TIMESTAMP) can retrieve the old rows if undo data is still available. Otherwise it’s a point-in-time restore from backup or rebuilding from upstream systems. Then write up the prevention: peer review, transactions, and restricted production write access.
Interview tips
- Talk through your approach before writing. State which tables you’ll join and how you’ll filter. Partial credit is real.
- Ask about the data. Can there be NULLs? Duplicates? Ties? It shows production instinct.
- Know window functions. ROW_NUMBER, RANK, DENSE_RANK, LAG and SUM OVER cover most “hard” questions.
- Always mention safety for writes. Transactions, backups, row counts and change control are what separate support engineers from developers in these interviews.
- Practise on real data. Load the dataset above and invent your own questions: “which ISIN has the most failed trades?”
Keep going
- SQL commands and utilities for application support: sessions, locks, plans, safe fixes, CSV exports.
- 10 Python exercises for finance and production support engineers
- All interview prep guides