SQL Keywords
On this page
What this page is
This is a practical keyword index for fast recall. Syntax varies by database; examples are ANSI-style unless noted.
Query structure
SELECT– choose columns/expressionsFROM– source tables/views/subqueriesWHERE– row filtering (before grouping)GROUP BY– group rows for aggregatesHAVING– filter groups (after grouping)ORDER BY– sort resultLIMIT/TOP/FETCH– restrict row count (vendor-specific)
Joins
JOIN/INNER JOIN– matching rows onlyLEFT JOIN– keep all left rows, match right when possibleRIGHT JOIN– keep all right rows (less common)FULL OUTER JOIN– keep both sides (not in MySQL; useUNIONworkaround)CROSS JOIN– cartesian product
SELECT o.id, c.name FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.status = 'paid';
Filtering and predicates
AND,OR,NOT– boolean logicIN– membership checkBETWEEN– inclusive rangeLIKE– pattern match (%,_)IS NULL/IS NOT NULL– null checksEXISTS– subquery existence
SELECT * FROM products WHERE price BETWEEN 10 AND 50 AND name LIKE '%pro%' AND discontinued IS NULL;
Aggregation
COUNT,SUM,AVG,MIN,MAX– aggregate functionsDISTINCT– unique values
SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id HAVING COUNT(*) >= 5 ORDER BY order_count DESC;
Set operations
UNION– merge distinct rowsUNION ALL– merge including duplicatesINTERSECT– common rows (vendor-specific)EXCEPT/MINUS– rows in first but not second (vendor-specific)
Subqueries and derived tables
AS– aliasWITH– common table expression (CTE)
WITH recent AS ( SELECT id, customer_id FROM orders WHERE created_at >= '2026-01-01' ) SELECT customer_id, COUNT(*) AS n FROM recent GROUP BY customer_id;
DDL (schema)
CREATE,ALTER,DROP– create/change/remove objectsTABLE,VIEW,INDEX,DATABASE– object typesPRIMARY KEY,FOREIGN KEY,UNIQUE,CHECK– constraintsDEFAULT,NOT NULL– column rules
CREATE TABLE users ( id INT PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP );
DML (data changes)
INSERT– add rowsUPDATE– modify rowsDELETE– remove rows
UPDATE users SET email = 'new@example.com' WHERE id = 10;
Transactions
BEGIN/START TRANSACTIONCOMMITROLLBACKSAVEPOINT
START TRANSACTION; UPDATE accounts SET balance = balance - 50 WHERE id = 1; UPDATE accounts SET balance = balance + 50 WHERE id = 2; COMMIT;
Handy notes
- WHERE vs HAVING: WHERE filters rows; HAVING filters groups.
- NULL is not equal to anything (even another NULL). Use
IS NULL. - Join condition belongs in
ON; row filters belong inWHERE(most of the time).