What happens to a repeated row when two query results overlap? Explore that question in a browser, then practice SQL on a six-table retail database in PostgreSQL.
Download the interactive guide · Browse PostgreSQL exercises · Explore the topic index
The guide starts with A = 1, 1, 2, 3 and B = 2, 3, 3, 4. Before opening it, try predicting how many rows each operator returns.
| Operation | Result rows, shown in ascending order | What to look for |
|---|---|---|
UNION |
1, 2, 3, 4 |
Each value appears once. |
UNION ALL |
1, 1, 2, 2, 3, 3, 3, 4 |
All eight input rows survive. |
INTERSECT |
2, 3 |
Both inputs contain these values. |
EXCEPT (A minus B) |
1 |
Only A contains 1. Its two copies produce one row. |
Open the downloaded HTML file in your browser. Add a second 4 to B and switch among the four operators. Which result changes? The guide also includes JOIN diagrams, SQL examples, and interview revision questions.
Use PostgreSQL 12 or later and connect to a disposable practice database in pgAdmin or psql. The setup script drops and recreates practice tables when you run it again.
| Step | File | How to use it |
|---|---|---|
| 1 · Set up the shop | Retail database setup | Create and populate customers, categories, employees, products, orders, and order items. |
| 2 · Run a query | Worked SQL examples | Run the opening SET search_path in your session, then select individual examples to try. |
| 3 · Pick a topic | SQL topic index | Browse the numbered outline to find a concept to revisit. This file contains study notes. |
After setup, use this first query to find the cities with the most customers. The second sort column gives cities with equal counts a consistent order.
SELECT city, COUNT(*) AS customer_count
FROM retail_store_practice.customers
GROUP BY city
ORDER BY customer_count DESC, city;MIT