SQL for Excel users: what you already know, in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
If you've built a pivot table or a VLOOKUP, you already think in SQL; you just haven't written it yet. Each section below takes something you do in a spreadsheet and shows the same thing as a query on a small pizza shop's tables, run on a real database.
A filter is WHERE
In a spreadsheet: Filter the category column to “pizza”, and price to less than 14
Instead of clicking a filter, you write the condition. It doesn't hide rows; it returns only the ones that match.
SELECT name, price
FROM menu_items
WHERE category = 'pizza' AND price < 14
ORDER BY price;Result (PostgreSQL and SQLite, identical): 3 rows
| name | price |
|---|---|
| Margherita | 11 |
| Veggie Nebula | 13 |
| Pepperoni Rocket | 13.5 |
Try it: PostgreSQL playground · SQLite playground
SUMIF and COUNTIF are GROUP BY
In a spreadsheet: =SUMIF(category, "pizza", revenue) for each category
GROUP BY makes one row per category and the aggregate adds up each group, so you get every category's total at once instead of one formula per category.
SELECT m.category,
COUNT(*) AS order_lines,
SUM(oi.quantity * m.price) AS revenue
FROM order_items oi
JOIN menu_items m ON m.id = oi.item_id
GROUP BY m.category
ORDER BY revenue DESC;Result (PostgreSQL and SQLite, identical): 4 rows
| category | order_lines | revenue |
|---|---|---|
| pizza | 17 | 320.5 |
| side | 7 | 61 |
| drink | 6 | 24.5 |
| dessert | 2 | 19.5 |
Try it: PostgreSQL playground · SQLite playground
A pivot table is a GROUP BY with conditional sums
In a spreadsheet: Pivot table: categories as rows, vegetarian yes/no as columns, count of items
One aggregate per column, each with its own condition. The rows come from GROUP BY.
SELECT category,
SUM(CASE WHEN vegetarian THEN 1 ELSE 0 END) AS vegetarian,
SUM(CASE WHEN NOT vegetarian THEN 1 ELSE 0 END) AS not_vegetarian
FROM menu_items
GROUP BY category
ORDER BY category;Result (PostgreSQL and SQLite, identical): 4 rows
| category | vegetarian | not_vegetarian |
|---|---|---|
| dessert | 1 | 0 |
| drink | 2 | 0 |
| pizza | 3 | 2 |
| side | 1 | 2 |
Try it: PostgreSQL playground · SQLite playground
VLOOKUP is a JOIN
In a spreadsheet: =VLOOKUP(item_id, menu!A:C, 2, FALSE) to fetch each item's name
A JOIN looks up the matching row in another table for every row at once, and you can bring back as many columns as you like.
SELECT oi.order_id, m.name, oi.quantity, m.price
FROM order_items oi
JOIN menu_items m ON m.id = oi.item_id
ORDER BY oi.order_id, m.name
LIMIT 8;Result (PostgreSQL and SQLite, identical): 8 rows
| order_id | name | quantity | price |
|---|---|---|---|
| 1 | Lemon Soda | 2 | 3 |
| 1 | Margherita | 2 | 11 |
| 2 | Pepperoni Rocket | 1 | 13.5 |
| 2 | Wings | 1 | 8.5 |
| 3 | Cola | 1 | 2.5 |
| 3 | Four Cheese Orbit | 1 | 14 |
| 3 | Garlic Knots | 1 | 5 |
| 4 | BBQ Meteor | 2 | 15.5 |
Try it: PostgreSQL playground · SQLite playground
IF is CASE WHEN
In a spreadsheet: =IF(price >= 13, "premium", "regular")
CASE checks conditions in order and returns the first match; ELSE covers the rest.
SELECT name, price,
CASE WHEN price >= 13 THEN 'premium'
WHEN price >= 6 THEN 'regular'
ELSE 'small' END AS band
FROM menu_items
ORDER BY price DESC;Result (PostgreSQL and SQLite, identical): 11 rows
| name | price | band |
|---|---|---|
| BBQ Meteor | 15.5 | premium |
| Four Cheese Orbit | 14 | premium |
| Pepperoni Rocket | 13.5 | premium |
| Veggie Nebula | 13 | premium |
| Margherita | 11 | regular |
| Wings | 8.5 | regular |
| Caesar Salad | 7 | regular |
| Tiramisu | 6.5 | regular |
| Garlic Knots | 5 | small |
| Lemon Soda | 3 | small |
| Cola | 2.5 | small |
Try it: PostgreSQL playground · SQLite playground
Remove duplicates is DISTINCT
In a spreadsheet: Data → Remove duplicates on the city column
DISTINCT returns each different value once, without changing the table.
SELECT DISTINCT city
FROM customers
ORDER BY city;Result (PostgreSQL and SQLite, identical): 4 rows
| city |
|---|
| Austin |
| Boston |
| Chicago |
| Denver |
Try it: PostgreSQL playground · SQLite playground
Try it on your own data: the CSV tool turns a spreadsheet export into a table you can query, without uploading it. Prefer to stay in spreadsheets for now? Our sister site formulastreak teaches Excel and Sheets formulas the same way.
The lessons that teach this
- 01Your first SELECTFirst queries · free
- 02Filter with WHEREFirst queries · free
- 03Sort and limitFirst queries · free
- 04Count, sum, averageCounting things · Pro
- 05GROUP BYCounting things · Pro
- 06Your first JOINJoining tables · Pro
- 07Shape your resultsSharper filters · free
Keep going
Write your first query in about a minute
Five-minute lessons on a real database, with instant feedback. The first units are free, no account needed.