Your First Query: SELECT
Prerequisites: Our sample database
SELECT retrieves data. The simplest query asks for every column of every
row in a table:
SELECT * FROM products;
Expected output
+----+-----------------------------+-------------+--------+
| id | name | category | price |
+----+-----------------------------+-------------+--------+
| 1 | Mechanical Keyboard | Accessories | 89 |
| 2 | USB-C Hub | Accessories | 35.5 |
| 3 | 27-inch Monitor | Displays | 249.99 |
| 4 | Laptop Stand | Accessories | 42 |
| 5 | Noise-Cancelling Headphones | Audio | 199 |
| 6 | Webcam | Video | 59.9 |
+----+-----------------------------+-------------+--------+
SELECT— "give me..."*— "...every column..."FROM products— "...from the products table.";— end of the statement.
Notice the prices: 89 rather than 89.00. SQLite doesn't keep trailing
zeros for NUMERIC values; PostgreSQL with a NUMERIC(10,2) column would
show 89.00. The value is the same.
SQL keywords aren't case-sensitive
SELECT, select, and Select all work. The common convention is to
write keywords in uppercase and table and column names in lowercase,
which makes queries easier to scan. Text values inside quotes are
compared exactly (in most databases).
Line breaks don't matter
SQL ignores extra spaces and line breaks, so long queries are usually split across lines, one clause per line:
SELECT *
FROM customers;
+----+-------------------+----------------------+----------+------------+
| id | name | email | city | joined_on |
+----+-------------------+----------------------+----------+------------+
| 1 | Ada Lovelace | ada@example.com | London | 2024-01-15 |
| 2 | Linus Torvalds | linus@example.com | Helsinki | 2024-03-02 |
| 3 | Grace Hopper | grace@example.com | New York | 2024-03-20 |
| 4 | Alan Turing | alan@example.com | London | 2024-06-11 |
| 5 | Margaret Hamilton | margaret@example.com | Boston | 2024-09-05 |
| 6 | Tim Berners-Lee | tim@example.com | NULL | 2025-01-08 |
+----+-------------------+----------------------+----------+------------+
Common mistakes
- Misspelling a table name:
SELECT * FROM product;
Parse error near line 1: no such table: product
- Forgetting the semicolon between statements when running several at once.
Practice
Write a query that shows every row and column of the orders table.
Show solution
SELECT * FROM orders;
+----+-------------+------------+-----------+
| id | customer_id | ordered_on | status |
+----+-------------+------------+-----------+
| 1 | 1 | 2025-01-05 | delivered |
| 2 | 1 | 2025-02-14 | delivered |
| 3 | 2 | 2025-02-20 | shipped |
| 4 | 3 | 2025-03-01 | delivered |
| 5 | 3 | 2025-03-18 | cancelled |
| 6 | 4 | 2025-04-02 | pending |
| 7 | 5 | 2025-04-10 | shipped |
| 8 | 1 | 2025-04-22 | pending |
+----+-------------+------------+-----------+