sqlstreak

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

nameprice
Margherita11
Veggie Nebula13
Pepperoni Rocket13.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

categoryorder_linesrevenue
pizza17320.5
side761
drink624.5
dessert219.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

categoryvegetariannot_vegetarian
dessert10
drink20
pizza32
side12

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_idnamequantityprice
1Lemon Soda23
1Margherita211
2Pepperoni Rocket113.5
2Wings18.5
3Cola12.5
3Four Cheese Orbit114
3Garlic Knots15
4BBQ Meteor215.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

namepriceband
BBQ Meteor15.5premium
Four Cheese Orbit14premium
Pepperoni Rocket13.5premium
Veggie Nebula13premium
Margherita11regular
Wings8.5regular
Caesar Salad7regular
Tiramisu6.5regular
Garlic Knots5small
Lemon Soda3small
Cola2.5small

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

  1. 01Your first SELECTFirst queries · free
  2. 02Filter with WHEREFirst queries · free
  3. 03Sort and limitFirst queries · free
  4. 04Count, sum, averageCounting things · Pro
  5. 05GROUP BYCounting things · Pro
  6. 06Your first JOINJoining tables · Pro
  7. 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.

Start free