Beginner
SELECT & Filtering
Learn to retrieve exactly the data you need using SELECT statements, WHERE clauses, comparison operators, pattern matching, and sorting.
The SELECT Statement
SELECT is the most fundamental SQL command. It retrieves data from one or more tables.
SQL
-- Select specific columns SELECT name, email FROM users; -- Select all columns SELECT * FROM users; -- Select with aliases SELECT name AS customer_name, email AS contact_email FROM users; -- Select distinct values SELECT DISTINCT country FROM users;
Avoid SELECT * in production. Always specify the columns you need. SELECT * retrieves unnecessary data, slows queries, and can break code when table schemas change.
WHERE Clause
The WHERE clause filters rows based on conditions:
SQL
-- Comparison operators SELECT * FROM products WHERE price > 50; SELECT * FROM products WHERE price >= 50; SELECT * FROM products WHERE category != 'Electronics'; -- Logical operators SELECT * FROM products WHERE price > 50 AND category = 'Electronics'; SELECT * FROM products WHERE category = 'Books' OR category = 'Music'; SELECT * FROM products WHERE NOT category = 'Electronics';
LIKE, IN, and BETWEEN
SQL
-- LIKE for pattern matching SELECT * FROM users WHERE name LIKE 'A%'; -- Starts with A SELECT * FROM users WHERE email LIKE '%@gmail.com'; -- Ends with @gmail.com SELECT * FROM users WHERE name LIKE '_o%'; -- Second char is 'o' -- IN for matching a list of values SELECT * FROM products WHERE category IN ('Electronics', 'Books', 'Clothing'); -- BETWEEN for ranges (inclusive) SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31'; -- IS NULL / IS NOT NULL SELECT * FROM users WHERE phone IS NULL; SELECT * FROM users WHERE phone IS NOT NULL;
ORDER BY
SQL
-- Sort ascending (default) SELECT * FROM products ORDER BY price ASC; -- Sort descending SELECT * FROM products ORDER BY price DESC; -- Multiple sort columns SELECT * FROM products ORDER BY category ASC, price DESC;
LIMIT and OFFSET
SQL
-- Get top 10 most expensive products SELECT name, price FROM products ORDER BY price DESC LIMIT 10; -- Pagination: skip first 20, get next 10 SELECT * FROM products ORDER BY id LIMIT 10 OFFSET 20;
Computed Columns
SQL
SELECT name, price, quantity, price * quantity AS total_value, ROUND(price * 0.9, 2) AS discounted_price, CASE WHEN price > 100 THEN 'Premium' WHEN price > 50 THEN 'Standard' ELSE 'Budget' END AS price_tier FROM products;
Data science tip: Use CASE expressions to bin continuous variables into categories. This is a common feature engineering technique for machine learning.
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX