Saved

Press the star next to a tool's name to save it.

All my tools

SQL Cheat Sheet

This sql cheat sheet lists the statements you use most: SELECT, WHERE, ORDER BY, GROUP BY, joins, UNION, INSERT, UPDATE and DELETE. Search it, filter by topic or print it.

Restored your last values ·

Your search stays in your browser. Nothing is uploaded.

Commands

–

No commands match that search.

Recent results

This sql cheat sheet lists the statements for reading and changing data in a relational database. The order of the clauses in a query follows the SELECT page of the PostgreSQL manual. [1] SQL differs a little between databases, so check your own system's documentation for details such as LIMIT, quoting and column types.

How to use the sql cheat sheet

  1. Type in the search box to filter by any word in a name, a command or its note.
  2. Choose a topic to see one group instead of the whole list.
  3. Read the command in the right-hand column and its meaning on the left.
  4. Press Print for a clean copy of whatever is shown.

What is in each topic

The sql cheat sheet is split into 5 topics. The table shows how many entries each topic holds and which ones they are, so you can jump straight to the right group with the topic box.

TopicEntriesWhat it covers
Reading data7select columns; select everything; filter rows; sort rows; limit rows; remove duplicates; rename a column
Conditions5match a list; range; pattern; missing values; combine tests
Grouping and totals4count rows; total; group rows; filter groups
Joining tables6inner join; left join; right join; full join; cross join; combine results
Changing data6insert a row; update rows; delete rows; create a table; add a column; delete a table

Reading data with SELECT

A SELECT query is built from clauses in a fixed order: WITH, SELECT, FROM, WHERE, GROUP BY, HAVING, WINDOW, UNION, INTERSECT or EXCEPT, ORDER BY, and then LIMIT, OFFSET or FETCH. [1] FROM names the source tables, WHERE filters individual rows, ORDER BY sorts the output and LIMIT returns only some rows. The manual advises pairing LIMIT with an ORDER BY that gives a unique order, otherwise the rows you get back are not predictable. [1]

Pattern tests with LIKE, ranges with BETWEEN, lists with IN and missing values with IS NULL are standard SQL conditions; they are listed here from our own working rather than from that page.

Grouping and totals

GROUP BY condenses rows that share the same values so that aggregates such as COUNT, SUM, AVG, MIN and MAX can be computed per group. HAVING then filters the groups. The manual puts the difference plainly: WHERE filters individual rows before GROUP BY is applied, while HAVING filters the group rows that GROUP BY created. [1]

A common mistake is to put an aggregate in a WHERE clause, which is not allowed because the groups do not exist yet. Move that test into HAVING.

Joins, UNION and changing data

A join combines two FROM items. An inner JOIN keeps only matching rows, LEFT JOIN keeps every row from the left table with nulls where nothing matches, RIGHT JOIN does the reverse, FULL JOIN keeps unmatched rows from both and CROSS JOIN pairs every row with every row. [1] UNION combines the results of two SELECT statements, and the result contains no duplicate rows unless you write ALL; both queries must have the same number of compatible columns. [1]

INSERT adds rows, UPDATE changes them and DELETE removes them. These statements are listed from our own working. Always include a WHERE clause on UPDATE and DELETE unless you really mean every row, and try the same WHERE in a SELECT first to see which rows it matches.

Who this page helps

Developers use a cheat sheet to look up a command they have not typed for a while, to learn the names of the options and to compare similar commands. Beginners can read down a topic to see what is possible, and experienced users can search for the one flag they forgot.

Tips for getting the most from the sql cheat sheet

  • Write a SELECT with your WHERE clause before you run an UPDATE or DELETE, so you can see which rows will change.
  • Name the columns you need instead of using a star in production queries.
  • Check how your database handles NULL, because comparisons with NULL are never simply true or false.

Common mistakes

Most problems come from running a command in the wrong place or with the wrong target. Read a command before you press Enter, replace every placeholder with your own value, and try destructive commands on a test copy first. When a command behaves differently from the note, check the version you have installed and the official documentation.

Where the entries come from

Each command was checked against the documentation listed below this article. Where a command is standard but is not shown on the page we read, the article says it is our own working. Options and defaults change between releases, so run the help command for the version you have installed before you rely on a flag.

What this page does not do

It lists the commands most people need and does not try to cover every option. For the complete syntax, error messages and advanced features, open the official documentation linked in the sources, and keep it beside the cheat sheet while you learn.

Printing and checking

Filter by topic before you print if you only need one section, and the printed page will be shorter. The notes are short on purpose, so keep your textbook or the official documentation open for the full detail and for anything that changes between versions.

What next

Use this often? Add SQL Cheat Sh to your Home screen.

Frequently asked questions

What is in this sql cheat sheet?

SELECT and its clauses, conditions, grouping and totals, the join types and UNION, and the statements that insert, update, delete and define tables.

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping. HAVING filters the groups that GROUP BY creates, so it can use aggregates such as SUM.

What does a LEFT JOIN do?

It keeps every row from the left table and fills the right table's columns with nulls where no row matches.

Does this work for every database?

The basics are shared, but LIMIT, quoting and data types vary between systems. Check your database's own manual for details.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools