Top SQL Functions Every Data Analyst Should Know

Learn essential SQL functions like COALESCE, CASE, GROUP BY, and aggregate functions every data analyst should know — with real examples.

April 17, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

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.

🔢 1. COUNT() — Count Rows in SQL

This 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.

📊 2. GROUP BY — Aggregate Your Data

GROUP 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

📦 3. SQL Aggregate Functions — 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.

🎯 4. CASE WHEN — Conditional Logic in SQL

The 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.

🧩 5. COALESCE() — Handle NULL Values

NULLs 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.

🪜 6. RANK(), ROW_NUMBER(), DENSE_RANK() — Window Ranking Functions

Need 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:

  • Leaderboards
  • Percentile rankings
  • Retention calculations
  • Deduplication

Explore more in our Window Function Interview Questions

⏱ 7. DATE_TRUNC() and Other Time Functions

SQL 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/year
  • NOW() – Current timestamp
  • AGE() – Difference between dates
  • EXTRACT() – Pull year/month/day from a date

🔄 8. JOIN (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 sides
  • LEFT JOIN – keep all from the left
  • RIGHT JOIN – keep all from the right
  • FULL JOIN – keep everything, match where possible

📘 Learn more in our Glossary of SQL Terms

💡 Bonus: Use These in Galaxy

Want to practice these SQL functions with real data?

Try Galaxy — a modern, fast, AI-powered SQL workspace that lets you:

  • Write and test queries instantly
  • Collaborate with others on shared queries
  • Explore and analyze your own database
  • Practice with built-in examples

👉 Explore Galaxy and start writing better SQL today.

✍️ Final Thoughts

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.

🔗 More Resources

Ourv0.1-alphais coming in April 2025.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other blog posts!

Trusted by top engineers on high-velocity teams

Aryeo Logo
Assort Health
Curri
Rubie
Comulate
Truvideo Logo