Note Listing columns explicitly improves readability and query performance since the database only fetches what you need.
pick columnschoose fieldsselect specific columns
WHERE Clause
syntax
SELECT columns FROM table WHERE condition;
example
SELECT first_name, email
FROM users
WHERE is_active = true;
output
-- Only rows where is_active is true
Note WHERE filters rows before any grouping occurs. Use HAVING to filter after GROUP BY.
filter rowsconditional selectwhere conditionfilter data
ORDER BY
syntax
SELECT columns FROM table ORDER BY column [ASC|DESC];
example
SELECT product_name, price
FROM products
ORDER BY price DESC;
output
-- Products sorted from most expensive to cheapest
Note ASC is the default direction. You can sort by multiple columns: ORDER BY category ASC, price DESC. Sorting happens after WHERE filtering.
sort resultsorder by columnsort ascendingsort descendingarrange rows
LIMIT and OFFSET
syntax
SELECT columns FROM table LIMIT count OFFSET skip;
example
SELECT product_name, price
FROM products
ORDER BY created_at DESC
LIMIT 10 OFFSET 20;
output
-- Returns rows 21-30 (skips first 20, then takes 10)
Note MySQL/PostgreSQL use LIMIT. SQL Server uses TOP or FETCH FIRST. High OFFSET values degrade performance because the database still scans skipped rows.
paginate resultslimit rowspaginationfirst N rowsskip rowstop N
DISTINCT
syntax
SELECT DISTINCT column FROM table;
example
SELECT DISTINCT city
FROM users
ORDER BY city;
output
-- Returns each city only once, no duplicates
Note DISTINCT applies to the entire row when used with multiple columns. SELECT DISTINCT city, state treats (city, state) pairs as the unit of uniqueness.
SELECT
u.first_name AS name,
o.total_amount AS order_total
FROM users AS u
JOIN orders AS o ON o.user_id = u.id;
output
-- name | order_total
-- Alice | 149.99
Note AS is optional in most databases but improves clarity. You cannot reference a column alias in the WHERE clause of the same query — use a subquery or CTE instead.
rename columnaliascolumn aliastable aliasshorten table name
Computed Columns
syntax
SELECT expression AS alias FROM table;
example
SELECT
product_name,
price,
quantity,
price * quantity AS line_total
FROM order_items;
Note You can use arithmetic operators (+, -, *, /), string functions, and CASE expressions directly in the SELECT list.
calculated columnmath in selectcomputed fieldderived column
SELECT INTO / CREATE TABLE AS
syntax
-- PostgreSQL / SQL Server
SELECT columns INTO new_table FROM source_table;
-- MySQL
CREATE TABLE new_table AS SELECT columns FROM source_table;
example
CREATE TABLE active_users AS
SELECT id, first_name, email
FROM users
WHERE is_active = true;
output
-- Creates a new table active_users with matching rows
Note This copies data but not indexes or constraints. MySQL uses CREATE TABLE ... AS SELECT, while PostgreSQL and SQL Server support SELECT ... INTO.
copy tablecreate table from queryselect into new tableclone data
FETCH FIRST (ANSI Standard)
syntax
SELECT columns FROM table
ORDER BY column
FETCH FIRST n ROWS ONLY;
example
SELECT product_name, price
FROM products
ORDER BY price DESC
OFFSET 5 ROWS
FETCH FIRST 10 ROWS ONLY;
output
-- Skips 5, returns next 10 most expensive products
Note FETCH FIRST is the ANSI SQL standard way to limit results. PostgreSQL supports both FETCH FIRST and LIMIT. MySQL only supports LIMIT. Use FETCH FIRST for maximum portability.
ansi limitfetch first rowsstandard paginationtop rows ansi