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

On This Page
- What Is SQL Syntax?
- SQL Syntax Cheat Sheet
- How to Use This SQL Cheat Sheet
- Basic SQL Query Syntax
- SELECT
- SELECT All Columns
- DISTINCT
- Filtering Data in SQL
- WHERE
- AND and OR
- IN
- BETWEEN
- LIKE
- Sorting and Limiting SQL Results
- ORDER BY
- LIMIT
- SQL JOIN Syntax
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL OUTER JOIN
- SQL Aggregate Functions
- COUNT
- SUM
- AVG
- MIN and MAX
- GROUP BY and HAVING
- HAVING
- Adding Data With INSERT
- Changing Data With UPDATE
- Removing Data With DELETE
- Creating a SQL Table
- Changing Table Structure
- ALTER TABLE
- DROP TABLE
- SQL NULL Values
- SQL Comments
- Common SQL Syntax Order
- SQL Syntax Across MySQL, PostgreSQL, SQL Server and SQLite
- Who Is This SQL Cheat Sheet For?
- SQL Beginners
- Developers
- Data Analysts
- Students
- Occasional SQL Users
- Tips for Writing Better SQL Queries
- Use Clear Formatting
- Use Meaningful Table Aliases
- Be Careful With SELECT *
- Test UPDATE and DELETE Conditions First
- Check Your Database Documentation
- Frequently Asked Questions About SQL Syntax
- What is the basic syntax of SQL?
- What are the most important SQL commands to learn?
- Is SQL syntax the same for every database?
- Does SQL require semicolons?
- Is SQL case-sensitive?
- What is the difference between WHERE and HAVING?
- What is the difference between DELETE, DROP and TRUNCATE?
- What is the easiest way to learn SQL syntax?
- Can I print this SQL syntax cheat sheet?
- Keep This SQL Reference Handy
- 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:
SELECTdetermines which columns are returned.FROMidentifies the table containing the data.WHEREfilters the records.ORDER BYcontrols 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
- Search for the SQL task you need. Try terms such as select, join, group by, insert or delete.
- Filter by category. Narrow the list to related SQL commands and syntax references.
- Copy SQL syntax instantly. Use the copy button next to an entry instead of manually typing the command.
- Bookmark frequently used SQL commands. Save references you use regularly for faster access later.
- 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 sheetSQL Syntax Cheat Sheet
36 of 36 shown
- Filter Rows with WHERE
Returns only rows matching a condition.
More
SELECT * FROM users WHERE age > 30; - Inner Join
Returns rows with matches in both tables.
More
SELECT * FROM orders o JOIN users u ON u.id = o.user_id; - Insert a Row
Adds a new row to a table.
More
INSERT INTO users (name, email) VALUES ('Ada', 'ada@example.com'); - Left Join
Returns all left rows plus matches from the right.
More
SELECT * FROM users u LEFT JOIN orders o ON o.user_id = u.id; - Create a Table
Creates a new table with columns.
More
CREATE TABLE users ( id SERIAL PRIMARY KEY, name TEXT NOT NULL ); - Sum, Average, Min, Max
Aggregates a numeric column.
More
SELECT SUM(total), AVG(total), MIN(total), MAX(total) FROM orders; - Rename a Column in Results
Gives a column or expression an alias.
More
SELECT name AS full_name FROM users; - Subquery
Uses one query's results inside another.
More
SELECT * FROM users WHERE id IN (SELECT user_id FROM orders); - Conditional Value with CASE
Returns values based on conditions.
More
CASE WHEN total > 100 THEN 'big' ELSE 'small' END - Filter a Range with BETWEEN
Filters values inside a range (inclusive).
More
WHERE price BETWEEN 10 AND 20 - Combine Results with UNION
Stacks results of two queries, removing duplicates.
More
SELECT email FROM a UNION SELECT email FROM b; - Common Table Expression
Names a temporary result set with WITH.
More
WITH recent AS ( SELECT * FROM orders WHERE created_at > now() - interval '7 days' ) SELECT * FROM recent; - Add a Foreign Key
Links a column to another table.
More
ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id); - Use a Transaction
Groups statements so they all succeed or fail.
More
BEGIN; ... COMMIT; -- or ROLLBACK; - Create a View
Saves a query as a virtual table.
More
CREATE VIEW active_users AS SELECT * FROM users WHERE active;
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.
