How to Use This SQL Reference
This SQL cheat sheet is organized into logical sections: querying data, filtering, sorting, modifying data, aggregation, joins, advanced features, and functions. Each command includes its basic syntax and a link to the chapter where you can learn it with examples and exercises. If you are just starting out, begin with our SQL Introduction chapter.
💡 Quick Navigation Tip
Use your browser’s Ctrl + F (or Cmd + F on Mac) to instantly search for any SQL keyword on this page. You can also use the Table of Contents above to jump to a specific section.
Data Query Commands
These are the fundamental SQL commands for retrieving and reading data from a database.
| Command | Description | Syntax | Learn More |
|---|---|---|---|
| SELECT | Retrieve data from one or more columns | SELECT col1, col2 FROM table; |
Chapter 3 |
| SELECT * | Retrieve all columns from a table | SELECT * FROM table; |
Chapter 3 |
| SELECT DISTINCT | Return only unique (non-duplicate) values | SELECT DISTINCT col FROM table; |
Chapter 4 |
| SELECT TOP / LIMIT | Limit the number of rows returned | SELECT * FROM table LIMIT n; |
Chapter 12 |
| FETCH FIRST | ANSI-standard row limiting (SQL Server, Oracle, PostgreSQL) | FETCH FIRST n ROWS ONLY |
Chapter 12 |
| AS (Alias) | Give a column or table a temporary name | SELECT col AS alias FROM table; |
Chapter 19 |
Filtering and Conditions
Use these SQL keywords to filter which rows are returned by your queries.
| Command | Description | Syntax | Learn More |
|---|---|---|---|
| WHERE | Filter rows based on a condition | SELECT * FROM table WHERE condition; |
Chapter 5 |
| AND | Combine multiple conditions (all must be true) | WHERE cond1 AND cond2 |
Chapter 7 |
| OR | Combine conditions (at least one must be true) | WHERE cond1 OR cond2 |
Chapter 7 |
| NOT | Negate a condition | WHERE NOT condition |
Chapter 7 |
| IN | Match any value in a list | WHERE col IN ('a', 'b', 'c') |
Chapter 17 |
| BETWEEN | Filter values within a range (inclusive) | WHERE col BETWEEN 10 AND 50 |
Chapter 18 |
| LIKE | Search for a pattern in text | WHERE col LIKE 'A%' |
Chapter 15 |
| Wildcards | Pattern matching characters used with LIKE | % (any chars), _ (single char) |
Chapter 16 |
| IS NULL | Check for missing (NULL) values | WHERE col IS NULL |
Chapter 9 |
| IS NOT NULL | Check that a value is not missing | WHERE col IS NOT NULL |
Chapter 9 |
| EXISTS | Check if a subquery returns any rows | WHERE EXISTS (subquery) |
Chapter 28 |
Sorting and Ordering
Control the order in which your query results appear.
| Command | Description | Syntax | Learn More |
|---|---|---|---|
| ORDER BY | Sort results by one or more columns | SELECT * FROM table ORDER BY col; |
Chapter 6 |
| ASC | Sort in ascending order (default) | ORDER BY col ASC |
Chapter 6 |
| DESC | Sort in descending order | ORDER BY col DESC |
Chapter 6 |
Data Modification Commands (DML)
These commands let you add, change, and remove data in your tables.
| Command | Description | Syntax | Learn More |
|---|---|---|---|
| INSERT INTO | Add new rows to a table | INSERT INTO table (cols) VALUES (vals); |
Chapter 8 |
| UPDATE | Modify existing rows | UPDATE table SET col = val WHERE cond; |
Chapter 10 |
| DELETE | Remove rows from a table | DELETE FROM table WHERE cond; |
Chapter 11 |
⚠️ Always Use WHERE with UPDATE and DELETE
Running UPDATE or DELETE without a WHERE clause will affect every row in the table. Always double-check your condition before executing these commands.
Aggregation and Grouping
Summarize data by calculating totals, averages, counts, and more.
| Command | Description | Syntax | Learn More |
|---|---|---|---|
| COUNT() | Count the number of rows | SELECT COUNT(*) FROM table; |
Chapter 14 |
| SUM() | Calculate the total of a numeric column | SELECT SUM(col) FROM table; |
Chapter 14 |
| AVG() | Calculate the average of a numeric column | SELECT AVG(col) FROM table; |
Chapter 14 |
| MIN() | Find the smallest value | SELECT MIN(col) FROM table; |
Chapter 13 |
| MAX() | Find the largest value | SELECT MAX(col) FROM table; |
Chapter 13 |
| GROUP BY | Group rows that share a value, then aggregate | SELECT col, COUNT(*) FROM table GROUP BY col; |
Chapter 25 |
| HAVING | Filter groups after aggregation | HAVING COUNT(*) > 5 |
Chapter 26 |
SQL Joins
Combine rows from two or more tables based on related columns. Learn the fundamentals in our SQL Joins Introduction chapter.
| Join Type | Description | Syntax | Learn More |
|---|---|---|---|
| INNER JOIN | Return rows that have matching values in both tables | SELECT * FROM A INNER JOIN B ON A.id = B.id; |
Chapter 21 |
| LEFT JOIN | Return all rows from the left table + matched rows from the right | SELECT * FROM A LEFT JOIN B ON A.id = B.id; |
Chapter 22 |
| RIGHT JOIN | Return all rows from the right table + matched rows from the left | SELECT * FROM A RIGHT JOIN B ON A.id = B.id; |
Chapter 23 |
| FULL OUTER JOIN | Return all rows from both tables (matched and unmatched) | SELECT * FROM A FULL OUTER JOIN B ON A.id = B.id; |
Chapter 24 |
Set Operators
Combine the results of two or more SELECT queries.
| Command | Description | Syntax | Learn More |
|---|---|---|---|
| UNION | Combine results and remove duplicates | SELECT col FROM A UNION SELECT col FROM B; |
Chapter 27 |
| UNION ALL | Combine results and keep all rows (including duplicates) | SELECT col FROM A UNION ALL SELECT col FROM B; |
Chapter 27 |
Subqueries
A subquery is a query nested inside another query. It can appear in the WHERE, FROM, or SELECT clause.
| Type | Description | Example Syntax | Learn More |
|---|---|---|---|
| WHERE Subquery | Filter using the result of another query | WHERE col IN (SELECT col FROM table2) |
Chapter 28 |
| FROM Subquery | Use a subquery as a derived table | SELECT * FROM (SELECT ...) AS sub |
Chapter 28 |
| Correlated Subquery | Subquery that references the outer query | WHERE col > (SELECT AVG(col) FROM ...) |
Chapter 28 |
Window Functions
Perform calculations across a set of rows without collapsing the result. All window functions use the OVER() clause.
| Function | Description | Syntax | Learn More |
|---|---|---|---|
| ROW_NUMBER() | Assign a unique sequential number to each row | ROW_NUMBER() OVER (ORDER BY col) |
Chapter 29 |
| RANK() | Rank rows with gaps after ties | RANK() OVER (ORDER BY col) |
Chapter 29 |
| DENSE_RANK() | Rank rows with no gaps after ties | DENSE_RANK() OVER (ORDER BY col) |
Chapter 29 |
| LAG() | Access a value from the previous row | LAG(col) OVER (ORDER BY col) |
Chapter 29 |
| LEAD() | Access a value from the next row | LEAD(col) OVER (ORDER BY col) |
Chapter 29 |
| PARTITION BY | Split window into groups | OVER (PARTITION BY col ORDER BY col) |
Chapter 29 |
Conditional Expressions
Add logic directly inside your queries using CASE expressions.
SELECT Name,
CASE
WHEN Salary >= 70000 THEN 'Senior'
WHEN Salary >= 55000 THEN 'Mid-Level'
ELSE 'Junior'
END AS Level
FROM Employees;
Common SQL Functions
Built-in functions that help you transform and calculate data inside queries.
String Functions
| Function | Description | Example |
|---|---|---|
| UPPER() | Convert text to uppercase | UPPER('hello') → 'HELLO' |
| LOWER() | Convert text to lowercase | LOWER('HELLO') → 'hello' |
| LEN() / LENGTH() | Return the number of characters | LEN('SQL') → 3 |
| SUBSTRING() | Extract part of a string | SUBSTRING('Hello', 1, 3) → 'Hel' |
| CONCAT() | Join two or more strings together | CONCAT('Hi', ' ', 'SQL') → 'Hi SQL' |
| TRIM() | Remove leading and trailing spaces | TRIM(' SQL ') → 'SQL' |
| REPLACE() | Replace occurrences of a substring | REPLACE('abc', 'a', 'x') → 'xbc' |
| LEFT() / RIGHT() | Extract characters from start or end | LEFT('Hello', 2) → 'He' |
Numeric Functions
| Function | Description | Example |
|---|---|---|
| ROUND() | Round a number to specified decimals | ROUND(3.14159, 2) → 3.14 |
| ABS() | Return the absolute value | ABS(-42) → 42 |
| CEILING() / CEIL() | Round up to the nearest integer | CEILING(4.2) → 5 |
| FLOOR() | Round down to the nearest integer | FLOOR(4.8) → 4 |
| MOD() | Return the remainder of a division | MOD(10, 3) → 1 |
Date Functions
| Function | Description | DB Support |
|---|---|---|
| NOW() / GETDATE() | Return current date and time | MySQL: NOW() / SQL Server: GETDATE() |
| CURDATE() / CURRENT_DATE | Return current date only | MySQL / PostgreSQL |
| YEAR() / MONTH() / DAY() | Extract parts from a date | Most databases |
| DATEDIFF() | Calculate difference between two dates | MySQL / SQL Server |
| DATEADD() | Add interval to a date | SQL Server |
NULL Handling Functions
| Function | Description | Example |
|---|---|---|
| COALESCE() | Return the first non-NULL value in a list | COALESCE(NULL, 'fallback') → 'fallback' |
| NULLIF() | Return NULL if two values are equal | NULLIF(10, 10) → NULL |
| IFNULL() / ISNULL() | Replace NULL with a specified value | IFNULL(NULL, 0) → 0 |
| CAST() / CONVERT() | Convert a value to a different data type | CAST('123' AS INT) → 123 |
SQL Statement Order of Execution
SQL does not execute clauses in the order you write them. Understanding the actual processing order helps you debug queries and know where to apply filters.
| Step | Clause | What Happens |
|---|---|---|
| 1 | FROM / JOIN | Tables are loaded and joined |
| 2 | WHERE | Individual rows are filtered |
| 3 | GROUP BY | Rows are grouped |
| 4 | HAVING | Groups are filtered |
| 5 | SELECT | Columns and expressions are computed |
| 6 | DISTINCT | Duplicate rows are removed |
| 7 | ORDER BY | Results are sorted |
| 8 | LIMIT / FETCH | Row count is limited |
💡 Why Execution Order Matters
Because WHERE runs before SELECT, you cannot reference a column alias in the WHERE clause. And because HAVING runs after GROUP BY, it is the right place to filter aggregated results — not WHERE. This is also why window functions cannot be filtered in WHERE (they run during the SELECT step).
SQL Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | WHERE Salary = 55000 |
| <> or != | Not equal to | WHERE City <> 'Mumbai' |
| > | Greater than | WHERE Salary > 60000 |
| < | Less than | WHERE Salary < 60000 |
| >= | Greater than or equal to | WHERE Salary >= 55000 |
| <= | Less than or equal to | WHERE Salary <= 75000 |
SQL Keywords Quick List
Here is a compact alphabetical list of all major SQL keywords for quick scanning. Click any keyword’s chapter link above to learn it in detail.
ALL, AND, ANY, AS, ASC, BETWEEN, BY, CASE, CAST, COALESCE,
CONCAT, CONVERT, COUNT, CREATE, CROSS JOIN, CURDATE, DATEADD,
DATEDIFF, DAY, DELETE, DESC, DISTINCT, DROP, ELSE, END, EXISTS,
FETCH, FIRST, FLOOR, FROM, FULL OUTER JOIN, GETDATE, GROUP BY,
HAVING, IF, IFNULL, IN, INNER JOIN, INSERT INTO, IS NOT NULL,
IS NULL, ISNULL, JOIN, LAG, LEAD, LEFT, LEFT JOIN, LENGTH, LIKE,
LIMIT, LOWER, MAX, MIN, MOD, MONTH, NOT, NOW, NULL, NULLIF,
OFFSET, ON, OR, ORDER BY, OUTER, OVER, PARTITION BY, REPLACE,
RIGHT, RIGHT JOIN, ROUND, ROW_NUMBER, RANK, DENSE_RANK, SELECT,
SET, SUBSTRING, SUM, THEN, TOP, TRIM, UNION, UNION ALL, UPDATE,
UPPER, VALUES, WHEN, WHERE, YEAR
For a deeper understanding of SQL syntax rules and how statements are structured, revisit Chapter 2: SQL Syntax. If you want to practice SQL with real-world dashboards, explore our E-commerce KPI Scorecard or download a ready-made Excel Dashboard Template from NextGenTemplates.
See all chapters in our SQL Tutorial course on Neotech Navigators.
📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on SQL and data analytics.
📝 What You Learned in This Course
- Querying data — SELECT, DISTINCT, aliases, limiting rows
- Filtering — WHERE, AND, OR, NOT, IN, BETWEEN, LIKE, IS NULL
- Sorting — ORDER BY, ASC, DESC
- Modifying data — INSERT INTO, UPDATE, DELETE
- Aggregation — COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING
- Joins — INNER, LEFT, RIGHT, FULL OUTER JOIN
- Combining results — UNION, UNION ALL
- Subqueries — nested queries in WHERE, FROM, and SELECT
- Window functions — ROW_NUMBER, RANK, LAG, LEAD, SUM OVER
- Functions — string, numeric, date, and NULL handling functions
Frequently Asked Questions
SELECT, FROM, WHERE, ORDER BY, INSERT INTO, UPDATE, and DELETE. These seven commands cover the vast majority of everyday SQL tasks including reading, filtering, sorting, adding, and modifying data in a database.WHERE filters individual rows before grouping, while HAVING filters groups after aggregation. Use WHERE for row-level conditions and HAVING for conditions on aggregated values like COUNT(*) > 5 or SUM(Salary) > 100000.SELECT, WHERE, JOIN, GROUP BY, etc.) work across all major databases including MySQL, PostgreSQL, SQL Server, Oracle, and SQLite. Minor syntax differences are noted where relevant — for example, LIMIT works in MySQL and PostgreSQL while SQL Server uses TOP.SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. However, SQL executes in a different order: FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. Understanding execution order helps you debug errors with aliases and filters.CREATE TABLE, ALTER TABLE) and DCL commands (GRANT, REVOKE) which are typically handled by database administrators.📖 Chapter 30 of 30 — SQL Tutorial on Neotech Navigators



