query skeleton

SELECT column1, column2
FROM table_name
WHERE condition
GROUP BY column1
HAVING group_condition
ORDER BY column1 [ASC|DESC]
LIMIT n OFFSET m;
Clauses execute in a different order than they're written: FROMWHEREGROUP BYHAVINGSELECTORDER BYLIMIT. That's why you can't reference a SELECT alias in WHERE, but you usually can in ORDER BY.

common commands

CommandPurpose
CREATE TABLE t (id SERIAL PRIMARY KEY, name TEXT NOT NULL);define a new table
ALTER TABLE t ADD COLUMN col TYPE;add a column
DROP TABLE t;delete a table and its data
INSERT INTO t (name) VALUES ('x');add a row
UPDATE t SET name = 'y' WHERE id = 1;modify rows
DELETE FROM t WHERE id = 1;remove rows
TRUNCATE t;remove all rows, fast, resets identity columns

joins at a glance

JoinKeeps
INNER JOINrows with a match in both tables
LEFT JOINall left rows, plus matches from the right (NULLs where no match)
RIGHT JOINall right rows, plus matches from the left
FULL JOINall rows from both sides
CROSS JOINevery combination of rows (Cartesian product)
Full treatment with examples: Joins Explained.

common PostgreSQL data types

TypeUse for
integer / bigintwhole numbers
numeric(p, s)exact decimals — money, anything where float rounding is unacceptable
text / varchar(n)strings — text has no length limit and no performance penalty vs. varchar in Postgres
booleantrue/false
timestamp / timestamptzdate+time — prefer timestamptz unless you have a specific reason not to
uuiduniversally unique identifiers
jsonbsemi-structured data, indexable — see JSONB
array (e.g. text[])a column that holds a list

operators & NULL handling

ExpressionMeaning
=, <>, <, >, <=, >=comparison
AND, OR, NOTlogical
BETWEEN a AND binclusive range
IN (a, b, c)membership
LIKE 'a%' / ILIKEpattern match (case-sensitive / case-insensitive)
IS NULL / IS NOT NULLNULL check — = NULL is always unknown, never true
COALESCE(a, b, c)first non-NULL value

aggregate functions

COUNT(*), COUNT(col), SUM(col), AVG(col), MIN(col), MAX(col), ARRAY_AGG(col), STRING_AGG(col, ', ')
Full treatment: Aggregation & GROUP BY.

where to go from here

SQL Fundamentals — the same material, with explanations.
PostgreSQL Fundamentals — getting a real database running.