AI Hints
Home›SQL Tutorial›Recap and Roadmap
SQL · Lesson 124 of 126

Your SQL Cheat Sheet

Everything in one place.

⏱ 2 min read▶ Video at 2:56:56🎓 Free
Watch this lesson in the SQL courseWatch on YouTube ↗Full SQL course

Look how far you've come. At the start, you'd never written a query. Now you can join four tables, rank rows, build reports, clean messy data, and answer interview questions. Let's put everything on one page. Pause on any of these screens, and take a screenshot.

Your SQL Cheat Sheet
From the video: Your SQL Cheat Sheet
  • From zero
  • To reports and interviews
  • Everything on one page

Reading data

First, reading data. Select picks columns. From picks the table. Where filters rows. Order by sorts. Limit keeps the first few. And distinct removes repeats. These six words answered every question in the first hour.

ClauseWhat it does
SELECTpick columns
FROMpick the table
WHEREfilter rows
ORDER BYsort
LIMITkeep the first N
DISTINCTremove repeats

Summarising and joining

Next, summarising and joining. Count, sum, average, min, and max squash rows into numbers. Group by makes one answer per group. Having filters groups. Join and left join connect tables. And left join with is null finds what's missing.

ToolWhat it does
COUNT, SUM, AVGmany rows to one number
GROUP BYone answer per group
HAVINGfilter groups
JOINmatching rows only
LEFT JOINkeep every left row
LEFT JOIN + IS NULLfind what's missing

Power tools

And the power tools. Subqueries and CTEs break problems into steps. Case makes categories, and pivots. Window functions rank rows, and add running totals, without squashing. And lag compares each row with the one before.

ToolWhat it does
Subquery, CTEsteps with names
CASEcategories and pivots
ROW_NUMBER, RANKrank rows
SUM() OVERrunning totals
LAG, LEADprevious and next row
WITH RECURSIVEhierarchies, calendars

The mistakes to remember

And here are the mistakes this course warned you about. Forgetting the where on update or delete. Using equals null, instead of is null. Not in with a null in the list. Double counting after a join. Whole number division. And comparing text with the wrong capital letters. Each one runs without warning, so they're worth remembering.

MistakeThe fix
UPDATE / DELETE, no WHEREwrite WHERE first
= NULLIS NULL
NOT IN with a NULLNOT EXISTS
Double countingCOUNT(DISTINCT ...)
140 / 1598 = 0use 100.0
'fantasy' vs 'Fantasy'LOWER() both sides

The habits that matter

Finally, the habits that matter more than any keyword. Check every step before building the next one. Write the where before you update, or delete. Watch out for nulls. Check row counts after joins. And when something looks too good, be suspicious.

Check each stepbuild up slowly
WHERE firstbefore UPDATE, DELETE
Mind the NULLsIS NULL, NOT IN
Count rowsafter every join

Summary

That's the whole course on one page. Now, let's talk about what comes next.

Key takeaways
  • Read: SELECT ... WHERE
  • Summarise and join
  • CTEs and windows
Next up: Your Roadmap — what to learn next