SQL Interview Questions
Most SQL interview mistakes start with the wrong row count or the wrong meaning of missing data. State what one row in your result represents before deciding how to join, filter or aggregate.
Try answering aloud before opening each explanation.
Try the SQL practice demoFoundations
Why can an inner join produce more rows than either input?
It emits one row for each matching pair. Three left rows and four right rows sharing a key yield twelve joined rows for that key. Verify the cardinality before aggregating.
How does COUNT(*) differ from COUNT(column)?
COUNT(*) counts result rows, including rows with null fields. COUNT(column) counts only non-null values, which is useful when counting matches after a left join.
Why does column = NULL not find missing values?
Ordinary comparisons with NULL generally evaluate to unknown, and WHERE retains only true conditions. Use IS NULL to find missing values.
When does a filter belong in HAVING rather than WHERE?
WHERE filters input rows before grouping; HAVING filters the groups. Put a threshold on SUM(amount) in HAVING, but put the date range of rows to sum in WHERE.
How does UNION ALL differ from UNION?
UNION ALL keeps every input row. UNION removes duplicate result rows across and within its inputs. Use ALL when duplicates carry meaning or are already ruled out by the inputs.
Applied
How do you keep customers with no paid order in a left join?
Put the paid-order condition in the join's ON clause. A WHERE predicate requiring a paid status rejects the null-extended rows and loses customers with no match.
What can go wrong with NOT IN when the subquery returns NULL?
A non-matching row may evaluate to unknown rather than true because of the NULL in the list. A correlated NOT EXISTS expresses the anti-join directly; decide explicitly how null keys should be treated.
When should you use a window function instead of GROUP BY?
Use a window function when you need to retain detail rows and also calculate values over a partition. GROUP BY collapses rows to one result per group.
How do ROW_NUMBER, RANK and DENSE_RANK treat ties?
ROW_NUMBER assigns distinct positions. RANK gives ties the same position and leaves gaps; DENSE_RANK gives ties the same position without gaps. Use an explicit tie-breaker when row selection must be deterministic.
Why can a running total jump across rows with the same timestamp?
The default ordered window frame in many systems includes peers with equal ordering values. Use an explicit ROWS frame and a unique ordering when you intend one step per row.
Production scenarios
Why does a revenue total double after joining orders to both line items and payments?
Multiple rows on each child side multiply matches per order. Aggregate each child to the order grain first, then join the two summaries before calculating the total.
How can applying a function to a timestamp make filtering slower?
A predicate on a transformed column may not use a normal index on the column. Prefer a half-open range on the original timestamp, or use an appropriate expression index if the engine supports it.
How would you index a query that filters by account and returns its newest events?
Consider an index starting with account ID and then the ordering key and tie-breaker. Verify with the actual plan and realistic account sizes; indexes also cost storage and write work.
How do you claim a limited unit without overselling under concurrency?
Avoid a separate unchecked read followed by a write. Use an atomic conditional update or an appropriate lock inside a transaction, and treat no affected row as an unsuccessful claim.
Why might adding DISTINCT hide a data bug?
It discards equal output rows but does not fix a bad join or incorrect business grain. Compare keys and expected multiplicity before deduplicating, especially when sums or counts are involved.
Further reading
Official documentation
Related question sets
- Node.js interview questions
- TypeScript interview questions
- REST API interview questions
- Microservices interview questions
- Python interview questions
- JavaScript interview questions
- React interview questions
- Kubernetes interview questions
- System Design interview questions
- Java interview questions
- Docker interview questions
- MongoDB interview questions
- PostgreSQL interview questions
- Redis interview questions
- NestJS interview questions
- RabbitMQ interview questions