Read data
SELECT name, city FROM customers;Shows the name and city columns of the customers table.SELECT * FROM customers;Shows every column. Handy to explore, best avoided in an application.SELECT DISTINCT city FROM customers;Each city once, without duplicates.SELECT name AS customer FROM customers;Renames the column in the result.ORDER BY name ASC, signed_up DESCSorts by name (A to Z), then from newest to oldest.LIMIT 10 OFFSET 20Keeps 10 rows starting from the 21st (MySQL, PostgreSQL, SQLite). SQL Server: OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY.
Filter
WHERE price > 1000 AND stock > 0Both conditions must be true. OR: at least one of them.WHERE city IN ('Lagos', 'Nairobi')The value is in the list.WHERE name LIKE 'Jo%'Starts with “Jo”. % stands for any run of characters, _ for a single one.WHERE order_date BETWEEN '2026-01-01' AND '2026-06-30'Between two values, both included.WHERE email IS NULLThe value is missing. “= NULL” never works: use IS NULL or IS NOT NULL.
Group and compute
SELECT COUNT(*) FROM orders;Number of rows. COUNT(column) skips NULL values.SUM(amount), AVG(amount)Total and average of a numeric column.MIN(price), MAX(price)Smallest and largest value.SELECT city, COUNT(*) FROM customers GROUP BY city;One result per city: how many customers each has.HAVING COUNT(*) > 5Filters groups, after GROUP BY. WHERE filters rows, before.
Join tables
FROM orders o JOIN customers c ON c.id = o.customer_idINNER JOIN: only the orders that have a customer.FROM customers c LEFT JOIN orders o ON o.customer_id = c.idEvery customer, even without orders (order columns are NULL).WHERE o.id IS NULLWith a LEFT JOIN: the customers who never ordered.FULL OUTER JOINEvery row of both tables, matched when possible (not in MySQL).
Insert, update, delete
INSERT INTO customers (name, city) VALUES ('Awa', 'Dakar');Adds a row.UPDATE products SET price = 2500 WHERE id = 12;Changes the chosen rows. Without WHERE, the whole table changes.DELETE FROM carts WHERE created_at < '2026-01-01';Deletes the chosen rows. Run a SELECT with the same WHERE first to check.
Table structure
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL);Creates a table with a primary key and a required column.ALTER TABLE customers ADD COLUMN phone VARCHAR(20);Adds a column to an existing table.CREATE INDEX idx_customers_city ON customers (city);Speeds up searches and sorts on this column.DROP TABLE carts;Deletes the table and all its data, for good.
Going further
WITH sales AS (SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id) SELECT * FROM sales WHERE total > 100000;A named query (CTE) reused by the next one: easier to read than a subquery.WHERE id IN (SELECT customer_id FROM orders)Subquery: the customers with at least one order.BEGIN; … COMMIT;Transaction: everything is saved together, or nothing with ROLLBACK.FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMITThe order in which the database runs a query. That is why a SELECT alias does not exist yet in WHERE.
Other resources
Need help with your website?
See the services I build your custom website.
Showcase site, online shop, management tool: let’s talk about your project.