SQL

SQL Syntax Cheat Sheet

Explore 36 SQL syntax references in a searchable, printable cheat sheet with category filters, copy buttons and bookmarks.

Published Updated 9 min read

SQL syntax cheat sheet with common SQL commands and query examples
Quick-reference SQL syntax cheat sheet covering queries, joins, filtering, aggregation and database commands.— ShortcutSearch.app
On This Page
  1. What Is SQL Syntax?
  2. SQL Syntax Cheat Sheet
  3. How to Use This SQL Cheat Sheet
  4. Basic SQL Query Syntax
  5. SELECT
  6. SELECT All Columns
  7. DISTINCT
  8. Filtering Data in SQL
  9. WHERE
  10. AND and OR
  11. IN
  12. BETWEEN
  13. LIKE
  14. Sorting and Limiting SQL Results
  15. ORDER BY
  16. LIMIT
  17. SQL JOIN Syntax
  18. INNER JOIN
  19. LEFT JOIN
  20. RIGHT JOIN
  21. FULL OUTER JOIN
  22. SQL Aggregate Functions
  23. COUNT
  24. SUM
  25. AVG
  26. MIN and MAX
  27. GROUP BY and HAVING
  28. HAVING
  29. Adding Data With INSERT
  30. Changing Data With UPDATE
  31. Removing Data With DELETE
  32. Creating a SQL Table
  33. Changing Table Structure
  34. ALTER TABLE
  35. DROP TABLE
  36. SQL NULL Values
  37. SQL Comments
  38. Common SQL Syntax Order
  39. SQL Syntax Across MySQL, PostgreSQL, SQL Server and SQLite
  40. Who Is This SQL Cheat Sheet For?
  41. SQL Beginners
  42. Developers
  43. Data Analysts
  44. Students
  45. Occasional SQL Users
  46. Tips for Writing Better SQL Queries
  47. Use Clear Formatting
  48. Use Meaningful Table Aliases
  49. Be Careful With SELECT *
  50. Test UPDATE and DELETE Conditions First
  51. Check Your Database Documentation
  52. Frequently Asked Questions About SQL Syntax
  53. What is the basic syntax of SQL?
  54. What are the most important SQL commands to learn?
  55. Is SQL syntax the same for every database?
  56. Does SQL require semicolons?
  57. Is SQL case-sensitive?
  58. What is the difference between WHERE and HAVING?
  59. What is the difference between DELETE, DROP and TRUNCATE?
  60. What is the easiest way to learn SQL syntax?
  61. Can I print this SQL syntax cheat sheet?
  62. Keep This SQL Reference Handy
  63. FAQ

SQL Reference Guide

SQL Syntax Cheat Sheet: 36 Essential SQL Commands & Examples

Need to remember how to write a JOIN, filter rows with WHERE, group results, insert data or update a record? This SQL syntax cheat sheet gives you a fast reference to 36 commonly used SQL commands and syntax patterns.

Use the searchable SQL cheat sheet to find a command, filter references by category, copy syntax examples, bookmark useful entries and print the guide for offline use.

What Is SQL Syntax?

SQL, or Structured Query Language, is used to communicate with relational databases. SQL statements allow you to retrieve information, filter records, combine tables, calculate results and make changes to stored data.

SQL syntax refers to the keywords, clauses, operators and structure used to create those statements.

A basic SQL query might look like this:

SELECT first_name, last_name
FROM customers
WHERE state = 'CA'
ORDER BY last_name;

In this example:

  • SELECT determines which columns are returned.
  • FROM identifies the table containing the data.
  • WHERE filters the records.
  • ORDER BY controls the order of the results.

SQL Syntax Cheat Sheet

The interactive SQL cheat sheet on this page contains 36 syntax references covering common database tasks including data retrieval, filtering, joins, aggregation and database changes.

Instead of scrolling through long documentation pages, search for the SQL task you need and jump directly to the relevant syntax.

Tip: SQL implementations can differ slightly between database systems such as MySQL, PostgreSQL, SQL Server, SQLite and Oracle. Always verify database-specific behavior when working with advanced features.

How to Use This SQL Cheat Sheet

  1. Search for the SQL task you need. Try terms such as select, join, group by, insert or delete.
  2. Filter by category. Narrow the list to related SQL commands and syntax references.
  3. Copy SQL syntax instantly. Use the copy button next to an entry instead of manually typing the command.
  4. Bookmark frequently used SQL commands. Save references you use regularly for faster access later.
  5. Print the cheat sheet. Create an offline SQL reference for your desk, classroom or development workspace.

Basic SQL Query Syntax

Most beginners start with SQL queries that retrieve information from a database. The most recognizable SQL command is SELECT.

SELECT

Use SELECT to retrieve one or more columns from a table.

SELECT first_name, last_name
FROM customers;

SELECT All Columns

An asterisk can be used to return every column from a table.

SELECT *
FROM customers;

DISTINCT

Use DISTINCT when you want to remove duplicate values from query results.

SELECT DISTINCT country
FROM customers;

Filtering Data in SQL

SQL provides several clauses and operators that allow you to narrow database results.

WHERE

The WHERE clause returns only records that match a specified condition.

SELECT *
FROM customers
WHERE state = 'CA';

AND and OR

Combine multiple conditions using AND or OR.

SELECT *
FROM customers
WHERE state = 'CA'
AND status = 'Active';

IN

The IN operator checks whether a value matches one of several possible values.

SELECT *
FROM customers
WHERE state IN ('CA', 'NV', 'AZ');

BETWEEN

Use BETWEEN to filter values within a range.

SELECT *
FROM products
WHERE price BETWEEN 50 AND 100;

LIKE

The LIKE operator is commonly used for pattern matching.

SELECT *
FROM customers
WHERE last_name LIKE 'S%';

In many SQL systems, the percent symbol represents any sequence of characters.

Sorting and Limiting SQL Results

ORDER BY

Use ORDER BY to sort query results.

SELECT *
FROM products
ORDER BY price DESC;

ASC sorts in ascending order while DESC sorts in descending order.

LIMIT

Many SQL databases support LIMIT for restricting the number of rows returned.

SELECT *
FROM products
ORDER BY price DESC
LIMIT 10;

SQL Server commonly uses alternatives such as TOP or OFFSET/FETCH, so check the syntax supported by your database.

SQL JOIN Syntax

SQL joins combine rows from two or more tables using a related column. Joins are one of the most important SQL concepts to understand when working with relational databases.

INNER JOIN

An INNER JOIN returns records with matching values in both tables.

SELECT customers.name, orders.order_date
FROM customers
INNER JOIN orders
ON customers.id = orders.customer_id;

LEFT JOIN

A LEFT JOIN returns every row from the left table and matching rows from the right table.

SELECT customers.name, orders.order_date
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id;

RIGHT JOIN

A RIGHT JOIN returns all rows from the right table and matching rows from the left table where supported.

FULL OUTER JOIN

A FULL OUTER JOIN can return matching and nonmatching records from both tables on database systems that support it.

SQL Aggregate Functions

Aggregate functions calculate a value across multiple rows.

COUNT

SELECT COUNT(*)
FROM orders;

SUM

SELECT SUM(total)
FROM orders;

AVG

SELECT AVG(price)
FROM products;

MIN and MAX

SELECT MIN(price), MAX(price)
FROM products;

GROUP BY and HAVING

GROUP BY groups rows that contain the same values so aggregate calculations can be performed for each group.

SELECT category, COUNT(*)
FROM products
GROUP BY category;

HAVING

HAVING filters grouped results and is commonly used with aggregate functions.

SELECT category, COUNT(*)
FROM products
GROUP BY category
HAVING COUNT(*) > 10;

Adding Data With INSERT

Use INSERT INTO to create a new record.

INSERT INTO customers (first_name, last_name, state)
VALUES ('Jane', 'Smith', 'CA');

Explicitly listing column names makes SQL statements easier to read and can help reduce mistakes.

Changing Data With UPDATE

Use UPDATE to modify existing records.

UPDATE customers
SET status = 'Inactive'
WHERE customer_id = 125;

Important: Be careful when running an UPDATE statement without a WHERE clause because it may update every row in the table.

Removing Data With DELETE

Use DELETE to remove records from a table.

DELETE FROM customers
WHERE customer_id = 125;

A DELETE statement without a WHERE condition may remove all rows from the table. Review destructive queries carefully before running them against production databases.

Creating a SQL Table

The CREATE TABLE statement creates a new database table.

CREATE TABLE customers (
    customer_id INT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100)
);

Production tables will often include additional elements such as primary keys, indexes, constraints and database-specific data types.

Changing Table Structure

ALTER TABLE

Use ALTER TABLE to modify the structure of an existing table.

ALTER TABLE customers
ADD phone VARCHAR(20);

DROP TABLE

DROP TABLE removes a table and its stored data.

DROP TABLE temporary_customers;

Because this operation can permanently remove data, it should be used with caution.

SQL NULL Values

In SQL, NULL represents a missing or unknown value.

You generally test for null values using IS NULL rather than the equals operator.

SELECT *
FROM customers
WHERE phone IS NULL;

To find records that contain a value:

SELECT *
FROM customers
WHERE phone IS NOT NULL;

SQL Comments

Comments can help document SQL code and explain complex queries.

-- Get active customers
SELECT *
FROM customers
WHERE status = 'Active';

Multiline comment support and exact comment syntax can vary by database system.

Common SQL Syntax Order

A typical query may contain several SQL clauses in a predictable order.

SELECT column_name
FROM table_name
JOIN another_table
ON join_condition
WHERE filter_condition
GROUP BY column_name
HAVING group_condition
ORDER BY column_name;

You will not use every clause in every query, but understanding this general structure makes longer SQL statements easier to read.

SQL Syntax Across MySQL, PostgreSQL, SQL Server and SQLite

Core SQL commands such as SELECT, WHERE, INSERT, UPDATE and DELETE are widely supported, but SQL dialects are not completely identical.

Differences can appear in areas such as:

  • Limiting query results
  • Date and time functions
  • String functions
  • Auto-incrementing columns
  • JSON functions
  • Stored procedures
  • Window functions
  • Database administration commands

When writing production queries, confirm that the syntax is supported by your specific database engine and version.

Who Is This SQL Cheat Sheet For?

This reference is designed to be useful whether you are just learning SQL or simply need a fast reminder while working.

SQL Beginners

Learn the basic structure of common SQL statements without constantly searching through long documentation pages.

Developers

Quickly reference SQL commands while building applications, APIs and database-backed services.

Data Analysts

Keep common querying, filtering, joining and aggregation syntax within easy reach.

Students

Use the printable SQL cheat sheet as a study reference while learning relational databases.

Occasional SQL Users

If you know what SQL can do but cannot remember the exact syntax, search the cheat sheet instead of trying to memorize every command.

Tips for Writing Better SQL Queries

Use Clear Formatting

Breaking longer queries onto separate lines makes SQL easier to scan and debug.

Use Meaningful Table Aliases

Aliases can simplify queries involving multiple tables, but overly cryptic aliases can make SQL harder to understand.

Be Careful With SELECT *

Selecting every column may be convenient when exploring data, but production queries can often be clearer and more efficient when you request only the columns you need.

Test UPDATE and DELETE Conditions First

Before running a destructive query, consider running the same WHERE condition in a SELECT query so you can verify which records will be affected.

Check Your Database Documentation

Standard SQL concepts are widely shared, but database-specific functions and advanced syntax can differ.

Frequently Asked Questions About SQL Syntax

What is the basic syntax of SQL?

One of the most common SQL structures is SELECT column FROM table WHERE condition;. More advanced statements can include joins, grouping, sorting and aggregate functions.

What are the most important SQL commands to learn?

Common SQL commands include SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, INSERT, UPDATE and DELETE.

Is SQL syntax the same for every database?

No. Most relational databases share core SQL syntax, but MySQL, PostgreSQL, SQL Server, SQLite, Oracle and other systems have differences in functions, data types and advanced commands.

Does SQL require semicolons?

A semicolon is commonly used to terminate a SQL statement. Requirements can vary depending on the database platform and the software being used to execute the query.

Is SQL case-sensitive?

SQL keywords are generally treated as case-insensitive by many database systems, so SELECT and select often behave the same. Identifier and data comparison behavior can vary by database and configuration.

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping takes place, while HAVING is generally used to filter grouped or aggregated results.

What is the difference between DELETE, DROP and TRUNCATE?

DELETE removes rows, typically with optional filtering. DROP removes a database object such as a table. TRUNCATE generally removes all rows from a table more directly, although its exact behavior varies by database system.

What is the easiest way to learn SQL syntax?

Start with simple SELECT statements, then practice filtering, sorting, joins and aggregation using a small sample database. A searchable SQL cheat sheet can help you quickly look up syntax while practicing.

Can I print this SQL syntax cheat sheet?

Yes. Use the print option on the SQL cheat sheet to create a convenient offline reference for studying or keeping near your workstation.

Keep This SQL Reference Handy

SQL is much easier to use when you do not feel pressure to memorize every command.

Search the 36 SQL syntax references whenever you need a quick reminder, bookmark the commands you use most and print the cheat sheet when you want an offline reference.

As you become more comfortable with SQL, the same reference can continue to serve as a fast lookup tool for everyday database work.

Cheat sheet

36 of 36 shown

Full lists: SQL

FAQ

What does this SQL syntax cheat sheet cover?

It includes 36 items covering data retrieval, filtering, sorting, aggregation, joins, subqueries, data changes, table structure, indexes, views and transactions.

How can I find the reference I need?

Search by task or topic, then use the category filter to narrow the list to relevant items.

Can I copy or bookmark individual items?

Yes. Each item has copy and bookmark buttons so you can reuse its reference content or keep it handy.

Can I print this cheat sheet?

Yes. Use the print option to create a paper reference for use away from the page.

Find More SQL Shortcuts

Search SQL keyboard shortcuts on ShortcutSearch.

Browse SQL Shortcuts