TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #3110 3.43K
๐Ÿš€ Data Analyst Roadmap โ€” Part 18

๐Ÿง  SQL Level 8 โ€” Advanced Analytical Queries & Business Problems

At this stage, you know the core SQL building blocks:

SELECT โ†’ WHERE โ†’ GROUP BY โ†’ HAVING โ†’ JOIN โ†’ CTE โ†’ Window Functions โ†’ Date Analysis

Now it's time to combine them.

Real Data Analyst work rarely asks: "Write a query using RANK()"

Instead, you'll get business questions like:

โ€ข "Which customers are becoming inactive?"

โ€ข "What are our top-selling products in each category?"

โ€ข "Which month had the highest revenue growth?"

The real skill is converting a business problem into SQL logic.

๐Ÿ”น 1. Start With the Business Question

Before writing SQL, identify:

โ€ข What are we measuring?

โ€ข At what level?

โ€ข Which tables contain the required data?

โ€ข What filters are needed?

โ€ข Do we need aggregation?

โ€ข Do we need ranking or comparison?

For example: "Find the top 3 products in every category."

Break it down: Product โ†’ Category โ†’ Sales โ†’ Rank within Category โ†’ Keep Top 3

๐Ÿ”น 2. Find the Correct Grain

Grain means: What does one row represent?

โ€ข Orders โ†’ one row per order

โ€ข Order_Items โ†’ one row per product within an order

โ€ข Customers โ†’ one row per customer

If you don't understand the grain, you can accidentally double-count revenue.

๐Ÿ”น 3. Revenue by Customer

SELECT
c.Customer_ID,
c.Customer_Name,
SUM(o.Sales) AS Total_Sales
FROM Customers c
JOIN Orders o ON c.Customer_ID = o.Customer_ID
GROUP BY c.Customer_ID, c.Customer_Name;


๐Ÿ”น 4. Rank Customers by Revenue

WITH Customer_Sales AS (
SELECT Customer_ID, SUM(Sales) AS Total_Sales
FROM Orders GROUP BY Customer_ID
)
SELECT
Customer_ID,
Total_Sales,
RANK() OVER (ORDER BY Total_Sales DESC) AS Sales_Rank
FROM Customer_Sales;


๐Ÿ”น 5. Top 3 Customers in Each Region

WITH Customer_Sales AS (
SELECT Customer_ID, Region, SUM(Sales) AS Total_Sales
FROM Orders GROUP BY Customer_ID, Region
),
Ranked_Customers AS (
SELECT *, RANK() OVER (PARTITION BY Region ORDER BY Total_Sales DESC) AS Sales_Rank
FROM Customer_Sales
)

SELECT * FROM Ranked_Customers WHERE Sales_Rank <= 3;


๐Ÿ”น 6. Finding the Second-Highest Salary

WITH Ranked_Employees AS (
SELECT Employee, Salary,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS Salary_Rank
FROM Employees
)

SELECT Employee, Salary FROM Ranked_Employees WHERE Salary_Rank = 2;


๐Ÿ”น 7. Find Products That Never Sold

SELECT p.Product_ID, p.Product_Name
FROM Products p
LEFT JOIN Order_Items oi ON p.Product_ID = oi.Product_ID
WHERE oi.Product_ID IS NULL;


๐Ÿ”น 8. Customers With No Orders

SELECT c.Customer_ID, c.Customer_Name
FROM Customers c
LEFT JOIN Orders o ON c.Customer_ID = o.Customer_ID
WHERE o.Customer_ID IS NULL;


๐Ÿ”น 9. Customers Above Average Spending

WITH Customer_Sales AS (
SELECT Customer_ID, SUM(Sales) AS Total_Sales
FROM Orders GROUP BY Customer_ID
)
SELECT Customer_ID, Total_Sales FROM Customer_Sales
WHERE Total_Sales > (SELECT AVG(Total_Sales) FROM Customer_Sales);


๐Ÿ”น 10. Month-over-Month Sales Growth

WITH Monthly_Sales AS (
SELECT EXTRACT(YEAR FROM Order_Date) AS Year,
EXTRACT(MONTH FROM Order_Date) AS Month,
SUM(Sales) AS Total_Sales
FROM Orders GROUP BY 1, 2
),
Comparison AS (
SELECT Year, Month, Total_Sales,
LAG(Total_Sales) OVER (ORDER BY Year, Month) AS Previous_Sales
FROM Monthly_Sales
)

SELECT Year, Month, Total_Sales, Previous_Sales,
Total_Sales - Previous_Sales AS Sales_Change
FROM Comparison;
  • โค 4
More from @sqlspecialist
  1. Oct 4, 20269๏ธโƒฃ How would you calculate month-over-month growth? Sample Answer: โ€œI would first retrievโ€ฆ
  2. Oct 4, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 3 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Sep 29, 2026๐Ÿ”Ÿ How would you find duplicate records in SQL? Sample Answer: "I would first identify theโ€ฆ
  4. Sep 29, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 2 Guys, let's continue our Data Analyst Interviewโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026"After identifying duplicates, I investigate whether they are genuine duplicate records orโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’