Essential SQL Queries for Data Analysts: Practical Examples and Use Cases
WSDA News | May 4, 2025
SQL (Structured Query Language) remains one of the most critical tools for data professionals, even in an era dominated by AI and automation. Whether it’s for cleaning data, building reports, or driving business decisions, mastering SQL is fundamental to any data analyst's success. Here’s a breakdown of 20 essential SQL queries every data analyst should know, with examples that show how they apply in real-world scenarios.
1. Basic Data Retrieval with SELECT
SELECT * FROM customers;
Purpose: Retrieve all customer records.
2. Filtering Data with WHERE
SELECT * FROM orders WHERE status = 'Pending';
Purpose: Focus on specific records, such as pending orders.
3. Sorting Results with ORDER BY
SELECT * FROM products ORDER BY price DESC;
Purpose: Rank products, e.g., display highest-priced items first.
4. Limiting Output with LIMIT
SELECT * FROM products LIMIT 10;
Purpose: Preview a sample of data.
5. Counting Records with COUNT()
SELECT COUNT(*) FROM orders WHERE status = 'Cancelled';
Purpose: Measure specific metrics like canceled orders.
6. Grouping Data with GROUP BY
SELECT region, COUNT(*) FROM customers GROUP BY region;
Purpose: Analyze customer distribution by region.
7. Summing Values with SUM()
SELECT SUM(amount) FROM sales WHERE year = 2024;
Purpose: Calculate total sales.
8. Calculating Averages with AVG()
SELECT AVG(price) FROM products;
Purpose: Determine average pricing.
9. Finding Extremes with MIN() and MAX()
SELECT MIN(price), MAX(price) FROM products;
Purpose: Identify lowest and highest prices.
10. Getting Unique Values with DISTINCT
SELECT DISTINCT country FROM customers;
Purpose: See all countries served.
11. Filtering by Multiple Values with IN
SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
Purpose: Retrieve data from multiple departments.
12. Date Range Filtering with BETWEEN
SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';
Purpose: Filter records by date.
13. Combining Tables with INNER JOIN
SELECT o.order_id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id;
Purpose: Link customer details to orders.
14. Ensuring Completeness with LEFT JOIN
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.dept_id;
Purpose: Show all employees, even those without departments.
15. Highlighting All Categories with RIGHT JOIN
SELECT d.department_name, e.name
FROM employees e
RIGHT JOIN departments d ON e.dept_id = d.dept_id;
Purpose: Ensure all departments appear, even empty ones.
16. Conditional Logic with CASE
SELECT name,
CASE
WHEN salary > 100000 THEN 'High'
WHEN salary BETWEEN 50000 AND 100000 THEN 'Medium'
ELSE 'Low'
END AS salary_band
FROM employees;
Purpose: Classify data into categories.
17. Handling Missing Data with IS NULL
SELECT * FROM customers WHERE phone IS NULL;
Purpose: Identify incomplete records.
18. Modifying Data with UPDATE
UPDATE orders SET status = 'Shipped' WHERE order_id = 2023;
Purpose: Correct data as needed.
19. Removing Data with DELETE
DELETE FROM test_data WHERE created_at < '2022-01-01';
Purpose: Clean up old or irrelevant records.
20. Using Subqueries for Complex Filters
SELECT name FROM customers
WHERE id IN (SELECT customer_id FROM orders WHERE amount > 500);
Purpose: Find high-value customers based on order history.
Why This Matters for You
Being confident in these SQL queries allows you to handle data efficiently, whether you're supporting business teams, creating dashboards, or tackling complex datasets. These queries form the backbone of many real-world data tasks, giving you the ability to respond quickly and accurately to data requests.
Mastering SQL not only improves your technical abilities but also positions you as a reliable expert in your organization, capable of transforming raw data into meaningful insights.
Data No Doubt! Check out WSDALearning.ai and start learning Data Analytics and Data Science Today!