PostgreSQL interview questions
LiveJoins, window functions, EXPLAIN, MVCC, indexes and PostgreSQL-specific features.
Subtopics
- Joins10
- Aggregates & GROUP BY/HAVING10
- Window functions10
- CTEs10
- Indexes & seeks vs scans10
- Execution plans10
- Transactions & isolation levels10
- Deadlocks10
- Normalization10
- Stored procedures10
- NULL semantics10
- Query tuning10
Sample questions
- CodingJuniorAggregates & GROUP BY/HAVING
Table
sales(id, region, amount)whereamountis nullable. For every region that appears in the table return how many sale rows it has and the total of their amounts, treating a missing amount as 0 so that a region whose sales all have NULL amounts reports 0.00 rather than - CodingJuniorAggregates & GROUP BY/HAVING
Table
orders(id, customer_id, total)withtotalNOT NULL. Find the customers who placed at least three orders and whose average order total is strictly greater than 50. Report the customer id, the number of orders and the average rounded to two decimal places. - TheoryJuniorAggregates & GROUP BY/HAVING
In plain words, what does
GROUP BYdo to a result set, and what is the difference betweenCOUNT(*)andCOUNT(some_column)? - TheoryJuniorAggregates & GROUP BY/HAVING
When would you filter in
WHEREand when inHAVING? Can a query use both in the same statement? - TheoryJuniorAggregates & GROUP BY/HAVING
Why does PostgreSQL reject
SELECT customer_id, name, SUM(total) FROM orders JOIN customers ... GROUP BY customer_idin some cases and accept it in others? What is going on? - CodingJuniorCTEs
Table
orders(id, customer_id, total)withtotalNOT NULL. Compute how much each customer spent in total, then return only the customers whose total is strictly greater than the average customer total (the average over customers, not over orders).