25% off selected live virtual classroom courses – use code: BF26WEEK1SAT

4 November 2026

SQL cheat sheet: the syntax you need day to day

This SQL cheat sheet puts everyday syntax for querying, filtering, joins and aggregation in one place, alongside commands for changing data, defining tables, common functions and SQL dialect differenc...

StayAhead Team

This SQL cheat sheet puts everyday syntax for querying, filtering, joins and aggregation in one place, alongside commands for changing data, defining tables, common functions and SQL dialect differences.

Querying data: SELECT, WHERE, ORDER BY, LIMIT

Start with SELECT to choose columns, WHERE to filter rows and ORDER BY to sort the results. LIMIT restricts the number returned, although its syntax varies between databases.

SELECT first_name, salary

FROM employees

WHERE department = 'Sales'

ORDER BY salary DESC

LIMIT 10;

If you're learning SQL, get comfortable with SELECT and WHERE before progressing to joins. Structured SQL training courses can then develop these fundamentals through practical exercises.

Filtering: operators, IN, BETWEEN, LIKE, IS NULL

SQL provides several ways to narrow a result set:

WHERE salary >= 30000

WHERE department IN ('Sales', 'Finance')

WHERE salary BETWEEN 30000 AND 50000

WHERE surname LIKE 'Sm%'

WHERE manager_id IS NULL

A common mistake is writing = NULL. Because NULL represents an unknown or missing value, use IS NULL or IS NOT NULL instead.

Joining tables

SQL joins combine related data held in different tables.

Join Rows returned
INNER JOIN Matches in both tables
LEFT JOIN Every left table row, plus matches
RIGHT JOIN Every right table row, plus matches
FULL JOIN Rows from either table

The basic syntax remains similar:

SELECT e.name, d.department_name

FROM employees e

INNER JOIN departments d

ON e.department_id = d.id;

Change INNER JOIN to the appropriate join type depending on which unmatched rows you need to retain.

Aggregating: COUNT, SUM, AVG, GROUP BY, HAVING

Aggregate functions calculate values across multiple rows. GROUP BY creates groups before the calculation, with HAVING filtering the resulting groups.

SELECT department_id,

COUNT(*) AS employees,

AVG(salary) AS average_salary

FROM employees

WHERE active = 1

GROUP BY department_id

HAVING AVG(salary) > 40000

ORDER BY average_salary DESC;

The logical execution order explains an important distinction:

FROM

WHERE

GROUP BY

HAVING

SELECT

ORDER BY

WHERE filters individual rows before grouping. HAVING filters groups after aggregation.

Modifying data: INSERT, UPDATE, DELETE

Use these commands to add or change records:

INSERT INTO employees (name, department_id)

VALUES ('Sam Jones', 4);

UPDATE employees

SET department_id = 5

WHERE employee_id = 101;

DELETE FROM employees

WHERE employee_id = 101;

Take particular care with UPDATE and DELETE. Without a WHERE clause, the command may affect every row in the table.

DELETE removes selected rows and supports filtering with WHERE. TRUNCATE removes all rows from a table and does not provide row by row filtering.

Defining structure: CREATE, ALTER, DROP

Data definition commands create or change database objects.

CREATE TABLE employees (

employee_id INTEGER PRIMARY KEY,

name VARCHAR(100) NOT NULL,

salary DECIMAL(10,2)

);

ALTER TABLE employees

ADD email VARCHAR(255);

DROP TABLE employees;

Constraints such as PRIMARY KEY and NOT NULL apply rules to the data stored in a table.

Useful functions: string, date, numeric

Function names vary by database, but these are common examples:

Purpose Common functions
String UPPER(), LOWER(), LENGTH()
Date/time CURRENT_DATE, EXTRACT()
Numeric ROUND(), ABS()

Always check the documentation for your database before relying on a particular function or syntax.

Dialect differences: Oracle, PostgreSQL, MySQL, SQL Server

What is SQL if its syntax changes between databases? SQL is a standardised language for working with relational databases, but database products implement their own dialects. Core concepts such as SELECT, WHERE and GROUP BY transfer readily; some functions and syntax do not.

This distinction becomes important when moving between systems:

Task Oracle PostgreSQL MySQL SQL Server
Limit rows FETCH FIRST 10 ROWS ONLY LIMIT 10 LIMIT 10 TOP (10)
Join strings || || CONCAT() CONCAT() or +
Current date SYSDATE CURRENT_DATE CURDATE() GETDATE()
Auto generated ID Identity/sequence Identity/sequence AUTO_INCREMENT IDENTITY

SQL Server commonly uses TOP to restrict results, whereas PostgreSQL and MySQL support LIMIT. Modern Oracle supports FETCH FIRST ... ROWS ONLY.

If you work across database platforms, the dialect comparison is often as useful as the basic SQL cheat sheet itself. StayAhead offers dedicated Oracle SQL training alongside broader SQL courses for professionals who need to develop these skills.

Explore SQL courses.

FAQs

What is the difference between WHERE and HAVING?

WHERE filters individual rows before data is grouped, whereas HAVING filters groups after a GROUP BY operation. Use WHERE to decide which records enter the calculation and HAVING to filter the aggregated results.

Which databases does SQL cover?

The core language applies across relational database systems including Oracle, PostgreSQL, MySQL and SQL Server. Each has its own SQL dialect, so some commands and functions differ.

What is the best way to learn SQL properly?

A cheat sheet is useful for recalling syntax, but practical exercises are essential for learning how queries behave. Start with basic querying and filtering, then progress to joins and aggregation before tackling more complex database work. IT training courses will combine knowledge acquisition with practical application.