Skip to content
StackDecode

SQL Cheat Sheet

Copy-ready SQL queries from SELECT to window functions, on a small student database. Reference only; nothing is executed.

NeweasyRuns in your browser

How it works

SQL is a declarative language: you describe the rows you want, not how to fetch them. A query is read in this logical order, which explains most beginner errors:

  1. FROM / JOIN: choose and combine tables
  2. WHERE: keep matching rows
  3. GROUP BY then HAVING: group rows and filter groups
  4. SELECT: compute the output columns (so aliases defined here can’t be used in WHERE)
  5. ORDER BY then LIMIT: sort and trim
  • All examples use the four-table student database above, so you can see how tables relate through student_id and module_id.
  • Always test UPDATE and DELETE by running the same WHERE in a SELECT first.