SELECT: Retrieving Columns
SELECT is the entry point of every read query in SQL. It tells the database which columns you want to see in the result.
The sample dataset (customers, products, orders) is defined in the Introduction to SQL lesson.
Syntax
-- Select specific columns
SELECT column1, column2, column3
FROM table_name;
-- Select all columns
SELECT *
FROM table_name;
Examples
Example 1: Select all columns
SELECT *
FROM customers;
Result:
| customer_id | name | city | country | |
|---|---|---|---|---|
| 1 | Alice Johnson | alice@example.com | New York | USA |
| 2 | Bob Smith | bob@example.com | London | UK |
| 3 | Carol White | carol@example.com | Sydney | Australia |
| 4 | Dave Brown | dave@example.com | Toronto | Canada |
| 5 | Eve Davis | eve@example.com | New York | USA |
Example 2: Select specific columns
Naming only the columns you need is the standard practice in real-world queries.
SELECT
order_id,
order_date,
total_amount
FROM orders;
Result:
| order_id | order_date | total_amount |
|---|---|---|
| 1 | 2024-01-05 | 999.00 |
| 2 | 2024-01-10 | 59.98 |
| 3 | 2024-01-15 | 349.00 |
| 4 | 2024-02-01 | 796.00 |
| 5 | 2024-02-10 | 49.90 |
| ... | ... | ... |
Common Mistake
Avoid
SELECT * in productionSELECT * is useful for quick exploration but is risky in production code. If someone adds or renames a column, your query silently returns different data. Always list the columns you actually need.
Practice
Write a query that returns the name, category, and price of every product.
Show answer
SELECT
name,
category,
price
FROM products;
Expected result:
| name | category | price |
|---|---|---|
| Laptop | Electronics | 999.00 |
| Mouse | Electronics | 29.99 |
| Desk | Furniture | 349.00 |
| Chair | Furniture | 199.00 |
| Notebook | Stationery | 4.99 |