SQL - MySQL

Module 01 - Database Theory
1. Introduction to Databases2. DBMS Theory Concepts3. Types of Keys4. Database Relationships5. DBMS Interview Questions
Module 02 - CRUD Operations
1. Create - INSERT2. Read - SELECT3. Update - UPDATE4. Delete - DELETE5. Alter - ALTER TABLE
MySQL Playground
Profile
Akkal DhamiFull Stack Developer

Building modern web experiences with a focus on performance, scalability, and clean architecture.

© 2026 | Akkal Dhami | All rights reserved

Built with
byAkkal Dhami

Navigation

  • Projects
  • Dev Setup
  • Playbook
  • Templates
  • Networking
  • SQL - MySQL
  • SQL Playground
  • DSA
AKKAL DHAMIAKKAL DHAMIAKKAL DHAMI

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:

example.sql
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

example.sql
SELECT column1, column2
FROM table_name;
  • SELECT — which columns you want
  • FROM — which table to read from

Selecting all columns

Use * to get every column, without listing each one:

example.sql
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:

example.sql
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.

example.sql
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

OperatorMeaningExample
=Equal toWHERE author = 'Frank Herbert'
!= or <>Not equal toWHERE author != 'Frank Herbert'
> / <Greater than / less thanWHERE price > 10
>= / <=Greater/less than or equal toWHERE price <= 15
BETWEENWithin a range (inclusive)WHERE price BETWEEN 10 AND 15
INMatches any value in a listWHERE author IN ('George Orwell', 'Aldous Huxley')
LIKEPattern match on textWHERE title LIKE 'D%'
IS NULLColumn has no valueWHERE author IS NULL

Combining conditions

Use AND / OR to combine multiple conditions:

example.sql
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.

example.sql
-- 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.

example.sql
-- 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; -- WRONG

example.sql
SELECT 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:

  • AS is 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.

example.sql
-- 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:

example.sql
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.

example.sql
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:

example.sql
-- 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:

example.sql
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

ClausePurpose
SELECTChoose which columns to return
FROMChoose which table to read from
WHEREFilter which rows are returned
ORDER BYSort the results
LIMITCap the number of rows returned
OFFSETSkip a number of rows before returning results
select-reference.sql
-- 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;