Querydrill

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.

0 of 11 solvedStart session 1

Session 1: Text and dates

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

Session 4: One row per group, and the missing rows

Other plans and cases

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.