By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
You’re a data analyst at a mid-sized e-commerce company. Your boss drops a 10GB CSV file of customer orders from the past year and says: "Find me the top 5 highest-spending customers in California who made purchases in the last 30 days. And I need it in 10 minutes."
If you don’t know SELECT, FROM, WHERE, ORDER BY, and LIMIT cold, you’re either: - Manually filtering rows in Excel (slow, error-prone, and impossible at scale).- Writing a Python script (overkill for a one-off query).- Panicking because you don’t know how to extract exactly what you need from the database.
SELECT
FROM
WHERE
ORDER BY
LIMIT
These five clauses are the foundation of SQL for data analysis. They let you: ✅ Extract only the data you need (no more dumping entire tables).✅ Filter rows to focus on relevant records (e.g., "only California customers").✅ Sort results meaningfully (e.g., "highest spenders first").✅ Limit output to avoid overwhelming dashboards or reports.
Real-world impact:- Performance: A query with WHERE runs 10–100x faster than one without it (databases optimize filtered queries).- Cost: In cloud databases (BigQuery, Snowflake, Redshift), you pay per byte scanned. SELECT * on a 1TB table could cost $50+ per query.- Clarity: Stakeholders don’t want 1M rows—they want the top 10 outliers or trends for a specific segment.
SELECT *
SELECT col1, col2
analytics.orders
orders
WHERE state = 'CA'
customer_id
notes
WHERE status = 'active'
status
NULL
'Active'
WHERE status ILIKE 'active'
WHERE status IN ('active', 'Active')
ASC
DESC
ORDER BY revenue DESC LIMIT 10
LIMIT 10 OFFSET 20
SELECT * FROM table LIMIT 10
sql order_id (INT), customer_id (INT), order_date (DATE), total_amount (DECIMAL), state (VARCHAR), status (VARCHAR)
INSERT INTO orders VALUES (1, 101, '2023-10-01', 150.00, 'CA', 'completed'), (2, 102, '2023-10-15', 200.00, 'NY', 'completed'), (3, 101, '2023-11-01', 300.00, 'CA', 'completed'), (4, 103, '2023-11-10', 50.00, 'CA', 'cancelled'), (5, 104, '2023-11-20', 500.00, 'TX', 'completed'), (6, 101, '2023-11-25', 100.00, 'CA', 'completed'); ```
Step 1: Verify the table structure
SELECT * FROM orders LIMIT 5;
Expected output:
order_id | customer_id | order_date | total_amount | state | status ----------+-------------+------------+--------------+-------+----------- 1 | 101 | 2023-10-01 | 150.00 | CA | completed 2 | 102 | 2023-10-15 | 200.00 | NY | completed 3 | 101 | 2023-11-01 | 300.00 | CA | completed 4 | 103 | 2023-11-10 | 50.00 | CA | cancelled 5 | 104 | 2023-11-20 | 500.00 | TX | completed
Step 2: Filter for California customers (WHERE)
SELECT customer_id, total_amount FROM orders WHERE state = 'CA';
Output:
customer_id | total_amount -------------+-------------- 101 | 150.00 101 | 300.00 103 | 50.00
Step 3: Add date filtering (last 30 days)
SELECT customer_id, total_amount FROM orders WHERE state = 'CA' AND order_date >= CURRENT_DATE - INTERVAL '30 days';
Output (assuming today is 2023-11-26):
customer_id | total_amount -------------+-------------- 101 | 300.00 101 | 100.00 103 | 50.00
Step 4: Aggregate spending per customer (GROUP BY + SUM)
SELECT customer_id, SUM(total_amount) AS total_spent FROM orders WHERE state = 'CA' AND order_date >= CURRENT_DATE - INTERVAL '30 days' GROUP BY customer_id;
customer_id | total_spent -------------+------------- 101 | 400.00 103 | 50.00
Step 5: Sort by highest spenders (ORDER BY)
SELECT customer_id, SUM(total_amount) AS total_spent FROM orders WHERE state = 'CA' AND order_date >= CURRENT_DATE - INTERVAL '30 days' GROUP BY customer_id ORDER BY total_spent DESC;
Step 6: Limit to top 5 (LIMIT)
SELECT customer_id, SUM(total_amount) AS total_spent FROM orders WHERE state = 'CA' AND order_date >= CURRENT_DATE - INTERVAL '30 days' GROUP BY customer_id ORDER BY total_spent DESC LIMIT 5;
Final output:
state
order_date
sql CREATE INDEX idx_orders_state ON orders(state); CREATE INDEX idx_orders_date ON orders(order_date);
-- Good (only scans needed columns) SELECT customer_id, total_amount FROM orders WHERE state = 'CA'; `` - UseLIMITearly: If you only need the top 10 rows, addLIMITbefore expensive operations likeORDER BYorJOIN`.
`` - Use
early: If you only need the top 10 rows, add
before expensive operations like
or
AS
sql SELECT customer_id AS customer, SUM(total_amount) AS revenue
sql SELECT customer_id, SUM(total_amount) AS total_spent FROM orders WHERE state = 'CA' AND order_date >= CURRENT_DATE - INTERVAL '30 days' GROUP BY customer_id ORDER BY total_spent DESC LIMIT 5;
sql -- Top 5 CA customers by spend (last 30 days) SELECT customer_id, SUM(total_amount) AS total_spent FROM orders WHERE state = 'CA' AND order_date >= CURRENT_DATE - INTERVAL '30 days' GROUP BY customer_id ORDER BY total_spent DESC LIMIT 5;
IS NULL
IS NOT NULL
sql WHERE status IS NOT NULL
ILIKE
LOWER()
sql WHERE LOWER(state) = 'ca'
BETWEEN
sql WHERE order_date BETWEEN '2023-11-01' AND '2023-11-30'
GROUP BY
SUM()
AVG()
= NULL
'CA'
'ca'
Answer: sql SELECT * FROM orders WHERE state = 'CA' AND order_date BETWEEN '2023-11-01' AND '2023-11-30';
sql SELECT * FROM orders WHERE state = 'CA' AND order_date BETWEEN '2023-11-01' AND '2023-11-30';
Sorting + limiting: "Find the 3 most recent orders."
Answer: sql SELECT * FROM orders ORDER BY order_date DESC LIMIT 3;
sql SELECT * FROM orders ORDER BY order_date DESC LIMIT 3;
Aggregation + filtering: "Calculate the total revenue from completed orders in Texas."
Answer: sql SELECT SUM(total_amount) AS revenue FROM orders WHERE state = 'TX' AND status = 'completed';
sql SELECT SUM(total_amount) AS revenue FROM orders WHERE state = 'TX' AND status = 'completed';
Trap: WHERE vs. HAVING: "Find customers who spent more than $100."
HAVING
WHERE SUM(total_amount) > 100
sql SELECT customer_id, SUM(total_amount) AS total_spent FROM orders GROUP BY customer_id HAVING SUM(total_amount) > 100;
FETCH FIRST
FETCH FIRST 5 ROWS ONLY
Challenge:"Find the top 2 customers by total spend in each state. Return the state, customer ID, and their total spend."
Solution:
SELECT state, customer_id, SUM(total_amount) AS total_spent FROM orders GROUP BY state, customer_id ORDER BY state, total_spent DESC;
Why it works:- GROUP BY state, customer_id groups data by both state and customer.- ORDER BY state, total_spent DESC sorts customers within each state by spend (highest first).- To get only the top 2 per state, you’d need a window function (advanced topic), but this query gives you the raw data to verify.
GROUP BY state, customer_id
ORDER BY state, total_spent DESC
FROM orders
WHERE state = 'CA' AND order_date > '2023-11-01'
ORDER BY total_amount DESC
LIMIT 10
OFFSET
GROUP BY customer_id
HAVING SUM(amount) > 100
WHERE order_date BETWEEN '2023-11-01' AND '2023-11-30'
IN
WHERE state IN ('CA', 'NY', 'TX')
OR
LIKE
WHERE name LIKE 'A%'
DISTINCT
SELECT DISTINCT state FROM orders
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.