SQL has hundreds of functions. You need about 10 of them for 90% of the work you will do as a data analyst. This guide covers those 10 with real examples.
If you learn these well, you can answer most business questions, pass most interview tests, and handle most of what your first year on the job throws at you.
1. COUNT - How Many Rows?
COUNT tells you how many rows match your criteria. It is the most basic question in data analysis: how many?
SELECT COUNT(*) AS total_orders FROM orders;
How many orders do we have? How many customers signed up this month? How many products are in stock? COUNT answers all of these.
2. SUM - Add Up a Column
SUM adds up all the values in a numeric column. Total revenue, total units sold, total hours worked.
SELECT SUM(amount) AS total_revenue FROM sales WHERE sale_date >= '2026-01-01';
What was our total revenue this year? SUM gives you the answer in one line.
3. AVG - Find the Average
AVG calculates the mean of a numeric column. Average order value, average delivery time, average score.
SELECT AVG(order_total) AS avg_order_value FROM orders;
Averages are useful but can be misleading. A few very large orders can pull the average up. Always check alongside COUNT to understand the full picture.
4. MIN and MAX - Smallest and Largest
MIN returns the smallest value. MAX returns the largest. What was our cheapest product? When was the most recent order? What is the highest salary in the department?
SELECT MIN(price) AS cheapest,
MAX(price) AS most_expensive
FROM products;5. GROUP BY - Summarise by Category
GROUP BY splits your data into groups and applies an aggregate function to each group. It turns 'total revenue' into 'total revenue per region' or 'per month' or 'per product.'
SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region ORDER BY total_sales DESC;
Almost every business question is a GROUP BY question. 'Sales by region.' 'Orders by month.' 'Customers by country.' If you hear 'by' in the question, you need GROUP BY.
6. WHERE - Filter Before Grouping
WHERE filters rows before any grouping or aggregation happens. It answers: 'only show me rows that match this condition.'
SELECT product_name, SUM(quantity) AS total_sold FROM sales WHERE sale_date >= '2026-03-01' GROUP BY product_name;
Without WHERE, you get everything. With WHERE, you get exactly what was asked. Most business questions have a filter hiding in them.
7. HAVING - Filter After Grouping
HAVING filters groups after aggregation. WHERE filters rows before grouping. HAVING filters the results of GROUP BY.
SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id HAVING COUNT(*) >= 5;
This finds customers who placed 5 or more orders. You cannot do this with WHERE because the count does not exist until after the grouping.
8. ORDER BY - Sort Your Results
ORDER BY sorts the output. ASC for smallest first (default), DESC for largest first. Add LIMIT to get the top N.
SELECT product_name, SUM(quantity) AS total_sold FROM sales GROUP BY product_name ORDER BY total_sold DESC LIMIT 5;
Top 5 best-selling products. Top 10 customers by revenue. Bottom 3 performing stores. ORDER BY with LIMIT handles all of these.
9. COALESCE - Replace Nulls
Real data has missing values. COALESCE replaces NULL with a default value so your reports do not show blanks.
SELECT customer_name,
COALESCE(phone, 'No phone on file') AS phone
FROM customers;Without COALESCE, missing phone numbers show as empty cells or NULL. With it, you get a clean, readable result.
10. CASE WHEN - Add Logic
CASE WHEN is the if-else of SQL. It lets you create new columns based on conditions. Categorise customers, flag issues, label data.
SELECT customer_name, total_spent,
CASE
WHEN total_spent >= 10000 THEN 'VIP'
WHEN total_spent >= 5000 THEN 'Regular'
ELSE 'New'
END AS segment
FROM customers;You can also put CASE inside SUM to count by condition, or inside ORDER BY for custom sorting. It is the most versatile tool in SQL.
What About JOIN?
JOIN is a clause, not a function, which is why it is not on this list. But yes, you absolutely need it. JOIN connects two tables using a shared column. Most real databases have many tables and JOIN is how you bring them together.
SELECT c.name, COUNT(o.id) AS total_orders FROM customers c JOIN orders o ON c.id = o.customer_id GROUP BY c.name;
Learn these 10 functions plus JOIN and you are ready for most analyst work.
The Quick Reference
- COUNT - how many rows
- SUM - total of a column
- AVG - average of a column
- MIN / MAX - smallest / largest
- GROUP BY - summarise by category
- WHERE - filter before grouping
- HAVING - filter after grouping
- ORDER BY - sort results
- COALESCE - replace nulls
- CASE WHEN - add logic
These cover every weekly report, every ad hoc question from your manager, most interview questions, and 90% of what you will write in your first year.
Try it yourself
Start with Level 1: SELECT, in your browser.
The first 5 levels are free · no sign-up needed