SQL Tutorial

Query relational data — SELECT, JOIN, GROUP BY, and indexes, with runnable examples.

1 min read 162 words Updated Mar 2, 2026

SQL is the language of relational databases. Learn to read and shape data with a few core statements.

Selecting rows

SELECT id, name, price
FROM products
WHERE price > 10
ORDER BY price DESC
LIMIT 10;

Filtering and patterns

SELECT * FROM users
WHERE email LIKE '%@example.com'
  AND created_at >= '2026-01-01';

Aggregating with GROUP BY

SELECT category, COUNT(*) AS n, AVG(price) AS avg_price
FROM products
GROUP BY category
HAVING COUNT(*) > 5;

Joining tables

SELECT o.id, u.name, o.total
FROM orders o
JOIN users u ON u.id = o.user_id
WHERE o.status = 'paid';
Join type Returns
INNER rows with matches in both tables
LEFT all left rows, nulls on no match
FULL all rows from both tables

Creating tables and indexes

CREATE TABLE products (
  id        SERIAL PRIMARY KEY,
  name      TEXT NOT NULL,
  price     NUMERIC(10, 2),
  category  TEXT
);

CREATE INDEX idx_products_category ON products (category);

Indexes speed up reads but slow down writes. Add them on columns you filter or join on often.