Tutorials › SQL Tutorial › SQL Keyword Reference

SQL Keyword Reference

6 min read · Lesson 23 of 24

A quick reference to the SQL keywords, operators and functions used in this tutorial. Follow the lesson links for full explanations and runnable examples.

Querying data

KeywordDescriptionExampleLesson
SELECTChooses the columns or values to returnSELECT name, price FROM products;SELECT
*All columnsSELECT * FROM orders;SELECT
FROMNames the table to read fromFROM customersSELECT
DISTINCTRemoves duplicate rows from the resultSELECT DISTINCT country FROM customers;SELECT DISTINCT
WHEREKeeps only rows that meet a conditionWHERE city = 'Kisumu'WHERE
ANDBoth conditions must be trueWHERE price > 1000 AND stock > 0AND, OR, NOT
ORAt least one condition must be trueWHERE category = 'Audio' OR category = 'Video'AND, OR, NOT
NOTReverses a conditionWHERE NOT country = 'Kenya'AND, OR, NOT
ORDER BYSorts the resultORDER BY price DESCORDER BY
ASC / DESCAscending (default) / descending sortORDER BY joined DESCORDER BY
LIMITMaximum number of rows to return (SQLite, MySQL, PostgreSQL)LIMIT 5LIMIT
OFFSETSkips rows before LIMIT startsLIMIT 5 OFFSET 10LIMIT
TOPSQL Server's version of LIMITSELECT TOP 5 * FROM products;LIMIT
ASGives a column or table an aliasprice * stock AS stock_valueAliases

Conditions and operators

KeywordDescriptionExampleLesson
=, <>, <, >, <=, >=Comparison operatorsWHERE stock >= 50WHERE
IS NULLValue is missingWHERE country IS NULLNULL Values
IS NOT NULLValue is presentWHERE country IS NOT NULLNULL Values
LIKEMatches a text patternWHERE name LIKE '%USB%'LIKE
%Wildcard: any number of charactersLIKE 'K%'LIKE
_Wildcard: exactly one characterLIKE '_o%'LIKE
INValue is in a list or subquery resultWHERE status IN ('pending', 'shipped')IN and BETWEEN
BETWEENValue is within a range (ends included)WHERE price BETWEEN 1000 AND 5000IN and BETWEEN
EXISTSSubquery returns at least one rowWHERE EXISTS (SELECT 1 FROM orders o WHERE ...)Subqueries

To join pieces of text together, standard SQL uses two pipe characters (see SELECT):

SELECT city || ', ' || country FROM customers;

MySQL uses CONCAT(city, ', ', country) instead, and SQL Server uses +.

Functions

FunctionDescriptionExampleLesson
COUNT()Number of rows or non-NULL valuesCOUNT(*), COUNT(DISTINCT city)Aggregate Functions
SUM()Total of a numeric columnSUM(quantity)Aggregate Functions
AVG()Average of a numeric columnAVG(price)Aggregate Functions
MIN()Smallest valueMIN(order_date)Aggregate Functions
MAX()Largest valueMAX(price)Aggregate Functions
ROUND()Rounds a numberROUND(AVG(price), 2)Aggregate Functions
COALESCE()First non-NULL value in a listCOALESCE(country, 'Unknown')NULL Values
SUBSTR()Part of a text valueSUBSTR(order_date, 1, 7)GROUP BY and HAVING

Grouping and logic

KeywordDescriptionExampleLesson
GROUP BYForms groups of rows for aggregate functionsGROUP BY categoryGROUP BY and HAVING
HAVINGFilters groups after groupingHAVING COUNT(*) > 1GROUP BY and HAVING
CASEIf/then logic inside a queryCASE WHEN stock = 0 THEN 'Out' ELSE 'In' ENDCASE
WHEN / THEN / ELSE / ENDParts of a CASE expressionsee aboveCASE

Joins

KeywordDescriptionExampleLesson
JOIN / INNER JOINRows with a match in both tablesFROM orders o JOIN customers c ON o.customer_id = c.customer_idINNER JOIN
ONThe join conditionON oi.product_id = p.product_idINNER JOIN
LEFT JOINAll rows from the left table, matches from the rightFROM customers c LEFT JOIN orders o ON ...LEFT JOIN
RIGHT JOINAll rows from the right table (not in older SQLite)FROM orders o RIGHT JOIN customers c ON ...LEFT JOIN
FULL OUTER JOINAll rows from both tables (not in MySQL or older SQLite)FROM a FULL OUTER JOIN b ON ...LEFT JOIN
CROSS JOINEvery row paired with every rowFROM products CROSS JOIN customersINNER JOIN

Changing data

KeywordDescriptionExampleLesson
INSERT INTOAdds new rowsINSERT INTO products (name, price) VALUES ('Mouse Pad', 300);INSERT INTO
VALUESThe values for a new rowVALUES (13, 'Kitale Agrovet')INSERT INTO
UPDATEChanges existing rowsUPDATE orders SET status = 'shipped' WHERE order_id = 107;UPDATE
SETThe columns and new values in an UPDATESET stock = stock + 30UPDATE
DELETE FROMRemoves rowsDELETE FROM orders WHERE status = 'cancelled';DELETE
TRUNCATE TABLEQuickly removes all rows (not in SQLite)TRUNCATE TABLE order_items;DELETE

Defining tables

KeywordDescriptionExampleLesson
CREATE TABLECreates a new tableCREATE TABLE suppliers (...);CREATE TABLE
ALTER TABLEChanges an existing tableALTER TABLE products ADD COLUMN supplier_id INTEGER;CREATE TABLE
DROP TABLEDeletes a table and its dataDROP TABLE IF EXISTS suppliers;CREATE TABLE
PRIMARY KEYUniquely identifies each rowcustomer_id INTEGER PRIMARY KEYCREATE TABLE
FOREIGN KEY / REFERENCESLinks to a key in another tableREFERENCES customers(customer_id)CREATE TABLE
NOT NULLColumn must have a valuename TEXT NOT NULLCREATE TABLE
UNIQUENo duplicate values allowedemail TEXT UNIQUECREATE TABLE
DEFAULTValue used when none is givenmethod TEXT DEFAULT 'M-Pesa'CREATE TABLE
CHECKValues must satisfy a conditionCHECK (rating BETWEEN 1 AND 5)CREATE TABLE
Tip: Clause order in a SELECT statement is always: SELECT → FROM → JOIN ... ON → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT.
Sign in or create a free account to track progress, earn XP, badges and a certificate.