godz.online
Back to tools

TLDR reference

SQL cheatsheet

A searchable reference for the SQL you write most - joins, filtering, aggregation, and common query patterns - each with a copyable snippet. Type to search, or filter by group. Everything runs in your browser.

33 shown

How it works

A searchable SQL cheatsheet covering the queries you write most: selecting and filtering rows, joining tables, grouping and aggregating, modifying data, and shaping a schema. Each card pairs a copyable snippet with a plain-language explanation of what it does and when to reach for it. Type to search by keyword - "join", "group by", "upsert", "index" - or use the chips to browse a single group such as joins or aggregation.

The syntax sticks to standard SQL that works across PostgreSQL, MySQL, SQLite, and SQL Server, with a note where a dialect differs. It walks through the join types that trip people up - inner, left, right, and full outer - alongside the common patterns for counting, ranking, deduplicating, and updating rows safely. Everything is static and runs in your browser, so the lookup is instant and works offline once the page has loaded.

Example. Searching "join" lines up the four kinds: an INNER JOIN keeps only rows that match in both tables, a LEFT JOIN keeps every row from the left table and fills in NULLs where the right has no match, and a FULL OUTER JOIN keeps unmatched rows from both sides. Filtering by the Aggregation chip groups COUNT, GROUP BY, and HAVING together.

FAQ

What is the difference between an INNER JOIN and a LEFT JOIN?

An INNER JOIN returns only the rows that have a match in both tables - if a customer has no orders, that customer simply does not appear. A LEFT JOIN (short for LEFT OUTER JOIN) returns every row from the left table regardless, and fills the right-hand columns with NULL where there is no match, so the customer with no orders still shows up with empty order fields. Reach for a LEFT JOIN whenever "include everything from this table, even without a match" is the question, such as finding customers who have never ordered.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before they are grouped, while HAVING filters the groups after aggregation. Because an aggregate like COUNT(*) or SUM(total) does not exist until the rows are grouped, you cannot reference it in WHERE - you put that condition in HAVING instead. A typical query uses both: WHERE narrows the raw rows (say, orders from this year), GROUP BY collapses them per customer, and HAVING keeps only the groups that meet an aggregate test (say, more than five orders).

How do I remove duplicate rows from a result?

For a quick distinct list, SELECT DISTINCT collapses identical rows in the output. When you need to keep one row per group based on some ordering - the latest order per customer, say - a window function is the standard tool: number the rows with ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC) in a subquery, then keep only the rows where that number equals 1. To delete duplicates from the table itself, the same row-numbering trick identifies which copies to remove.

Why should I always pair UPDATE and DELETE with a WHERE clause?

Because without a WHERE clause they apply to every row in the table. An UPDATE with no WHERE rewrites the column for all rows, and a DELETE with no WHERE empties the table entirely - both run instantly and both are easy to trigger by accident. The safe habit is to write the WHERE clause first, test it with a SELECT to confirm it matches exactly the rows you mean, and only then change the SELECT into an UPDATE or DELETE. Running inside a transaction gives you a ROLLBACK if something still goes wrong.