SQL is one of the most important tools in a data analyst’s toolkit. But writing SQL isn’t just about selecting rows and filtering tables — it’s about transforming, aggregating, and making sense of raw data using powerful functions.
In this guide, we’ll walk through the top SQL functions every data analyst should know — especially if you’re working with business reports, dashboards, or preparing for SQL interviews.
Whether you’re learning SQL for the first time or brushing up for an analytics role, these functions will appear in 90% of your day-to-day queries.
COUNT()
— Count Rows in SQLThis one’s a classic. COUNT()
lets you count rows in a table — whether it’s all rows, distinct values, or conditional counts.
-- Count all rows in a table
SELECT COUNT(*) FROM orders;
-- Count distinct customers
SELECT COUNT(DISTINCT customer_id) FROM orders;
-- Count only rows where the value is not null
SELECT COUNT(product_id) FROM orders;
🔍 Tip: Use this with GROUP BY
to count grouped values by region, product, etc.
GROUP BY
— Aggregate Your DataGROUP BY
is essential for summarizing data. It groups rows by one or more columns so you can apply aggregate functions like SUM()
, COUNT()
, or AVG()
.
SELECT country, COUNT(*) AS order_count
FROM orders
GROUP BY country;
You’ll use this all the time when analyzing performance by team, time period, product, or location.
🎓 See more in our SQL Aggregates Guide
SUM()
, AVG()
, MIN()
, MAX()
Aggregate functions summarize multiple rows into a single value. Analysts use these to calculate total sales, average revenue, min/max values, and more.
SELECT
department,
COUNT(*) AS num_employees,
AVG(salary) AS avg_salary,
MAX(salary) AS top_salary
FROM employees
GROUP BY department;
These pair beautifully with GROUP BY
and help drive the metrics behind dashboards and reports.
CASE WHEN
— Conditional Logic in SQLThe CASE
function adds if-else logic to your queries. It’s perfect for creating derived labels, tiering values, or handling exceptions.
SELECT
order_id,
amount,
CASE
WHEN amount > 1000 THEN 'High'
WHEN amount > 500 THEN 'Medium'
ELSE 'Low'
END AS order_size
FROM orders;
Use CASE
to categorize, rank, or flag data directly in your queries without needing separate transformations.
COALESCE()
— Handle NULL ValuesNULLs are everywhere in real-world data. COALESCE()
lets you replace them with a default.
SELECT
customer_id,
COALESCE(email, 'no_email_found') AS customer_email
FROM customers;
It returns the first non-null value from the list. Great for ensuring cleaner outputs or creating fallback logic.
RANK()
, ROW_NUMBER()
, DENSE_RANK()
— Window Ranking FunctionsNeed to rank users by revenue or find the top 5 products per category? That’s where window functions shine.
SELECT
product_id,
category,
sales,
RANK() OVER (PARTITION BY category ORDER BY sales DESC) AS sales_rank
FROM product_sales;
These functions help with:
Explore more in our Window Function Interview Questions
DATE_TRUNC()
and Other Time FunctionsSQL time functions are key for any analyst working with time-series or business data.
SELECT
DATE_TRUNC('month', created_at) AS month,
COUNT(*) AS new_users
FROM users
GROUP BY month;
Popular ones:
DATE_TRUNC()
– Round to week/month/yearNOW()
– Current timestampAGE()
– Difference between datesEXTRACT()
– Pull year/month/day from a dateJOIN
(INNER, LEFT, RIGHT, FULL)This isn’t a function per se, but it’s critical to data analysis. JOINs allow you to combine data from multiple tables.
SELECT o.order_id, c.customer_name
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id;
Learn to master:
INNER JOIN
– match on both sidesLEFT JOIN
– keep all from the leftRIGHT JOIN
– keep all from the rightFULL JOIN
– keep everything, match where possible📘 Learn more in our Glossary of SQL Terms
Want to practice these SQL functions with real data?
Try Galaxy — a modern, fast, AI-powered SQL workspace that lets you:
👉 Explore Galaxy and start writing better SQL today.
These are the core SQL functions every data analyst should know — whether you're applying for your first data role, prepping for interviews, or leveling up in your job.
By mastering GROUP BY
, CASE
, COALESCE
, COUNT()
, and window functions, you’ll be equipped to tackle real-world data problems with confidence.