Lesson 3 โข Beginner
SELECT Statement
Master the most important SQL command โ retrieving data from tables with SELECT, DISTINCT, aliases, and LIMIT.
๐ฏ What You'll Learn
- Select all columns vs. specific columns
- Create readable aliases with AS
- Remove duplicates with DISTINCT
- Limit results with LIMIT and OFFSET
- Create calculated columns in your output
Basic SELECT Syntax
The SELECT statement retrieves data from a table. It's the command you'll use 90% of the time.
๐ Real-World Analogy
SELECT is like ordering from a menu. SELECT * means "give me everything." SELECT name, price means "just tell me the dish name and how much it costs."
SELECT * returns all columns. It's great for exploring, but in production code, always specify the columns you need โ it's faster and clearer.
โ ๏ธ Common Mistake
Using SELECT * in production applications. It fetches unnecessary data, slows queries, and breaks when columns are added or removed.
SELECT Basics
Try selecting all columns, specific columns, and using aliases
-- Select ALL columns from a table
SELECT * FROM products;
-- Select specific columns only
SELECT product_name, price FROM products;
-- Use column aliases for cleaner output
SELECT
product_name AS "Product",
price AS "Price ($)",
category AS "Category"
FROM products;DISTINCT โ Remove Duplicates
DISTINCT removes duplicate rows from your results. It compares all selected columns โ a row is only a duplicate if every selected column matches.
๐ก Pro Tip
DISTINCT can be slow on large tables because the database must compare every row. Use it only when you actually need unique values.
DISTINCT in Action
Remove duplicate values from your query results
-- Remove duplicate rows with DISTINCT
SELECT DISTINCT category FROM products;
-- Count unique values
SELECT DISTINCT department, job_title
FROM employees;
-- DISTINCT applies to the entire row
-- Both columns must match for a row to be "duplicate"LIMIT & OFFSET โ Control Result Size
LIMIT restricts how many rows are returned. OFFSET skips a number of rows โ perfect for pagination (page 1, page 2, etc.).
๐ Analogy
LIMIT 10 OFFSET 20 is like saying: "Skip the first 20 search results and show me results 21โ30."
LIMIT & Pagination
Control how many rows you get back
-- Limit results to first N rows
SELECT * FROM products LIMIT 10;
-- Pagination: skip first 10, get next 10
SELECT * FROM products LIMIT 10 OFFSET 10;
-- Combine with column selection
SELECT product_name, price
FROM products
LIMIT 5;
-- Useful for previewing large tables
-- Always use LIMIT when exploring unknown data!Calculated Columns
Create new columns with math and string operations
-- Calculated columns
SELECT
product_name,
price,
price * 0.9 AS discounted_price,
price * 1.08 AS price_with_tax
FROM products;
-- String concatenation (varies by database)
-- MySQL:
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
-- PostgreSQL:
-- SELECT first_name || ' ' || last_name AS full_name
-- FROM employees;๐ Quick Reference
| Syntax | Purpose |
|---|---|
| SELECT * | All columns |
| SELECT col1, col2 | Specific columns |
| AS alias | Rename column in output |
| DISTINCT | Remove duplicates |
| LIMIT n | Return only n rows |
| OFFSET n | Skip first n rows |
๐ Lesson Complete!
You can now retrieve data with SELECT, remove duplicates, use aliases, and paginate results. Next, you'll learn to filter data with the powerful WHERE clause!
Sign up for free to track which lessons you've completed and get learning reminders.