PostgreSQL cheat sheet

The PostgreSQL features you reach for most, on one printable page: strict types and casts, dates and intervals, text functions, the extras SQLite doesn't have, arrays and JSON. Every snippet was run on PostgreSQL.

Practise in 5 minutes
sqlstreakPostgreSQL cheat sheetPostgreSQL · tested on every release · sqlstreak.com

Types and casts

SELECT '42'::int, 3.99::int, '2026-03-15'::date;

:: is PostgreSQL's cast. 3.99::int rounds to 4.

SELECT pg_typeof(price), pg_typeof(vegetarian)
FROM menu_items LIMIT 1;

pg_typeof shows a value's type.

SELECT 7 / 2, 7 / 2.0, 7::numeric / 2;

Integer division drops the fraction (3); add a decimal.

SELECT ROUND(AVG(price), 2) FROM menu_items;

ROUND(x, 2) needs numeric: cast floats with ::numeric.

Dates and intervals

SELECT DATE '2026-03-15' + 7,
       DATE '2026-03-15' - DATE '2026-03-01';

Date + integer = date; date - date = days.

SELECT ordered_on + INTERVAL '1 month'
FROM orders LIMIT 2;

Intervals: '2 hours', '1 month', '1 year 2 days'.

SELECT date_trunc('month', ordered_on)::date,
       EXTRACT(DOW FROM ordered_on)
FROM orders LIMIT 3;

date_trunc rounds down; EXTRACT pulls out parts (DOW 0 = Sunday).

SELECT to_char(ordered_on, 'YYYY-MM Dy')
FROM orders LIMIT 3;

to_char formats: YYYY MM DD HH24 MI, Dy = weekday name.

SELECT age(DATE '2026-11-22', hired_on)
FROM staff LIMIT 2;

age() gives years, months and days.

Text

SELECT name FROM pets WHERE name ILIKE 'b%';

ILIKE: case-insensitive LIKE (LIKE is case-sensitive here).

SELECT split_part(name, ' ', 1),
       strpos(name, ' ')
FROM staff;

split_part takes the Nth piece; strpos finds text.

SELECT regexp_replace(phone, '[^0-9]', '', 'g')
FROM adoption_requests;

Regular expressions: ~, regexp_replace, regexp_match.

SELECT format('%s (%s)', name, species)
FROM pets;

format: %s for values, %I and %L for quoting.

SELECT string_agg(name, ', ' ORDER BY name)
FROM pets;

Join rows into one string.

PostgreSQL extras

SELECT DISTINCT ON (species) species, name, age
FROM pets
ORDER BY species, age;

The first row per group, in ORDER BY order.

SELECT COUNT(*) FILTER (WHERE adopted) AS adopted,
       COUNT(*) AS total
FROM pets;

FILTER: a condition per aggregate.

SELECT d::date FROM generate_series(
  DATE '2026-03-01', DATE '2026-03-03', INTERVAL '1 day') d;

generate_series: numbers or dates.

UPDATE pets SET age = age + 1
WHERE id = 1 RETURNING name, age;

RETURNING on INSERT, UPDATE and DELETE.

Arrays and JSON

SELECT species, array_agg(name ORDER BY name)
FROM pets GROUP BY species;

array_agg collects values into an array.

SELECT name FROM pets
WHERE species = ANY (ARRAY['cat', 'bird']);

= ANY (array): like IN.

SELECT unnest(ARRAY['S', 'M', 'L']) AS size;

unnest turns an array into rows.

SELECT json_build_object('name', name, 'age', age)
FROM pets LIMIT 2;

Build JSON; ->> reads a field as text.

SELECT '{"size": "L"}'::json ->> 'size';

->> gets text, -> gets JSON.

Stricter than SQLite

SELECT species, name, COUNT(*)
FROM pets GROUP BY species;

Every selected column must be grouped or aggregated.

SELECT name FROM pets WHERE species = "cat";

Double quotes are names, never text.

SELECT name FROM pets WHERE age = '3 years';

Text that isn't a number can't be compared with one.

SELECT 1 / 0;

Division by zero is an error, not NULL.

More cheat sheets

Learn it properly

Read the PostgreSQL vs SQLite: SQL syntax differences guide, or practise in “Date maths” (Pro): short exercises with instant feedback.