What is it?
SELECT is the most-used SQL command. It retrieves data from one or more tables. Every query follows the same structure: which columns, from which table, with what conditions.
Why does it matter?
Every feature that displays data uses SELECT -- product listings, user profiles, order history, search results, reports. Writing efficient SELECTs is the single most valuable SQL skill.
Master SQL SELECT -- columns, WHERE filters, IN, BETWEEN, ORDER BY, LIMIT, and column aliases.
Real-World Use Cases
- 🔍 Product search - SELECT name, price FROM products WHERE category='Electronics' ORDER BY price LIMIT 20 -- the foundation of any product listing.
- 📋 User directory - SELECT id, name, email FROM users WHERE active=1 ORDER BY name -- fetch active users alphabetically for an admin panel.
- 📊 Pagination - LIMIT 10 OFFSET 20 fetches page 3 -- used on every paginated listing from e-commerce to social feeds.
- 📈 Latest orders - SELECT * FROM orders ORDER BY created_at DESC LIMIT 5 -- show the 5 most recent orders on a dashboard.
Filtering with WHERE
SELECT * FROM products WHERE price > 500;
SELECT * FROM products WHERE category = 'Electronics' AND price < 2000;
-- IN -- shortcut for multiple OR values
SELECT * FROM users WHERE city IN ('Pune', 'Mumbai', 'Delhi');
-- BETWEEN -- inclusive range
SELECT * FROM products WHERE price BETWEEN 500 AND 2000;
-- IS NULL -- find missing values
SELECT * FROM users WHERE phone IS NULL;
Sorting, Limiting, and Pagination
SELECT * FROM products ORDER BY price ASC;
SELECT * FROM products ORDER BY price DESC;
SELECT * FROM products ORDER BY category ASC, price DESC;
-- First 10 rows only
SELECT * FROM products LIMIT 10;
-- Pagination: page 2 with 10 items per page (rows 11-20)
SELECT * FROM products LIMIT 10 OFFSET 10;
Column Aliases
SELECT name AS product_name,
price AS price_inr,
stock AS available_units
FROM products
WHERE stock > 0;
-- Aliases appear as column headers in results
Q: Why is SELECT * bad practice in production?
SELECT * fetches every column including large TEXT or BLOB fields. It wastes bandwidth, slows queries, and breaks if column order changes. Always name only the columns you need.
Comments (0)
No comments yet. Be the first!
Leave a Comment