Skip to main content

Top 10 SQL Queries Every Data Analyst Must Know

Top 10 SQL Queries Every Data Analyst Must Know



PowerBI Course at Rs 99


If you can explain these with confidence, you are interview-ready

SQL interviews and day-to-day analytics work rarely test syntax alone. What matters is how you use queries to answer business questions.

This article covers 10 SQL queries every data analyst must know, explained with real-world examples, not textbook definitions.


1. SELECT with WHERE — Filtering the Right Data


Why it matters:
Almost every analysis starts by narrowing data.

Query:

SELECT *
FROM Orders
WHERE OrderDate >= '2025-01-01';

Real-world use:
Analyze sales for the current year only.


2. ORDER BY — Finding Top or Bottom Performers


Why it matters:
Business users often ask: Top customers? Worst products?

Query:

SELECT ProductName, SalesAmount
FROM Sales
ORDER BY SalesAmount DESC;

Example insight:
Identify the highest-selling product.


3. GROUP BY with Aggregates — Business Summaries


Why it matters:
Executives don’t want raw data; they want summaries.

Query:

SELECT Region, SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY Region;

Use case:
Regional performance dashboard.


4. HAVING — Filtering Aggregated Results


Why it matters:
WHERE filters rows; HAVING filters groups.

Query:

SELECT Region, SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY Region
HAVING SUM(SalesAmount) > 100000;

Business insight:
Regions contributing meaningful revenue.


5. INNER JOIN — Combining Related Tables


Why it matters:
Real data lives in multiple tables.

Query:

SELECT c.CustomerName, o.OrderDate
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID;

Use case:
Customer-wise order history.


6. LEFT JOIN — Finding Missing Data


Why it matters:
Used to identify gaps, not just matches.

Query:

SELECT c.CustomerName
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID
WHERE o.CustomerID IS NULL;

Business question:
Which customers never placed an order?


7. CASE WHEN — Business Logic in SQL


Why it matters:
Turns raw numbers into business-friendly categories.

Query:

SELECT SalesAmount,
CASE
WHEN SalesAmount >= 50000 THEN 'High'
WHEN SalesAmount >= 20000 THEN 'Medium'
ELSE 'Low'
END AS SalesCategory
FROM Sales;

Use case:
Customer or product segmentation.


8. Subquery — Step-by-Step Thinking


Why it matters:
Useful when logic needs to be broken into parts.

Query:

SELECT *
FROM Employees
WHERE Salary >
(SELECT AVG(Salary) FROM Employees);

Insight:
Employees earning above average.


9. Window Function — Running Totals & Rankings


Why it matters:
Powerful analytics without collapsing rows.

Query:

SELECT OrderDate,
SUM(SalesAmount) OVER (ORDER BY OrderDate) AS RunningSales
FROM Sales;

Use case:
Cumulative sales trend.


10. DISTINCT — Eliminating Duplicates


Why it matters:
Clean results = correct insights.

Query:

SELECT DISTINCT CustomerID
FROM Orders;

Business question:
How many unique customers placed orders?


What Interviewers Really Look For

They don’t want:
 ❌ Memorized syntax

They want:
 ✅ Why you used the query
 ✅ What business question it answers
 ✅ How it performs on large data


If you can:

  • Explain GROUP BY vs HAVING
  • Use JOINs correctly
  • Apply CASE and window functions

You are already ahead of most candidates.


PowerBI Course at Rs 99



Comments

Popular posts from this blog

Why Do People Dislike DAX and Data Modeling in Power BI?

Why Do People Dislike DAX and Data Modeling in Power BI? Many Power BI users express frustration with DAX (Data Analysis Expressions) and data modeling , primarily due to their complexity and steep learning curves.  Reasons Why People Dislike DAX Steep Learning Curve : DAX has a syntax that can feel unintuitive for newcomers, especially for those without prior experience in Excel's Power Pivot or similar analytical languages. The concept of row context vs. filter context is often confusing and requires significant effort to master. Complexity of Advanced Calculations : Basic measures like sums and averages are straightforward, but creating advanced measures (e.g., time intelligence, ranking, or cumulative totals) can quickly become overwhelming. Many users struggle with understanding functions like CALCULATE , FILTER , and ALL , which are essential for advanced analytics. Error Handling : DAX error messages are not always clear or descriptive, making it difficult to debug issues ...

Connecting Power BI to Azure Data Lake: Streamlining Big Data Analytics

Connecting Power BI to Azure Data Lake: Streamlining Big Data Analytics Azure Data Lake and Power BI provide a powerful combination for businesses to handle and analyze large datasets efficiently. Here’s a step-by-step breakdown of how connecting Power BI to Azure Data Lake helps streamline big data analytics. 1. What is Azure Data Lake? Azure Data Lake is a cloud-based storage solution designed to handle large volumes of structured and unstructured data. It provides highly scalable and cost-effective storage, making it an ideal choice for big data projects, data lakes, and large-scale analytics. 2. Benefits of Connecting Power BI to Azure Data Lake Handling Large Datasets : Power BI’s integration with Azure Data Lake allows users to work with large datasets without needing to import all the data into Power BI. Instead, users can connect and query data directly. Scalable Analytics : Azure Data Lake’s ability to scale horizontally ensures that it can handle growing volumes of data se...

Leveraging Power BI's Bookmarks and Selections for Interactive Dashboards

Leveraging Power BI's Bookmarks and Selections for Interactive Dashboards Bookmarks and Selections in Power BI are powerful features that can significantly enhance the interactivity and user experience of dashboards. Here's how you can use them effectively: 1. What are Bookmarks in Power BI? Bookmarks capture the current state of a report page, including: Visible or hidden visuals Filter states Slicer selections Sort order, drill state, and focus mode By saving different views of your report with bookmarks, you can create interactive storytelling, custom navigation, and dynamic reports. 2. What is the Selection Pane? The Selection Pane lets you control the visibility of report visuals. Using the pane, you can: Show or hide visuals based on user actions Layer visuals in an orderly manner to control how users interact with them Combine with bookmarks to toggle the visibility of different report components 3. Use Cases for Bookmarks and Selections Here are some common scenarios ...