๐ง 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;