SQL Reference
Find the right SQL query by describing what you want to do β JOINs, aggregations, window functions, CTEs and more with plain English explanations.
Basics
SELECT, WHERE, ORDER BY, INSERT, UPDATE, DELETE
SELECT query
Retrieve data from one or more columns in a table.
SELECT * FROM users;WHERE clause
Filter query results to only return rows that match a condition.
SELECT name, email
FROM usersORDER BY
Sort query results by one or more columns, ascending or descending.
SELECT name, age
FROM usersLIMIT rows
Restrict how many rows a query returns β useful for pagination and top-N queries.
SELECT name, age
FROM usersFiltering
AND/OR, IN, LIKE, NULL, BETWEEN
AND, OR, NOT
Combine multiple filter conditions in a WHERE clause.
SELECT name, age, country
FROM usersIN operator
Match a column against a list of values.
SELECT name, country
FROM usersLIKE pattern matching
Filter rows based on partial string matches using wildcards.
SELECT name, email
FROM usersNULL values
Check for, filter, and handle NULL values correctly.
SELECT name, email
FROM usersBETWEEN operator
Filter rows within a range of values.
SELECT name, age
FROM usersJoins
INNER, LEFT, RIGHT, FULL OUTER, self-join
INNER JOIN
Return only rows that have matching records in both tables.
SELECT
u.name,LEFT JOIN
Return all rows from the left table, and matching rows from the right table (NULLs if no match).
SELECT
u.name,JOIN types overview
Understand the difference between INNER, LEFT, RIGHT, FULL OUTER and CROSS JOIN.
SELECT u.name, o.total
FROM users uSelf-join
Join a table to itself to compare rows or navigate hierarchies.
SELECT
e.name AS employee,Aggregation
COUNT, SUM, AVG, GROUP BY, HAVING
COUNT rows
Count rows, non-null values, or distinct values in a table.
SELECT COUNT(*) AS total_users
FROM users;SUM, AVG, MIN, MAX
Calculate totals, averages, and extremes across rows.
SELECT SUM(total) AS revenue
FROM ordersGROUP BY
Group rows with the same value and apply aggregate functions to each group.
SELECT
country,HAVING clause
Filter groups after aggregation β like WHERE but for GROUP BY results.
SELECT
country,Subqueries
Nested queries, EXISTS, correlated subqueries
Modifying Data
INSERT, UPDATE, DELETE, UPSERT
INSERT rows
Add one or more new rows to a table.
INSERT INTO users (name, email, age, country)
VALUES ('Alice', 'alice@example.com', 28, 'Kenya');UPDATE rows
Modify existing data in one or more rows.
UPDATE users
SET country = 'Nigeria'DELETE rows
Remove one or more rows from a table.
DELETE FROM users
WHERE id = 42;Schema
CREATE TABLE, ALTER, indexes, constraints
CREATE TABLE
Define a new table with columns, data types and constraints.
CREATE TABLE users (
id SERIAL PRIMARY KEY,ALTER TABLE
Add, remove or modify columns and constraints on an existing table.
ALTER TABLE users
ADD COLUMN phone TEXT;Indexes
Speed up queries by creating indexes on frequently queried columns.
CREATE INDEX idx_users_email ON users(email);Advanced
CTEs, window functions, JSON, EXPLAIN
CTEs (WITH clause)
Write cleaner, reusable subqueries using Common Table Expressions.
WITH active_users AS (
SELECT id, name, emailWindow functions
Calculate running totals, rankings and moving averages without collapsing rows.
SELECT
name,Top N per group
Select the top N records within each category or group.
WITH ranked AS (
SELECTCASE expression
Add conditional logic to SQL queries β like an if/else.
SELECT
name,UPSERT
Insert a row if it does not exist, or update it if it does.
INSERT INTO users (email, name, country)
VALUES ('alice@example.com', 'Alice', 'Kenya')EXPLAIN / query performance
Understand how the database executes your query and find performance bottlenecks.
EXPLAIN
SELECT u.name, COUNT(o.id)Delete duplicate rows
Find and remove duplicate records while keeping one copy.
SELECT email, COUNT(*) AS occurrences
FROM usersJSON queries
Extract, filter and manipulate JSON stored in database columns.
-- Table: events (data JSONB)
-- data: {"user": "alice", "action": "login", "ip": "1.2.3.4"}About This Reference
This SQL reference is organized by what you are trying to do, not by command name. Whether you need to join two tables, find the top N rows per group, delete duplicates, or understand window functions β search by intent and get the right query with a plain English explanation and copy-paste example.
Each entry includes dialect notes for PostgreSQL, MySQL, SQLite and SQL Server where syntax differs. Common mistakes are highlighted so you avoid the most frequent pitfalls.