Show 45 more cards
Or learn them all in the app
Primary key
the column or columns that uniquely identify each row; it cannot be NULL
id INTEGER PRIMARY KEY
Foreign key
a column that refers to the primary key of another table
user_id INTEGER REFERENCES users(id)
Schema
the structure of a database: its tables, columns, types and constraints
SELECT
read data from a table
SELECT name, email FROM users;
SELECT *
select all columns
SELECT * FROM products;
WHERE
keep only the rows that match a condition
SELECT * FROM orders WHERE total > 100;
AND, OR, NOT
combine or negate conditions
WHERE country = 'US' AND age >= 18
ORDER BY
sort the result; ASC is ascending (the default), DESC descending
ORDER BY created_at DESC
LIMIT
return at most N rows
SELECT * FROM posts ORDER BY likes DESC LIMIT 10;
OFFSET
skip the first N rows; used with LIMIT for paging
LIMIT 10 OFFSET 20
DISTINCT
remove duplicate rows from the result
SELECT DISTINCT country FROM users;
AS (alias)
give a column or table a temporary name
SELECT price * qty AS total FROM items;
Comparison operators
= equal, <> or != not equal, < less, > greater, <= and >=
BETWEEN
the value is within a range, including both ends
WHERE price BETWEEN 10 AND 20
IN
the value matches any value in a list
WHERE status IN ('new', 'paid')
LIKE
pattern matching: % stands for any number of characters, an underscore for exactly one
WHERE name LIKE 'A%'
NULL
a missing or unknown value; it is not equal to anything, not even another NULL
IS NULL and IS NOT NULL
test for missing values; comparing with = NULL never matches
WHERE phone IS NULL
COALESCE
return the first argument that is not NULL
SELECT COALESCE(nickname, name) FROM users;
CASE
if-then-else logic inside a query
CASE WHEN total > 100 THEN 'big' ELSE 'small' END
Aggregate function
computes one value from many rows: COUNT, SUM, AVG, MIN, MAX
COUNT(*) vs COUNT(column)
COUNT(*) counts all rows; COUNT(column) counts only rows where that column is not NULL
SUM and AVG
the total and the average of a numeric column
SELECT AVG(price) FROM products;
MIN and MAX
the smallest and the largest value in a column
GROUP BY
group rows with the same values so aggregates are computed per group
SELECT country, COUNT(*) FROM users GROUP BY country;
HAVING
filter groups after aggregation
GROUP BY country HAVING COUNT(*) > 100
WHERE vs HAVING
WHERE filters rows before grouping; HAVING filters groups after it
Logical order of a SELECT query
FROM, JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT
JOIN
combine rows from two tables using a related column
INNER JOIN
returns only rows that have a match in both tables; plain JOIN means INNER JOIN
SELECT * FROM orders o
JOIN users u ON o.user_id = u.id;
LEFT JOIN
all rows from the left table, plus matching rows from the right; NULLs where there is no match
RIGHT JOIN
all rows from the right table, plus matching rows from the left
FULL OUTER JOIN
all rows from both tables, matched where possible
CROSS JOIN
every combination of rows from two tables
Self join
joining a table to itself
SELECT e.name, m.name AS manager
FROM employees e JOIN employees m ON e.manager_id = m.id;
ON vs USING
ON takes any join condition; USING(column) joins on a column that has the same name in both tables
UNION and UNION ALL
stack the results of two queries; UNION removes duplicates, UNION ALL keeps them
Subquery
a query nested inside another query
WHERE price > (SELECT AVG(price) FROM products)
EXISTS
true if a subquery returns at least one row
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id)
CTE (WITH)
a named temporary result that the main query can use
WITH big AS (SELECT * FROM orders WHERE total > 100)
SELECT COUNT(*) FROM big;
Window function
computes a value over a set of related rows without collapsing them into one
SUM(total) OVER (PARTITION BY user_id)
PARTITION BY
splits rows into groups for a window function
ROW_NUMBER()
numbers the rows within a window
ROW_NUMBER() OVER (ORDER BY score DESC)
RANK() vs DENSE_RANK()
both rank rows; RANK leaves gaps after ties (1, 1, 3), DENSE_RANK does not (1, 1, 2)
INSERT
add new rows to a table
INSERT INTO users (name, email)
VALUES ('Ann', 'ann@example.com');