Skip to main content

Command Palette

Search for a command to run...

10 SQL Queries Every Data Analyst Must Know

Updated
4 min read
10 SQL Queries Every Data Analyst Must Know
N
Simplifying AI, Machine Learning & Data Science for beginners. Free cheat sheets, roadmaps & resources to help you start your data journey — no CS degree needed.

SQL is the language of data. Every dataset lives in a database. Every analyst uses SQL daily.

Here are the 10 most important SQL queries you need to know — with real examples and use cases.


1. SELECT — Pull the Data You Need

The most basic query. But most people don't use it efficiently.

Example:

SELECT name, salary, department
FROM employees;

Use case: Pulling specific columns instead of everything — keeps your queries clean and fast.


2. WHERE — Filter Your Results

Don't pull the whole table. Pull only what matters.

Example:

SELECT name, salary
FROM employees
WHERE department = 'Data Science'
AND salary > 50000;

Use case: Filtering customers by region, product by category, or employees by department.


3. GROUP BY + COUNT — Find Patterns Fast

This is where analysis starts.

Example:

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
ORDER BY total_employees DESC;

Use case: Finding which department has the most employees, which product gets most orders.


4. SUM and AVG — Calculate Totals and Averages

The two aggregation functions you'll use every single day.

Example:

SELECT department,
       SUM(salary) AS total_salary,
       AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

Use case: Calculating total revenue per region, average order value per product.


5. JOIN — Combine Two Tables

Real data is never in one table. Joins are non-negotiable.

Example:

SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.id;

Use case: Combining a sales table with a customer table to see who bought what.


6. LEFT JOIN — Keep All Records From the First Table

Use this when you want everything from the left table, even if there's no match.

Example:

SELECT customers.name, orders.order_id
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id;

Use case: Finding customers who have never placed an order.


7. HAVING — Filter After Grouping

WHERE filters before grouping. HAVING filters after. Most beginners mix these up.

Example:

SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;

Use case: Showing only departments with more than 10 employees.


8. CASE WHEN — Add Conditions Inside a Query

SQL's version of an if-else statement.

Example:

SELECT name, salary,
  CASE
    WHEN salary > 80000 THEN 'High'
    WHEN salary > 50000 THEN 'Medium'
    ELSE 'Low'
  END AS salary_band
FROM employees;

Use case: Creating salary bands, labeling customer tiers, categorizing order sizes.


9. SUBQUERY — Query Inside a Query

Use when you need to filter based on a calculated result.

Example:

SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

Use case: Finding employees who earn above the company average.


10. ROW_NUMBER() — Rank Your Results

A window function. One of the most powerful tools in SQL.

Example:

SELECT name, salary,
  ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;

Use case: Finding the top earner in each department, ranking products by sales per region.


Quick Reference — Save This

Query What It Does
SELECT Pull specific columns
WHERE Filter rows before grouping
GROUP BY + COUNT Count records by category
SUM / AVG Calculate totals and averages
INNER JOIN Combine matching rows from two tables
LEFT JOIN Keep all rows from left table
HAVING Filter rows after grouping
CASE WHEN Add if-else logic inside SQL
SUBQUERY Filter using a calculated result
ROW_NUMBER() Rank rows within a group

One Important Rule

SQL queries do NOT change your original data. SELECT only reads. Always test on a small filter before running on a full table.

Save this article and come back to it every time you sit down with a new dataset.


More from this blog

N

Neural Notes

19 posts