SQL 101
In Software Development 101, order 194 ended up as rows in Postgres. Now you'll ask those rows questions, in real Postgres running right here in your browser.
SELECT name, price
FROM products
WHERE product_id = 3;
If you can read a spreadsheet, you can learn to read SQL.
quantity * unit_price.SELECTWhich columns?"Show me name and price…"FROMWhich table?"…from products…"WHEREWhich rows?"…but only Electronics."
SELECT name, price
FROM products
WHERE category = 'Electronics';
SELECT full_name, city
FROM customers
LIMIT 5;
SELECT * means every column.LIMIT to peek before asking for everything.
SELECT name, price
FROM products
WHERE price > 100;
| = | equals |
|---|---|
| <> | not equal |
| > < >= <= | compare |
| LIKE 'S%' | starts with S |
| IN ('a', 'b') | any of a list |
| AND, OR | combine |
| IS NULL | has no value |
Without ORDER BY, rows come back in no promised order.
-- The 5 most expensive products
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 5;
DESC puts the biggest first. ASC, the default, puts the smallest first.SELECT COUNT(*) FROM orders;SELECT SUM(quantity * unit_price) FROM order_items;SELECT ROUND(AVG(price), 2) FROM products;
SELECT COUNT(*) AS orders, MIN(order_date) AS first, MAX(order_date) AS last
FROM orders;
Revenue only counts completed orders: $103,843.10. Getting it takes a JOIN.
SELECT city, COUNT(*) AS customers
FROM customers
GROUP BY city
HAVING COUNT(*) >= 7
ORDER BY customers DESC, city;
GROUP BY makes one row per city, per month, per anything.WHERE filters rows before grouping. HAVING filters the groups after.
SELECT o.order_id, o.order_date, c.full_name, c.city
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
ORDER BY o.order_id
LIMIT 5;
ON says how rows match: the shared id.o and c are short nicknames for the tables.
SELECT TO_CHAR(o.order_date, 'YYYY-MM') AS month,
SUM(oi.quantity * oi.unit_price) AS revenue
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
WHERE o.status = 'completed'
GROUP BY month
ORDER BY month;
BEGIN;
INSERT INTO orders (customer_id, order_date)
VALUES (7, CURRENT_DATE);
INSERT INTO order_items
(order_id, product_id, quantity, unit_price)
VALUES (currval('orders_order_id_seq'), 3, 1, 89.00);
COMMIT;
SELECT order_id, product_id, quantity, unit_price
FROM order_items
WHERE order_id = (SELECT MAX(order_id) FROM orders);
currval reads the number it just got.BEGIN and COMMIT make both rows land together, or neither.
UPDATE orders
SET status = 'cancelled'
WHERE order_id = 194
RETURNING order_id, status;
-- No WHERE!
UPDATE products SET price = 0;
That sets every price to zero. Once it's committed, there's no undo button.
The habit: write a SELECT with the same WHERE first, check the rows, then change the verb.
SELECT reads, INSERT adds, UPDATE changes, DELETE removes.
CREATE TABLE suppliers (
supplier_id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
Columns, types and rules.
ALTER TABLE products
ADD COLUMN stock INT DEFAULT 0;
Change the shape and keep the data.
DROP TABLE suppliers;
Table and data, gone. No undo.
-- The rules hold: products has CHECK (price >= 0)
INSERT INTO products (name, category, price) VALUES ('Broken', 'Test', -5);
| SELECT … FROM … | pick columns from a table |
|---|---|
| WHERE | filter rows: =, <>, >, LIKE, IN, AND, OR |
| ORDER BY … DESC LIMIT n | sort, keep the top n |
| COUNT, SUM, AVG, MIN, MAX | collapse rows into one answer |
| GROUP BY … HAVING | one row per group, then filter the groups |
| JOIN … ON | connect tables by their shared id |
| INSERT INTO … VALUES | add rows |
| UPDATE … SET … WHERE | change rows: WHERE first! |
| DELETE FROM … WHERE | remove rows: WHERE first! |
docker compose up -d.They all send SQL to the same database, so what you learned works in every one.
The best practice data is your own team's. Ask for read-only access this week.
SELECT questions
FROM audience;