Four sample questions with answers
The tables and values are fictional. These are original practice questions, not questions from any employer’s test.
1. Revenue by month
The table orders has order_id, order_date, status and amount. Show paid revenue for each month, oldest first.
Answer:
SELECT DATE_TRUNC('month', order_date) AS month, SUM(amount) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY 1
ORDER BY 1;
The filter goes in WHERE, before grouping. A condition on the total, such as months over 10,000, would go in HAVING. Practice in GROUP BY.
2. Customers who never ordered
List customers with no orders.
Answer: SELECT c.customer_id FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id WHERE o.order_id IS NULL; An inner join would drop exactly the customers you are looking for. See LEFT JOIN.
3. A total that came out too high
Revenue looks inflated after joining orders to order_items. Why?
Answer: Each order now appears once per item, so SUM(o.amount) counts an order’s amount several times. Total the items (SUM(i.quantity * i.unit_price)), or aggregate the items in a WITH step before joining. Check the row count whenever a join could add rows.
4. The best seller in each category
Return the top product by revenue in each category.
Answer: Rank inside each category with ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC) and keep rank 1. If two products can tie, use RANK() and say which one you chose. See window functions.
How to practice before the test
- Take the free SQL skills test for your level.
- Work through the interview sets: basics, intermediate and advanced.
- Finish with the timed mock exam.
The best SQL practice sites comparison shows where other sites fit next to this.
ChatGPT teaches you. The Work Gym lets you practice.
