Lesson 3 • Beginner Track
SELECT Statement
By the end of this lesson you'll be able to pull exactly the data you want out of any table — choosing columns, renaming them, removing duplicates, paginating, and calculating new values on the fly. SELECT is the command you'll use more than any other in SQL.
What You'll Learn
- ✓Select all columns vs. just the ones you need
- ✓Rename columns in the output with AS
- ✓Remove duplicate rows with DISTINCT
- ✓Page through results with LIMIT and OFFSET
- ✓Build calculated columns with maths
- ✓Read a query result like a database does
Our Sample Table: products
Every query in this lesson runs against this little products table. Keep it in mind as you read — knowing the data is half of writing good SQL.
Result:
| id | product_name | category | price | stock |
|---|---|---|---|---|
| 1 | Wireless Mouse | Electronics | 24.99 | 120 |
| 2 | Coffee Mug | Kitchen | 9.5 | 300 |
| 3 | Mechanical Keyboard | Electronics | 79 | 45 |
| 4 | Notebook | Stationery | 3.25 | 500 |
| 5 | Desk Lamp | Home | 32 | 80 |
| 6 | USB-C Cable | Electronics | 12.99 | 200 |
1. The Basic SELECT
A SELECT statement answers the question "show me some data". The simplest version asks for everything:
🛒 Real-world analogy
SELECT is like ordering from a menu. SELECT * means "bring me one of everything"; SELECT product_name, price means "just the dish name and the price, please".
SELECT * — every column
Return all columns and all rows.
-- Return EVERY column for EVERY row in the table
SELECT * FROM products;
-- The * is a wildcard meaning "all columns".
-- Great for exploring, but name your columns in real apps (see below).Result — 6 rows:
| id | product_name | category | price | stock |
|---|---|---|---|---|
| 1 | Wireless Mouse | Electronics | 24.99 | 120 |
| 2 | Coffee Mug | Kitchen | 9.5 | 300 |
| … | … | … | … | … |
In real applications, name the columns you want instead of using * — it's faster, clearer, and won't silently change when the table gains new columns.
Specific columns
Ask only for product_name and price.
-- Ask for only the columns you actually need
SELECT product_name, price
FROM products;
-- Cleaner output, faster query, and it won't break
-- if someone adds new columns to the table later.Result:
| product_name | price |
|---|---|
| Wireless Mouse | 24.99 |
| Coffee Mug | 9.5 |
| Mechanical Keyboard | 79 |
| … | … |
Your Turn: pick the columns
Fill in the blanks to list the category and stock of every product. The expected result is in the comments so you can check yourself.
🎯 Your Turn: category & stock
Replace the ___ blanks with the column names.
-- 🎯 YOUR TURN — fill in the two blanks, then press "Try it Yourself"
-- Goal: list the category and stock level of every product.
SELECT ___, ___ -- 👉 put the column names here: category, stock
FROM products;
-- ✅ Expected result: 6 rows, two columns (category, stock),
-- e.g. Electronics | 120 , Kitchen | 300 , ...2. Renaming Columns with AS
The AS keyword gives a column a friendly name in the result. The underlying table is never changed — the alias only exists for this one query. It becomes essential the moment you start calculating columns.
Column aliases
Rename columns for readable output.
-- AS renames a column just for this result (the table is unchanged)
SELECT
product_name AS "Product",
price AS "Price (USD)"
FROM products;
-- Aliases make headings readable and are essential
-- once you start using calculated columns.Result:
| Product | Price (USD) |
|---|---|
| Wireless Mouse | 24.99 |
| Coffee Mug | 9.5 |
| … | … |
3. DISTINCT — Remove Duplicates
DISTINCT collapses duplicate rows into one. Our table has three Electronics products, but asking for distinct categories returns each category only once.
DISTINCT compares every selected column, so it can be slow on big tables. Only reach for it when you genuinely need unique values.DISTINCT categories
Collapse duplicates to unique values.
-- DISTINCT removes duplicate rows from the result
SELECT DISTINCT category
FROM products;
-- A row counts as a duplicate only if EVERY selected
-- column matches, so DISTINCT works on the whole row.Result — 4 unique rows:
| category |
|---|
| Electronics |
| Kitchen |
| Stationery |
| Home |
Your Turn: unique categories
One blank this time — add the keyword that removes duplicates.
🎯 Your Turn: DISTINCT
Return each category exactly once.
-- 🎯 YOUR TURN — fill in the blank to list each category only once.
SELECT ___ category -- 👉 the keyword that removes duplicates
FROM products;
-- ✅ Expected result (4 rows): Electronics, Kitchen, Stationery, Home4. LIMIT & OFFSET — Control How Many Rows
LIMIT caps the number of rows returned; OFFSET skips rows first. Together they power "page 1, page 2, page 3" pagination you see on every website.
LIMIT 3 OFFSET 3 is "skip the first 3 results, then give me the next 3" — exactly page 2 of a 3-per-page list.
LIMIT & pagination
Cap and page through rows.
-- LIMIT caps how many rows come back
SELECT product_name, price
FROM products
LIMIT 3;
-- OFFSET skips rows first — this is how pagination works:
SELECT product_name, price
FROM products
LIMIT 3 OFFSET 3; -- skip the first 3, return the next 35. Calculated Columns
A SELECT can compute brand-new columns from existing ones using maths or functions. The original data stays untouched — the new column lives only in the result.
Calculated columns
Add a price_with_tax column on the fly.
-- You can compute new columns on the fly
SELECT
product_name,
price,
price * 1.20 AS price_with_tax -- add 20% tax
FROM products;
-- The products table is never changed — the calculation
-- only exists in this query's result.Result:
| product_name | price | price_with_tax |
|---|---|---|
| Wireless Mouse | 24.99 | 29.988 |
| Coffee Mug | 9.5 | 11.4 |
| … | … | … |
Common Errors (and the fix)
- "no such column: productname" — column names must match exactly (and SQL has no spaces in names). It's
product_name, notproductnameor"product name". - Forgetting
FROM:SELECT product_name;errors — every column query needs a table:SELECT product_name FROM products; SELECT *in real apps: it works, but fetches columns you don't need and breaks code when the table changes. Name your columns.- Quotes mix-up: use single quotes for text values (
'Electronics'); double quotes are for identifiers/aliases in most databases.'≠". - Missing semicolon: most tools need
;to end a statement, especially when running several at once.
📘 Quick Reference
| Syntax | Purpose |
|---|---|
| SELECT * | Every column |
| SELECT col1, col2 | Only these columns |
| AS alias | Rename a column in the output |
| DISTINCT | Remove duplicate rows |
| LIMIT n | Return at most n rows |
| OFFSET n | Skip the first n rows |
| price * 1.2 AS x | A calculated column |
Frequently Asked Questions
Q: Does the order of columns in SELECT matter?
Yes — the result shows columns in the exact order you list them. SELECT price, product_name puts price first.
Q: Is SQL case-sensitive?
Keywords (SELECT, FROM) are not — but it's convention to UPPERCASE them. Table and column names can be case-sensitive depending on the database, so match them exactly.
Q: Why use AS if the table doesn't change?
Readability and necessity: calculated columns have no name until you give them one, and clear headings make results far easier to read.
Q: Will DISTINCT change my data?
No. SELECT only reads — it never modifies the table. DISTINCT just de-duplicates the result.
Mini-Challenge: Half-Price Preview
Put it all together — a brief, a blank canvas, and the expected result in the comments. Write it, then copy it into a playground to confirm.
🎯 Mini-Challenge
product_name + a half_price calculated column, first 4 rows.
-- 🎯 MINI-CHALLENGE
-- Using ONLY what this lesson covered (SELECT, AS, calculated columns, LIMIT):
-- 1. Show product_name and a column called half_price (price * 0.5)
-- 2. Return just the first 4 products
--
-- ✅ Expected: 4 rows, columns product_name and half_price
-- e.g. Wireless Mouse | 12.495 , Coffee Mug | 4.75 , ...
-- your query here🎉 Lesson Complete
- ✅
SELECT *grabs everything; naming columns is better in real apps - ✅
ASrenames columns in the output only - ✅
DISTINCTremoves duplicate rows from the result - ✅
LIMIT+OFFSETcap and paginate results - ✅ You can compute calculated columns without changing the table
- ✅ Next: the
WHEREclause — return only the rows that match a condition
Sign up for free to track which lessons you've completed and get learning reminders.