PostgreSQL Basic Queries
Insert, update, delete, select, sort, search, page and count: the PostgreSQL queries you use every day, all together so you don't have to search for each one.

When I started with PostgreSQL I kept searching for the same queries over and over: how to update a row, how to sort, how to page through results. This post puts them all in one place.
The whole list comes first, so you can copy what you need. Further down, each query is broken into its clauses with what every part does.
All the queries
-- Add a rowINSERT INTO users (name, email) VALUES ('Ted', 'ted@example.com');
-- Delete matching rowsDELETE FROM users WHERE email = 'ted@example.com';
-- Change matching rowsUPDATE users SET name = 'Charles' WHERE email = 'charles@example.com';
-- Find rowsSELECT * FROM users WHERE email = 'charles@example.com';
-- Sort rowsSELECT * FROM users ORDER BY email;
-- Search textSELECT * FROM users WHERE name LIKE '%e%';
-- Page through rowsSELECT * FROM users ORDER BY email OFFSET 1 LIMIT 2;
-- Count rowsSELECT COUNT(*) FROM users WHERE email = 'charles@example.com';The example table
Every query uses this users table. Run it in psql or pgAdmin to follow
along:
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(128), email VARCHAR(128) UNIQUE);CREATE TABLE users: makes a new table calledusers.id SERIAL PRIMARY KEY: a column PostgreSQL numbers for you (1, 2, 3…).PRIMARY KEYmeans each row'sidis unique and is how the row is identified.name VARCHAR(128): a text column of up to 128 characters.email VARCHAR(128) UNIQUE: also text, andUNIQUEstops two users from having the same email.
INSERT: add a row
INSERT INTO users (name, email) VALUES ('Ted', 'ted@example.com');INSERT INTO users: add a new row to theuserstable.(name, email): the columns you are filling. Any column left out, likeid, gets its default.VALUES ('Ted', 'ted@example.com'): the values, in the same order as the columns. Text goes in single quotes.;: ends the statement.
Add RETURNING id before the ; to get the new row's id back in the same
query.
DELETE: remove rows
DELETE FROM users WHERE email = 'ted@example.com';DELETE FROM users: remove rows from theuserstable.WHERE email = 'ted@example.com': only the rows whose email matches. Every matching row is deleted, not just the first.
UPDATE: change rows
UPDATE users SET name = 'Charles' WHERE email = 'charles@example.com';UPDATE users: change rows in theuserstable.SET name = 'Charles': the new value. To change several columns, separate them with commas:SET name = 'Charles', email = 'c@example.com'.WHERE email = 'charles@example.com': which rows to change. Without it, every row gets the new name.
SELECT: find rows
SELECT * FROM users WHERE email = 'charles@example.com';SELECT *: return every column. To get only some, name them instead:SELECT name, email.FROM users: the table to read from.WHERE email = 'charles@example.com': only return rows where this is true. Leave it out to get every row.
ORDER BY: sort
SELECT * FROM users ORDER BY email;SELECT * FROM users: every column of every row, as above.ORDER BY email: sort the rows by email, A to Z. AddDESC(ORDER BY email DESC) for Z to A.
Without ORDER BY, PostgreSQL doesn't promise any order, and it can change
between runs.
LIKE: search text
SELECT * FROM users WHERE name LIKE '%e%';SELECT * FROM users: every column, fromusers.WHERE name LIKE '%e%': keep rows whose name matches the pattern.%: stands for any number of characters, including none.'%e%'means "has an e somewhere",'e%'means "starts with e", and'%e'means "ends with e"._: stands for exactly one character, so'T_d'matches "Ted" and "Tod".
LIKE is case-sensitive in PostgreSQL, so '%e%' won't match "Ed". Use
ILIKE when case shouldn't matter.
OFFSET and LIMIT: page through rows
SELECT * FROM users ORDER BY email OFFSET 1 LIMIT 2;SELECT * FROM users: every column, fromusers.ORDER BY email: sort first, so the pages are always cut from the same order.OFFSET 1: skip the first row.LIMIT 2: then return at most two rows.
Together they return rows 2 and 3. For pages of 10, page 1 is OFFSET 0 LIMIT 10, page 2 is OFFSET 10 LIMIT 10, and so on.
COUNT: count rows
SELECT COUNT(*) FROM users WHERE email = 'charles@example.com';SELECT COUNT(*): instead of the rows, return one number: how many rows there are.FROM users: the table to count in.WHERE email = 'charles@example.com': only count rows that match. Leave it out to count the whole table.
A result of 0 means no such user exists, which is a quick check before you
insert one.