← Learn SQL

CASE — SQL's If/Then for Labels & Buckets

Label every row with CASE, put the tests in the right order and always write the ELSE. Then use CASE inside SUM to count shipped, cancelled and open orders side by side, and work out what percentage of orders were cancelled.

intermediate#sql#intermediate#case#case-when#conditional-aggregation#percentages#integer-division#coalesce

This article is part of the Learn SQL hands-on course series.

Prerequisite — this is a hands-on lesson. Reading it is not enough. Run every query yourself in CloudBeaver, against the Northwind Company sample database on PostgreSQL — and solve the koans yourself on DuckDB, the same Northwind Company data.

Not set up yet? It only takes a couple of minutes:

See here how to connect with CloudBeaver, and how to run the koans

A label for every order

Mnemosyne, questioning
Mnemosyne

Your invoicing question is still open, Leo — we'll get there. First, a job from the shipping team: they want every order labelled light, medium or heavy — freight under 25 is light, under 60 is medium, anything more is heavy. How would you do this?

Leo, thinking
Leo

In the last series I'd have run three queries, one WHERE per band, and put the answers together by hand. And even then I'd have three counts — not a label on each order.

Mnemosyne, pointing-right
Mnemosyne

CASE is SQL's if-then. It checks each WHEN from the top: under 25 is light, under 60 is medium, and ELSE catches everything left over. One new column, worked out row by row.

SELECT "OrderID", "Freight",
       CASE
         WHEN "Freight" < 25 THEN 'light'
         WHEN "Freight" < 60 THEN 'medium'
         ELSE 'heavy'
       END AS "Band"
FROM "Orders"
ORDER BY "OrderID"
LIMIT 6;
Leo, thinking
Leo

So it's an expression, like the ones from the aliases lesson — it just chooses its value instead of calculating it.

OrderID
Freight
Band
1
38.4
medium
2
114.85
heavy
3
37.25
medium
4
29.15
medium
5
18.58
light
6
18.43
light
Leo, questioning
Leo

Order 1 is on 38.4 — so it's medium, because it isn't under 25.

one order, one question at a time

TestGives
WHEN"Freight" < 25'light'
WHEN"Freight" < 60'medium'
ELSE'heavy'

The first yes wins. Everything under it is never even asked.

Mnemosyne, pointing-right
Mnemosyne

Right — and look how it got there. The first WHEN asked "under 25?" — no. The second asked "under 60?" — yes, so CASE stopped and answered medium. Order 5 stopped at the very first one, and order 2, on 114.85, fell through both to the ELSE.

Leo, questioning
Leo

So it reads top to bottom and takes the first yes.

Mnemosyne, talking
Mnemosyne

Now you want only the heavy ones — so you reach for the label you just made. And CloudBeaver refuses it.

SELECT "OrderID",
       CASE
         WHEN "Freight" > 50 THEN 'heavy'
       END AS "Label"
FROM "Orders"
WHERE "Label" = 'heavy';
SQL Error [42703]: ERROR: column "Label" does not exist
  Position: 115
Leo, questioning
Leo

But the label is right there — I just wrote it. Why won't the WHERE see it?

Mnemosyne, talking
Mnemosyne

Because of the running order from the aliases lesson. The database reads FROM and WHERE before it has run the SELECT — so at the moment the WHERE looks, "Label" does not exist yet. The name is given later, in the part that runs last.

Leo, questioning
Leo

So I build the label where the WHERE can reach it — first, in a subquery — and filter outside.

Count the bands, in the order you want

Mnemosyne, questioning
Mnemosyne

Now count them. The same CASE, and GROUP BY "Band" makes one group per label it produced — sorted by the smallest freight in each band, so light comes first.

SELECT CASE
         WHEN "Freight" < 25 THEN 'light'
         WHEN "Freight" < 60 THEN 'medium'
         ELSE 'heavy'
       END AS "Band",
       count(*) AS "Orders"
FROM "Orders"
GROUP BY "Band"
ORDER BY min("Freight");
Leo, excited
Leo

Wait, Mnemo — the WHERE couldn't see my label. So how can the GROUP BY?

Mnemosyne, questioning
Mnemosyne

Good eye, Leo. GROUP BY runs before the SELECT too — PostgreSQL just lets it borrow the name, as a favour. SQL Server doesn't: there you repeat the whole CASE, or build the label in a subquery.

Band
Orders
light
2334
medium
3941
heavy
3725
Mnemosyne, proud
Mnemosyne

All 10000 orders, three bands: 2334 light, 3941 medium, 3725 heavy. That's the whole report the shipping team asked for.

Leo, smiling
Leo

One query over five years of orders, and I never wrote a band twice. I'd have spent the afternoon on that in the last series.

Mnemosyne, pointing-right
Mnemosyne

The shipping team wants the heaviest freight first. A plain ORDER BY "Band" won't do it — alphabetically it's heavy, light, medium, with light stuck in the middle. So write the order you want, as a CASE.

SELECT "Band", count(*) AS "Orders"
FROM (SELECT CASE
               WHEN "Freight" < 25 THEN 'light'
               WHEN "Freight" < 60 THEN 'medium'
               ELSE 'heavy' END AS "Band"
      FROM "Orders") AS t
GROUP BY "Band"
ORDER BY CASE WHEN "Band" = 'heavy' THEN 1
              WHEN "Band" = 'medium' THEN 2 ELSE 3 END;
Band
Orders
heavy
3725
medium
3941
light
2334
Leo, smiling
Leo

ORDER BY a CASE that gives each band a number — heavy 1, medium 2, light 3. And the label is built in a subquery first, so that CASE can reach it.

The query on the left: before — no subquery, simpler ORDER BY

SELECT CASE
         WHEN "Freight" < 25 THEN 'light'
         WHEN "Freight" < 60 THEN 'medium'
         ELSE 'heavy'
       END AS "Band",
       count(*) AS "Orders"
FROM "Orders"
GROUP BY "Band"
ORDER BY min("Freight");  -- DESC reverses the order

The query on the right: now — subquery + ORDER BY CASE: general, ANSI

SELECT "Band", count(*) AS "Orders"
FROM (SELECT CASE
               WHEN "Freight" < 25 THEN 'light'
               WHEN "Freight" < 60 THEN 'medium'
               ELSE 'heavy' END AS "Band"
      FROM "Orders") AS t
GROUP BY "Band"
ORDER BY CASE WHEN "Band" = 'heavy' THEN 1
              WHEN "Band" = 'medium' THEN 2 ELSE 3 END;
Leo, questioning
Leo

Hold on, Mnemosyne — wait. The query on the left looks simpler: it sorts light first, by min("Freight"). Heavy first is just that order, reversed. Wouldn't adding DESC there do it?

Mnemosyne, proud
Mnemosyne

Yes — and I'm glad you saw it. DESC would work, for us. But that query leans on PostgreSQL's favour, GROUP BY "Band" — and SQL Server refuses it: invalid column name Band.

Leo, questioning
Leo

So it only works where PostgreSQL does me the favour. Is that the only catch?

Mnemosyne, proud
Mnemosyne

No — and the second one is easy to miss: it isn't explicit. We want to sort by Band, but we sort by the minimum freight — a trick that works only because freight happens to run in the same order as the bands. If the team wanted medium first, then heavy, then light, no column sorts that way.

Leo, questioning
Leo

So the query on the right is a bit longer — but it's standard, it sorts by Band itself, and it can give any order — medium, heavy, light too, if that's what they want.

The query on the leftThe query on the right
✓ it works on PostgreSQL, DuckDB, MySQL✓ standard: it runs on every engine
✗ not standard: SQL Server refuses it✓ explicit and general: it sorts by the label itself
✗ not explicit or general: needs a column already sorted

The first true WHEN wins

Mnemosyne, questioning
Mnemosyne

Those bands are right, Leo — and CASE will let you break them without a single warning. Swap the first two WHENs — medium above light.

SELECT CASE
         WHEN "Freight" < 60 THEN 'medium'
         WHEN "Freight" < 25 THEN 'light'
         ELSE 'heavy'
       END AS "Band",
       count(*) AS "Orders"
FROM "Orders"
GROUP BY "Band"
ORDER BY min("Freight");
Leo, surprised
Leo

Same two tests, same three labels. How can the order of the lines matter?

Band
Orders
medium
6275
heavy
3725
Mnemosyne, questioning
Mnemosyne

No light orders at all — and medium jumped from 3941 to 6275. Where do you think they went?

Leo, surprised
Leo

But the 2334 light orders are still in the table. Their freight didn't change.

the same two tests — in the other order

TestGivesCount
WHEN"Freight" < 60'medium'6275
WHEN"Freight" < 25'light'never reached
ELSE'heavy'3725

2334 light + 3941 medium = 6275. The narrow test is still there — nothing ever gets to it.

Mnemosyne, questioning
Mnemosyne

Under 25 is also under 60. The first WHEN now asks "under 60?", every light order says yes, and CASE stops right there. The light test is never even asked.

Leo, surprised
Leo

So the narrow test goes first. Otherwise a wide one catches everything before it gets a chance.

No ELSE means NULL

Mnemosyne, pointing-right
Mnemosyne

Here's a second trap — a quieter one. Finance only cares about heavy orders — freight over 50 — so you write one WHEN and no ELSE at all.

SELECT CASE
         WHEN "Freight" > 50 THEN 'heavy'
       END AS "Label",
       count(*) AS "Orders"
FROM "Orders"
GROUP BY "Label"
ORDER BY "Label" NULLS LAST;
Leo, thinking
Leo

Then every other order just has no label. That sounds harmless.

Label
Orders
heavy
4654
[NULL]
5346
Mnemosyne, pointing-right
Mnemosyne

4654 heavy — and 5346 orders labelled NULL.

Leo, thinking
Leo

NULL? I never wrote NULL anywhere.

one gate — and no floor under it

TestGivesCount
WHEN"Freight" > 50'heavy'4654
(no ELSE)NULL5346

Nothing catches 38.4. A CASE that runs out of WHENs hands back NULL.

Mnemosyne, pointing-right
Mnemosyne

You didn't have to, Leo. A CASE with no ELSE returns NULL for every row no WHEN matched.

Leo, thinking
Leo

The empty cells from the lesson on missing values. And those don't behave like a value.

Leo, surprised
Leo

Then I'll count the orders that are NOT heavy. The label lives in a subquery, so the WHERE can see it.

SELECT count(*) AS "Not heavy"
FROM (SELECT CASE
               WHEN "Freight" > 50
               THEN 'heavy'
             END AS "Label"
      FROM "Orders") AS t
WHERE "Label" <> 'heavy';
Not heavy
0
Mnemosyne, shrugging
Mnemosyne

Zero.

Leo, surprised
Leo

…Zero? Hang on, Mnemosyne — there are 5346 of them. We just counted them.

Mnemosyne, shrugging
Mnemosyne

And every one of them is labelled NULL. NULL isn't unequal to 'heavy' — the comparison is unknown, and WHERE keeps only true. So all 5346 fell straight through.

Mnemosyne, proud
Mnemosyne

So write the ELSE. Give every other order a real label — here, 'standard' — and the same filter finds 5346.

SELECT count(*) AS "Not heavy"
FROM (SELECT CASE
               WHEN "Freight" > 50
               THEN 'heavy'
               ELSE 'standard'
             END AS "Label"
      FROM "Orders") AS t
WHERE "Label" <> 'heavy';
Not heavy
5346

the same gate — with a floor

TestGivesCount
WHEN"Freight" > 50'heavy'4654
ELSE'standard'5346

One line, and the hole is a rung. The 5346 come back with a label on them.

Leo, excited
Leo

One line. And the orders I couldn't see come straight back.

Leo, questioning
Leo

Could the ELSE just be 0, Mnemosyne? A number for 'not heavy'?

SELECT "OrderID",
       CASE
         WHEN "Freight" > 50 THEN 'heavy'
         ELSE 0
       END AS "Label"
FROM "Orders";
SQL Error [22P02]: ERROR: invalid input syntax for type integer: "heavy"
  Position: 65
Mnemosyne, talking
Mnemosyne

Try it — CloudBeaver refuses. The 0 made PostgreSQL decide the whole column is a number, so now 'heavy' is the line it can't read.

Leo, questioning
Leo

So a CASE gives back one column, with one type — every THEN and the ELSE have to agree. Words with words, numbers with numbers.

Mnemosyne, talking
Mnemosyne

Exactly, Leo. That's why the ELSE said 'standard', not 0.

The short form, and when to join instead

Mnemosyne, questioning
Mnemosyne

CASE has a shorter form, too. When every WHEN compares the same column with a value, write the column once: CASE "ShipVia" WHEN 1 THEN 'Speedy Express' and so on. Which couriers go in, Leo?

SELECT CASE "ShipVia"
         WHEN 1 THEN 'Speedy Express'
         WHEN 2 THEN 'United Package'
         WHEN 3 THEN 'Federal Shipping'
       END AS "Shipper",
       count(*) AS "Orders"
FROM "Orders"
GROUP BY "Shipper"
ORDER BY "Shipper";
Leo, proud
Leo

Speedy Express, United Package, Federal Shipping — the three from the first series.

Shipper
Orders
Federal Shipping
831
Speedy Express
7513
United Package
1145
[NULL]
511
Leo, surprised
Leo

511 orders with no courier? And no error.

Mnemosyne, questioning
Mnemosyne

Northwind Company has a fourth courier: ShipVia 4, Northern Freight Lines. Your CASE didn't name it, and there's no ELSE — so its orders came back NULL.

Leo, surprised
Leo

But the courier names are already in a table, Mnemosyne. Wouldn't I just join to "Shippers"?

JOIN "Shippers" ON "ShipperID" = "ShipVia" — every courier, by its stored name
Shipper
Orders
Federal Shipping
831
Northern Freight Lines
511
Speedy Express
7513
United Package
1145
Mnemosyne, proud
Mnemosyne

Yes — join. All four couriers, the 511 included. And when a courier is added or renamed, the table changes and your query doesn't have to.

Leo, thinking
Leo

So CASE is for labels nobody stored — like light, medium and heavy.

Mnemosyne, proud
Mnemosyne

Exactly. If the label already lives in a table, read it from the table.

COALESCE is a CASE

Mnemosyne, smiling
Mnemosyne

And you've already written one without knowing it. In the lesson on missing values you used COALESCE — it's a CASE: when the value is NULL, use the stand-in, otherwise use the value.

SELECT "CompanyName", "Country",
       COALESCE("Region", '(none)')
         AS "With COALESCE",
       CASE
         WHEN "Region" IS NULL THEN '(none)'
         ELSE "Region"
       END AS "With CASE"
FROM "Customers"
WHERE "Country" IN ('Canada', 'Poland')
ORDER BY "CustomerID";
CompanyName
Country
With COALESCE
With CASE
Cedar Grocers
Poland
(none)
(none)
Emerald Fine Foods
Canada
BC
BC
Golden Supermarket
Canada
BC
BC
Kestrel Provisions
Poland
(none)
(none)
Pampas Foods
Canada
BC
BC
Tundra Foods
Canada
QC
QC
Upland Trading
Canada
QC
QC
Zenith Food Hall
Poland
(none)
(none)
Leo, proud
Leo

Identical columns — so COALESCE is that CASE, abbreviated. But why not the short form here, Mnemo: CASE "Region" WHEN NULL?

Mnemosyne, smiling
Mnemosyne

Because the short form tests with =, and = NULL is never true — not even for a NULL. That WHEN would never fire, and Poland would come back NULL. For NULL, always write IS NULL.

Your turn, Leo.

Label every order: online, or offline.

Web and EDI orders are online. You should get 6234 offline and 3766 online.

▶ Continue once you've run it.

SELECT CASE WHEN "Channel" IN (___) THEN 'online'
            ELSE 'offline' END AS "Channel type",
       count(*) AS "Orders"
FROM "Orders" GROUP BY "Channel type";

CASE inside SUM: three counts side by side

Mnemosyne, excited
Mnemosyne

Here's the trick that makes CASE worth the whole lesson: count shipped, cancelled and open orders side by side. Every order has a Status, one of those three. For each sales channel: how many orders, and how many of each — one row per channel.

SELECT "Channel",
       count(*) AS "Orders",
       SUM(CASE WHEN "Status" = 'Shipped'
           THEN 1 ELSE 0 END) AS "Shipped",
       SUM(CASE WHEN "Status" = 'Cancelled'
           THEN 1 ELSE 0 END) AS "Cancelled",
       SUM(CASE WHEN "Status" = 'Open'
           THEN 1 ELSE 0 END) AS "Open"
FROM "Orders"
GROUP BY "Channel"
ORDER BY "Orders" DESC;
Leo, questioning
Leo

Three counts with three different WHEREs… but one query only gets one WHERE.

Channel
Orders
Shipped
Cancelled
Open
Sales rep
3749
3669
61
19
Web
2765
2669
50
46
Phone
2485
2413
42
30
EDI
1001
969
22
10
Mnemosyne, proud
Mnemosyne

Sales rep: 3749 orders — 3669 shipped, 61 cancelled, 19 open. Every channel on one row.

Leo, excited
Leo

But how did SUM count anything, Mnemo? It adds numbers.

Mnemosyne, proud
Mnemosyne

Each CASE turns an order in its status into a 1, and every other order into a 0. SUM adds the ones. That's a count — without a WHERE.

Leo, excited
Leo

And the three columns add back up to the orders. One column per status, all in the same GROUP BY.

Mnemosyne, questioning
Mnemosyne

You'll also see it written with count — and this time the missing ELSE is on purpose. Every other order gets NULL, count skips NULLs, and Sales rep still says 61 cancelled.

SELECT "Channel",
       count(*) AS "Orders",
       count(CASE WHEN "Status" = 'Shipped'
             THEN 1 END) AS "Shipped",
       count(CASE WHEN "Status" = 'Cancelled'
             THEN 1 END) AS "Cancelled",
       count(CASE WHEN "Status" = 'Open'
             THEN 1 END) AS "Open"
FROM "Orders"
GROUP BY "Channel"
ORDER BY "Orders" DESC;
Leo, thinking
Leo

So there, the missing ELSE was the trap — and here, it's the trick. It depends on whether I meant the NULLs.

Cancelled %: a division that comes back as 0

Mnemosyne, questioning
Mnemosyne

Sales rep has the most cancelled orders — and by far the most orders. So the fair comparison is a percentage: the cancelled count divided by count(*). What do you expect for Sales rep?

SELECT "Channel",
       count(*) AS "Orders",
       SUM(CASE WHEN "Status" = 'Cancelled'
           THEN 1 ELSE 0 END) AS "Cancelled",
       SUM(CASE WHEN "Status" = 'Cancelled'
           THEN 1 ELSE 0 END) / count(*) AS "Share"
FROM "Orders"
GROUP BY "Channel"
ORDER BY "Orders" DESC;
Leo, thinking
Leo

61 out of 3749. Small — but not nothing.

What CloudBeaver shows — PostgreSQL
Channel
Orders
Cancelled
Share
Sales rep
3749
61
0
Web
2765
50
0
Phone
2485
42
0
EDI
1001
22
0
Leo, surprised
Leo

Zero? Sales rep has 61 cancelled right there in the next column. That isn't zero.

Mnemosyne, shrugging
Mnemosyne

On PostgreSQL — what CloudBeaver runs — a whole number divided by a whole number is a whole number. 61 ÷ 3749 is 0.016 and a bit, and the fraction is thrown away. Every channel: 0. No error.

Leo, surprised
Leo

And if I'd tried it in DuckDB first, Mnemo?

Mnemosyne, shrugging
Mnemosyne

0.016 and a string of digits, for Sales rep. DuckDB keeps the fraction. Same query, two answers — so write it the way that's right on both.

Mnemosyne, proud
Mnemosyne

Multiply by 100.0 before you divide. The decimal point makes the whole calculation decimal, on every database — and ROUND to one place gives a percentage a person can read.

SELECT "Channel",
       count(*) AS "Orders",
       SUM(CASE WHEN "Status" = 'Cancelled'
           THEN 1 ELSE 0 END) AS "Cancelled",
       ROUND(100.0 * SUM(CASE WHEN "Status" = 'Cancelled'
             THEN 1 ELSE 0 END) / count(*), 1) AS "Cancelled %"
FROM "Orders"
GROUP BY "Channel"
ORDER BY "Orders" DESC;
Leo, excited
Leo

So the .0 is doing the real work. And multiplying first — before the division gets a chance to throw anything away.

The whole company: 175 of 10000 cancelled — 1.8
Channel
Orders
Cancelled
Cancelled %
Sales rep
3749
61
1.6
Web
2765
50
1.8
Phone
2485
42
1.7
EDI
1001
22
2.2
Leo, questioning
Leo

Real percentages. And look — Sales rep had the most cancellations, and the lowest percentage: 1.6.

Mnemosyne, questioning
Mnemosyne

And EDI had the fewest, 22 — and the highest percentage, 2.2. The count said one channel; the percentage says the other.

Leo, questioning
Leo

So EDI is where orders get cancelled.

Mnemosyne, questioning
Mnemosyne

Careful, Leo — read it against the whole company first: 1.8. EDI is 22 of 1001; four fewer and it reads 1.8 too. A percentage always goes next to its count.

Case file: one courier stands out

Mnemosyne, questioning
Mnemosyne

And now your question, Leo — the one from the subqueries lesson: in 2024, orders grew and invoicing fell. An order is invoiced once its ship date is recorded. So per courier, for 2024: how many orders are marked Shipped but have no ship date?

SELECT s."CompanyName" AS "Courier",
       count(*) AS "Orders",
       SUM(CASE WHEN o."Status" = 'Shipped' AND o."ShippedDate" IS NULL
           THEN 1 ELSE 0 END) AS "Shipped, no date",
       ROUND(100.0 * SUM(CASE WHEN o."Status" = 'Shipped' AND o."ShippedDate" IS NULL
             THEN 1 ELSE 0 END) / count(*), 1) AS "Share %"
FROM "Orders" o
JOIN "Shippers" s ON s."ShipperID" = o."ShipVia"
WHERE o."OrderDate" >= DATE '2024-01-01'
  AND o."OrderDate" < DATE '2025-01-01'
GROUP BY s."CompanyName"
ORDER BY s."CompanyName";
Leo, thinking
Leo

Two tests in one WHEN, and the percentage beside its count. Nothing I haven't just written.

Courier
Orders
Shipped, no date
Share %
Federal Shipping
216
0
0
Northern Freight Lines
125
0
0
Speedy Express
1768
178
10.1
United Package
289
0
0
Leo, surprised
Leo

Speedy Express: 178 of its 1768 orders are marked Shipped with no ship date. 10.1.

Mnemosyne, questioning
Mnemosyne

And the other three couriers: zero. Not one of their 630 orders.

Leo, surprised
Leo

So it isn't everywhere. It's one courier.

Mnemosyne, questioning
Mnemosyne

That's what the columns say — and all they say. Not why, and not whether those orders arrived. SQL tells us where to look; people confirm it.

What to take away, and what is next

Mnemosyne, questioning
Mnemosyne

Four rules, Leo. The first true WHEN wins, so put the narrow tests first. Write the ELSE, unless you mean the NULLs. The WHERE can't see a label the SELECT made — build it in a subquery. And a CASE that gives 1 or 0, inside a SUM, counts each status in its own column.

Leo, smiling
Leo

The first two stop a report that's quietly wrong. The third gets me past the first error everybody hits — and the fourth builds the report everybody asks for.

Your turn, Leo.

▶ Walk the path to enlightenment.

cd c:/DataPallas/db/datazeus
.\zeus.bat koans learnsql series2 _05
  • a label for every product: finish the CASE
  • the first true WHEN wins: write the tests in the right order
  • no ELSE means NULL: count only what the CASE labelled
  • …and nine more, ending with two you write yourself

…/learnsql/series2/_05/CaseExpressionsKoans.groovy

Mnemosyne, excited
Mnemosyne

That was CASE, Leo. A label worked out row by row: the first true WHEN wins, a row nobody matched gets NULL, and inside a SUM it counts any status you name, side by side. Next lesson: dates and times.

Leo, excited
Leo

Good — now we know which courier has the orders marked Shipped with no ship date: Speedy Express. Next I want to know since when it started.

That's all, folks!

  1. Subqueries — one query's result inside another
  2. CASE — SQL's if/then for labels & buckets (this lesson)
  3. Dates & Times — ranges and boundaries
  4. Multi-Table JOINs & Grain — rows dropped or multiplied
  5. CTEs (WITH) — a long query, in named steps
  6. VIEWs, CREATE TABLE AS & Temp Tables — query or answer
  7. EXISTS & Correlated Subqueries — and the NOT IN trap
  8. Self-JOINs & Hierarchies — rows in the same table
  9. Window Functions I: OVER, PARTITION BY & ROW_NUMBER
  10. String Functions, LIKE & Regex — clean up messy data
  11. Window Functions II: LAG/LEAD, Running Totals & Frames
  12. UNION, INTERSECT, EXCEPT — stack and reconcile lists
  13. GROUPING SETS, ROLLUP & CUBE — subtotals in one pass
  14. INSERT, UPDATE, DELETE & Transactions — safely
  15. UPSERT & MERGE — insert or update, safely re-run
  16. Median and Percentiles — WITHIN GROUP, not AVG
  17. Subqueries + CTEs + Window Functions — a real case

Found this to be a little gem? Help others find it too — someone out there is stuck trying to learn SQL right now. The button opens Reddit with the link and title already filled in, so you just pick a community and post.

Share on Reddit