SQL fundamentals interview questions
LiveEngine-agnostic SQL: the joins, aggregates, subqueries and set logic every data interview starts with.
Subtopics
- SELECT, WHERE & ordering10
- Joins10
- Aggregates & GROUP BY/HAVING10
- Subqueries & derived tables10
- Set operations10
- NULL semantics10
- CTEs10
- Window functions10
- Data types & casting10
- Constraints & keys10
- Normalization10
- DML: INSERT, UPDATE, DELETE10
Sample questions
- CodingJuniorAggregates & GROUP BY/HAVING
Table
products(id, category, price)wherepriceis never NULL. Return one row per category withcategory,product_count(how many products it has) andtotal_price(the sum of their prices). Categories are compared exactly as stored. - CodingJuniorAggregates & GROUP BY/HAVING
Table
orders(id, customer_id),customer_idnever NULL. Returncustomer_idandorder_countfor every customer who has placed three or more orders. Customers below the threshold must not appear at all. - TheoryJuniorAggregates & GROUP BY/HAVING
Walk me through the difference between
COUNT(*),COUNT(price)andCOUNT(DISTINCT price)on the same table. On a table of five rows where two prices are missing and the other three are all 9.99, what does each one return? - TheoryJuniorAggregates & GROUP BY/HAVING
What does
GROUP BYactually do to a result set? And why does the database rejectSELECT category, name, SUM(price) FROM products GROUP BY category? - TheoryJuniorAggregates & GROUP BY/HAVING
When do you put a condition in
WHEREand when does it belong inHAVING? Give one condition that only works inHAVINGand one that would be a mistake to put there. - CodingJuniorConstraints & keys
Before adding a
UNIQUEconstraint onusers.emailyou need to know what is in the way. Tableusers(id, email); emails are compared exactly as stored. Returnemailandcopiesfor every address that appears on more than one row.