1 SELECT
TopThe SELECT statement retrieves data from a table. It's the most-used SQL command by far — you'll write it hundreds of times a day as a developer or analyst. (We cover it in-depth with WHERE, ORDER BY, and GROUP BY in the next chapter; here we focus on the fundamentals.)
Select All Columns
SELECT * FROM products;id | name | category | price | stock | created ---|-----------------|-------------|--------|-------|---------- 1 | Wireless Mouse | Electronics | 29.99 | 150 | 2025-01-10 2 | SQL Textbook | Books | 45.00 | 80 | 2025-02-15 3 | USB-C Hub | Electronics | 39.99 | 200 | 2025-03-01 4 | Notebook Pack | Stationery | 12.50 | 500 | 2025-03-20 5 | Mechanical KB | Electronics | 89.99 | 60 | 2025-04-01
Select Specific Columns
Don't use * in production code — always list the columns you need:
SELECT name, price, stock
FROM products;name | price | stock ----------------|--------|------ Wireless Mouse | 29.99 | 150 SQL Textbook | 45.00 | 80 USB-C Hub | 39.99 | 200 Notebook Pack | 12.50 | 500 Mechanical KB | 89.99 | 60
Aliases: Rename Columns in Output
SELECT
name AS product_name,
price AS unit_price,
price * stock AS inventory_value
FROM products;product_name | unit_price | inventory_value ----------------|------------|---------------- Wireless Mouse | 29.99 | 4498.50 SQL Textbook | 45.00 | 3600.00 USB-C Hub | 39.99 | 7998.00 Notebook Pack | 12.50 | 6250.00 Mechanical KB | 89.99 | 5399.40
DISTINCT: Remove Duplicates
-- How many unique categories do we have?
SELECT DISTINCT category FROM products;category ----------- Electronics Books Stationery
LIMIT: Restrict the Number of Rows
-- Show only the first 3 products
SELECT name, price FROM products
LIMIT 3;Avoid SELECT * in real applications. It fetches every column, wastes bandwidth, and breaks your code if someone adds a column later. Always list the specific columns you need. SELECT * is fine for quick exploration and debugging only.
Key Takeaways
-
SELECT column1, column2 FROM tableretrieves specific data - Use
ASto rename columns and create calculated columns in the output -
DISTINCTremoves duplicate values from the results -
LIMIT nrestricts how many rows are returned - Avoid
SELECT *in production — name your columns explicitly