Read - SELECT
SELECT is how you ask the database questions. If INSERT is writing rows into the table, SELECT is reading them back out — and you can ask for all of them, some of them, sorted a certain way, or just a few at a time.
We'll use this table for every example below:
INSERT INTO books (title, author, price)
VALUES
('Dune', 'Frank Herbert', 15.50),
('1984', 'George Orwell', 9.99),
('Brave New World', 'Aldous Huxley', 10.25);Basic syntax
SELECT column1, column2
FROM table_name;SELECT— which columns you wantFROM— which table to read from
Selecting all columns
Use * to get every column, without listing each one:
SELECT * FROM books;Output:
+----+-----------------+---------------+-------+----------+---------------------+ | id | title | author | price | in_stock | created_at | +----+-----------------+---------------+-------+----------+---------------------+ | 1 | Dune | Frank Herbert | 15.50 | 1 | 2026-07-15 16:34:45 | | 2 | 1984 | George Orwell | 9.99 | 1 | 2026-07-15 16:34:45 | | 3 | Brave New World | Aldous Huxley | 10.25 | 1 | 2026-07-15 16:34:45 | +----+-----------------+---------------+-------+----------+---------------------+
Selecting specific columns
Only pull what you actually need — faster, and easier to read:
SELECT title, price FROM books;Output:
+-----------------+-------+ | title | price | +-----------------+-------+ | Dune | 15.50 | | 1984 | 9.99 | | Brave New World | 10.25 | +-----------------+-------+
Filtering with WHERE
WHERE narrows down which rows come back, based on a condition.
SELECT * FROM books
WHERE price < 12;+----+-----------------+---------------+-------+----------+---------------------+ | id | title | author | price | in_stock | created_at | +----+-----------------+---------------+-------+----------+---------------------+ | 2 | 1984 | George Orwell | 9.99 | 1 | 2026-07-15 16:34:45 | | 3 | Brave New World | Aldous Huxley | 10.25 | 1 | 2026-07-15 16:34:45 | +----+-----------------+---------------+-------+----------+---------------------+
Common comparison operators
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | WHERE author = 'Frank Herbert' |
| != or <> | Not equal to | WHERE author != 'Frank Herbert' |
| > / < | Greater than / less than | WHERE price > 10 |
| >= / <= | Greater/less than or equal to | WHERE price <= 15 |
| BETWEEN | Within a range (inclusive) | WHERE price BETWEEN 10 AND 15 |
| IN | Matches any value in a list | WHERE author IN ('George Orwell', 'Aldous Huxley') |
| LIKE | Pattern match on text | WHERE title LIKE 'D%' |
| IS NULL | Column has no value | WHERE author IS NULL |
Combining conditions
Use AND / OR to combine multiple conditions:
SELECT * FROM books
WHERE price < 15 AND author = 'George Orwell';Output:
+----+-------+---------------+-------+----------+---------------------+ | id | title | author | price | in_stock | created_at | +----+-------+---------------+-------+----------+---------------------+ | 2 | 1984 | George Orwell | 9.99 | 1 | 2026-07-15 16:34:45 | +----+-------+---------------+-------+----------+---------------------+
Pattern matching with LIKE
% matches any number of characters, _ matches exactly one character.
-- Titles starting with "The"
SELECT * FROM books WHERE title LIKE 'The%';
-- Titles containing "world" anywhere
SELECT * FROM books WHERE title LIKE '%world%';Aliasing — SELECT ... AS
AS renames a column (or table) in the query's output, without changing anything in the actual table. It's purely cosmetic for the result set.
-- Aliases with AS
SELECT title AS Title FROM books;
-- Aliases without AS
SELECT title Title FROM books;
-- Aliases with AS & quotes
SELECT title AS "Book Title" FROM books;
-- Aliases without AS & quotes
SELECT title Book Title FROM books; -- WRONGSELECT title as "Book Title", price Price FROM books;Output:
+-----------------+-------+ | Book Title | Price | +-----------------+-------+ | Dune | 15.50 | | 1984 | 9.99 | | Brave New World | 10.25 | +-----------------+-------+
Key points:
ASis optional:SELECT title Title FROM books;- Great for calculated columns:
SELECT price * 0.9 AS discounted_price FROM books; - Table aliases too:
SELECT b.title FROM books AS b;
Sorting with ORDER BY
Controls the order results come back in.
-- Cheapest to most expensive
SELECT * FROM books
ORDER BY price ASC;
-- Most expensive to cheapest
SELECT * FROM books
ORDER BY price DESC;ASC (ascending) is the default, so you can leave it off if that's what you want.
You can sort by multiple columns — the second column only matters when the first has ties:
SELECT * FROM books
ORDER BY author ASC, price DESC;Output:
+----+-----------------+---------------+-------+----------+---------------------+ | id | title | author | price | in_stock | created_at | +----+-----------------+---------------+-------+----------+---------------------+ | 3 | Brave New World | Aldous Huxley | 10.25 | 1 | 2026-07-15 16:34:45 | | 1 | Dune | Frank Herbert | 15.50 | 1 | 2026-07-15 16:34:45 | | 2 | 1984 | George Orwell | 9.99 | 1 | 2026-07-15 16:34:45 | +----+-----------------+---------------+-------+----------+---------------------+
Limiting results with LIMIT
Restricts how many rows come back — useful for previews, or "top N" queries.
SELECT * FROM books
ORDER BY price DESC
LIMIT 2;This returns just the 2 most expensive books.
Skipping rows with OFFSET
Combine with OFFSET to skip a number of rows first — useful for pagination:
-- Skip the first 2 rows, then take the next 2
SELECT * FROM books
ORDER BY id
LIMIT 2 OFFSET 2;Putting it all together
A realistic query combining everything above:
SELECT title, author, price
FROM books
WHERE price < 15
ORDER BY price DESC
LIMIT 2;Output:
+-----------------+---------------+-------+ | title | author | price | +-----------------+---------------+-------+ | Brave New World | Aldous Huxley | 10.25 | | 1984 | George Orwell | 9.99 | +-----------------+---------------+-------+
Read it like a sentence: "Give me the title, author, and price from books, where the price is under 15, sorted most expensive first, and only give me the top 2."
Note the order these clauses must appear in: SELECT → FROM → WHERE → ORDER BY → LIMIT. MySQL will error if you write them out of order.
Quick Reference
| Clause | Purpose |
|---|---|
SELECT | Choose which columns to return |
FROM | Choose which table to read from |
WHERE | Filter which rows are returned |
ORDER BY | Sort the results |
LIMIT | Cap the number of rows returned |
OFFSET | Skip a number of rows before returning results |
-- Select all columns
SELECT * FROM books;
-- Select specific columns
SELECT title, author FROM books;
-- Filter by price and author
SELECT * FROM books
WHERE price < 15 AND author = 'George Orwell';
-- Cheapest to most expensive
SELECT * FROM books
ORDER BY price ASC;
-- Most expensive to cheapest
SELECT * FROM books
ORDER BY price DESC;
-- Limit results to 2 rows
SELECT title, author, price
FROM books
WHERE price < 15
ORDER BY price DESC
LIMIT 2;