PostgreSQL features in four sessions
Standard SQL gets you most of the way. These exercises cover the parts that are specific to PostgreSQL and that people who use it every day reach for: ILIKE, FILTER, date_trunc, ROLLUP, string_agg, DISTINCT ON, LATERAL and generate_series.
Eleven exercises on a real PostgreSQL database in your browser. Each one also accepts the portable way of writing the same query, so you see both.
Session 1: Text and dates
- Search without caring about case (ILIKE)Return the names of customers whose name contains the letter e, in upper or lower case. Chloe and Elif both count.
- Full years of service (age)Return each employee's name and the number of full years between their hire date and 1 January 2026.
- Paid revenue per week (date_trunc)Return the first day (Monday) of each week that has paid orders, as a date, together with the paid revenue of that week.
Session 2: Aggregates with more to say
- Several counts in one pass (FILTER)For each customer_id that has orders, return the customer_id, the number of orders, the number of paid orders and the total amount of the paid orders, in that order. A customer with no paid orders shows 0 for both.
- Orders per weekday (EXTRACT)Return the ISO weekday number (1 for Monday to 7 for Sunday) and the number of orders placed on that weekday, whatever the status. Only weekdays that have orders.
- Subtotals and a grand total (ROLLUP)Return the paid revenue per country and, in the same result, one extra row with the total over all countries. The total row shows NULL as the country.
Session 3: Lists and whole-group conditions
- Products on each order as one text (string_agg)For each order that has order lines, return the order_id and the names of its products as one text, separated by a comma and a space, in alphabetical order. A product listed twice on an order appears once.
- Customers whose every order is paid (bool_and)Return the names of customers who have at least one order and whose orders all have the status paid.
Session 4: One row per group, and the missing rows
- Latest order per customer (DISTINCT ON)For each customer, return the customer_id, the id and the amount of their most recent order (latest date, then highest id), whatever the status.
- Two largest paid orders per customer (LATERAL)For each customer with paid orders, return the customer's name and the id and amount of their two largest paid orders (highest amount first, then lowest id). A customer with one paid order returns one row.
- A month with no sales still shows up (generate_series)Return every month from January to June 2025 as text like 2025-01, with its paid revenue. A month without paid orders must still appear, with 0.
Other plans and cases
- 14-day SQL interview sprint (41)
- SQL for data analyst interviews (24)
- SQL for BI analyst interviews (21)
- SQL for data engineer interviews (24)
- Product analytics SQL in four sessions (12)
- SQL traps and time periods in three sessions (10)
- Window functions in six sessions (18)
- SQL on messy data: one work week (16)
- SQL business case: the refund review (5)
- SQL business case: the payroll review (5)
- SQL business case: the monthly revenue report (4)
- SQL business case: customer value and segments (4)
- SQL business case: product performance (4)
- All exercises by level
Your first 8 exercises are free, with no account, from any plan or level. Progress is kept in this browser; a free account keeps it on every device.