๐ 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
โข This approach prevents complicated SQL from becoming confusing.
๐น 2. Find the Correct Grain
One of the most important analytical concepts is grain.
Grain means: What does one row represent?
For example:
โข 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
Suppose you have:
Customers
โข Customer_ID
โข Customer_Name
and Orders
โข Order_ID
โข Customer_ID
โข Order_Date
โข Sales
You can calculate customer revenue:
โข Now you have one row per customer.
๐น 4. Rank Customers by Revenue
Combine aggregation with a window function:
โข This answers: Who are our highest-value customers?
๐น 5. Top 3 Customers in Each Region
Now add another business dimension.
โข This is a classic advanced SQL interview problem.
๐น 6. Finding the Second-Highest Salary
A common interview question.
โข Using DENSE_RANK() is useful when multiple employees share the same salary.
๐น 7. Find Products That Never Sold
This is a classic LEFT JOIN problem.
Business interpretation: Products exist in the catalog but have no sales.
This could indicate:
โข Poor demand
โข Pricing problems
โข Inventory issues
โข Product visibility problems
๐น 8. Customers With No Orders
The same logic can identify customers who have never purchased.
๐ง 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
โข This approach prevents complicated SQL from becoming confusing.
๐น 2. Find the Correct Grain
One of the most important analytical concepts is grain.
Grain means: What does one row represent?
For example:
โข 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
Suppose you have:
Customers
โข Customer_ID
โข Customer_Name
and Orders
โข Order_ID
โข Customer_ID
โข Order_Date
โข Sales
You can calculate customer revenue:
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;
โข Now you have one row per customer.
๐น 4. Rank Customers by Revenue
Combine aggregation with a window function:
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;
โข This answers: Who are our highest-value customers?
๐น 5. Top 3 Customers in Each Region
Now add another business dimension.
WITH Customer_Sales AS (
SELECT Customer_ID, Region, SUM(Sales) AS Total_Sales
FROM Orders
GROUP BY Customer_ID, Region
),
Ranked_Customers AS (
SELECT
Customer_ID, Region, Total_Sales,
RANK() OVER (PARTITION BY Region ORDER BY Total_Sales DESC) AS Sales_Rank
FROM Customer_Sales
)
SELECT * FROM Ranked_Customers WHERE Sales_Rank <= 3;
โข This is a classic advanced SQL interview problem.
๐น 6. Finding the Second-Highest Salary
A common interview question.
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;
โข Using DENSE_RANK() is useful when multiple employees share the same salary.
๐น 7. Find Products That Never Sold
This is a classic LEFT JOIN problem.
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;
Business interpretation: Products exist in the catalog but have no sales.
This could indicate:
โข Poor demand
โข Pricing problems
โข Inventory issues
โข Product visibility problems
๐น 8. Customers With No Orders
The same logic can identify customers who have never purchased.
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;
โค6
โข This can help marketing teams identify potential customers who need activation campaigns.
๐น 9. Customers Above Average Spending
First calculate customer totals. Then compare them with the overall average.
โข Notice how concepts from earlier parts work together: CTE + Aggregation + Subquery
๐น 10. Month-over-Month Sales Growth
First aggregate sales by month. Then use LAG().
โข This produces a time-based comparison instead of just a total.
๐น 11. Calculate Growth Percentage
You can extend the previous query:
โข NULLIF() is important because it prevents division-by-zero errors.
โข A practical analyst must always think about edge cases.
๐น 12. Find the Latest Order for Every Customer
From Part 17, combine PARTITION BY + ORDER BY + ROW_NUMBER()
This is useful for:
โข Customer activity
โข Churn analysis
โข Last purchase reporting
โข CRM segmentation
๐น 13. Identify Potentially Inactive Customers
First find the last purchase date:
Then compare the last order against a chosen inactivity threshold.
โข The business rule might be: No purchase for 90 days โ Potentially inactive
โข The important lesson: SQL provides the calculation. The business defines what "inactive" means.
๐น 14. Find Duplicate Records
Window functions are excellent for detecting duplicates.
โข These records can then be investigated before cleaning the dataset.
๐น 15. Combining Multiple Tables
Real analysis often requires several joins.
For example: Customers โ Orders โ Order_Items โ Products
๐น 9. Customers Above Average Spending
First calculate customer totals. Then compare them with the overall average.
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);
โข Notice how concepts from earlier parts work together: CTE + Aggregation + Subquery
๐น 10. Month-over-Month Sales Growth
First aggregate sales by month. Then use LAG().
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 EXTRACT(YEAR FROM Order_Date), EXTRACT(MONTH FROM Order_Date)
),
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;
โข This produces a time-based comparison instead of just a total.
๐น 11. Calculate Growth Percentage
You can extend the previous query:
(Total_Sales - Previous_Sales) / NULLIF(Previous_Sales, 0) * 100
โข NULLIF() is important because it prevents division-by-zero errors.
โข A practical analyst must always think about edge cases.
๐น 12. Find the Latest Order for Every Customer
From Part 17, combine PARTITION BY + ORDER BY + ROW_NUMBER()
WITH Ranked_Orders AS (
SELECT Customer_ID, Order_ID, Order_Date,
ROW_NUMBER() OVER (PARTITION BY Customer_ID ORDER BY Order_Date DESC) AS rn
FROM Orders
)
SELECT Customer_ID, Order_ID, Order_Date FROM Ranked_Orders WHERE rn = 1;
This is useful for:
โข Customer activity
โข Churn analysis
โข Last purchase reporting
โข CRM segmentation
๐น 13. Identify Potentially Inactive Customers
First find the last purchase date:
SELECT Customer_ID, MAX(Order_Date) AS Last_Order_Date FROM Orders GROUP BY Customer_ID;
Then compare the last order against a chosen inactivity threshold.
โข The business rule might be: No purchase for 90 days โ Potentially inactive
โข The important lesson: SQL provides the calculation. The business defines what "inactive" means.
๐น 14. Find Duplicate Records
Window functions are excellent for detecting duplicates.
WITH Duplicate_Check AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Customer_ID, Order_Date, Sales ORDER BY Order_ID) AS rn
FROM Orders
)
SELECT * FROM Duplicate_Check WHERE rn > 1;
โข These records can then be investigated before cleaning the dataset.
๐น 15. Combining Multiple Tables
Real analysis often requires several joins.
For example: Customers โ Orders โ Order_Items โ Products
SELECT c.Customer_ID, c.Customer_Name, p.Product_Name, oi.Quantity, oi.Sales
FROM Customers c
JOIN Orders o ON c.Customer_ID = o.Customer_ID
JOIN Order_Items oi ON o.Order_ID = oi.Order_ID
JOIN Products p ON oi.Product_ID = p.Product_ID;
โค3
โ ๏ธ Every additional join can change the number of rows.
โข Always check whether the resulting grain is still what you expect.
๐น 16. The Most Important Analytical Pattern
Many advanced SQL problems can be solved using this structure:
โข
1. Filter the raw data โ 2. Join required tables โ 3. Aggregate to the correct grain โ 4. Apply window functions โ 5. Filter the analytical result โ 6. Present the final output
For example:
โข Orders โ Filter โ GROUP BY Customer โ Calculate Sales โ RANK() โ Keep Top 3 โ Final Report
โข Learning this thought process is more valuable than memorizing individual queries.
๐ผ Real-World Business Problems You Should Practice
Sales
โ Top 5 products by revenue
โ Top products within each category
โ Month with highest sales
โ Revenue growth by month
โ Customers contributing most revenue
Customers
โ Customers with no orders
โ Customers with declining purchases
โ Most recent purchase per customer
โ Repeat customers
โ Average order value per customer
Operations
โ Orders taking longer than expected
โ Products never sold
โ Duplicate transactions
โ Most active regions
โ Employees with above-average performance
๐ฏ SQL Interview Challenge
Question: Find the highest-selling product in each category.
Think about the problem before writing the query:
โข Product โ Category โ Total Sales โ Rank Within Category โ Keep Rank 1
A possible solution:
๐ก Double Tap โค๏ธ For More
โข Always check whether the resulting grain is still what you expect.
๐น 16. The Most Important Analytical Pattern
Many advanced SQL problems can be solved using this structure:
โข
1. Filter the raw data โ 2. Join required tables โ 3. Aggregate to the correct grain โ 4. Apply window functions โ 5. Filter the analytical result โ 6. Present the final output
For example:
โข Orders โ Filter โ GROUP BY Customer โ Calculate Sales โ RANK() โ Keep Top 3 โ Final Report
โข Learning this thought process is more valuable than memorizing individual queries.
๐ผ Real-World Business Problems You Should Practice
Sales
โ Top 5 products by revenue
โ Top products within each category
โ Month with highest sales
โ Revenue growth by month
โ Customers contributing most revenue
Customers
โ Customers with no orders
โ Customers with declining purchases
โ Most recent purchase per customer
โ Repeat customers
โ Average order value per customer
Operations
โ Orders taking longer than expected
โ Products never sold
โ Duplicate transactions
โ Most active regions
โ Employees with above-average performance
๐ฏ SQL Interview Challenge
Question: Find the highest-selling product in each category.
Think about the problem before writing the query:
โข Product โ Category โ Total Sales โ Rank Within Category โ Keep Rank 1
A possible solution:
WITH Product_Sales AS (
SELECT Product_ID, Category, SUM(Sales) AS Total_Sales
FROM Product_Sales_Data
GROUP BY Product_ID, Category
),
Ranked_Products AS (
SELECT Product_ID, Category, Total_Sales,
RANK() OVER (PARTITION BY Category ORDER BY Total_Sales DESC) AS Sales_Rank
FROM Product_Sales
)
SELECT Product_ID, Category, Total_Sales FROM Ranked_Products WHERE Sales_Rank = 1;
๐ก Double Tap โค๏ธ For More
โค9
๐ ๐๐ฅ๐๐ ๐ฅ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ๐ ๐๐ผ ๐๐ฒ๐ฎ๐ฟ๐ป ๐๐ฎ๐๐ฎ ๐๐ป๐ฎ๐น๐๐๐ถ๐ฐ๐ ๐
Want to build a career in Data Analytics but donโt know where to start? Learn the most important skills completely FREE with these expert YouTube resources.
๐ฅ Learn โ Practice โ Build Projects โ Become Job-Ready
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4ysm4XS
๐ฏ Perfect for Students โข Freshers โข Job Seekers โข Aspiring Data Analysts
Want to build a career in Data Analytics but donโt know where to start? Learn the most important skills completely FREE with these expert YouTube resources.
๐ฅ Learn โ Practice โ Build Projects โ Become Job-Ready
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4ysm4XS
๐ฏ Perfect for Students โข Freshers โข Job Seekers โข Aspiring Data Analysts
โค6
๐ ๐ง๐๐ง๐ ๐๐ฟ๐ผ๐๐ฝ ๐๐ฅ๐๐ ๐ฉ๐ถ๐ฟ๐๐๐ฎ๐น ๐๐ป๐๐ฒ๐ฟ๐ป๐๐ต๐ถ๐ฝ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐ ๐
Tata Group/TCS virtual job simulations let you work through industry-style tasks and strengthen your resume.
๐ 3 FREE Virtual Programs:
๐ Data Visualisation
๐ Cybersecurity
๐ฑ ESG (Environmental, Social & Governance)
๐ป Virtual & flexible
๐ Free Certificate on Completion
๐ Add the experience to your Resume/LinkedIn
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4yoXEOI
๐ฅ Perfect for Students โข Freshers โข Job Seekers
Tata Group/TCS virtual job simulations let you work through industry-style tasks and strengthen your resume.
๐ 3 FREE Virtual Programs:
๐ Data Visualisation
๐ Cybersecurity
๐ฑ ESG (Environmental, Social & Governance)
๐ป Virtual & flexible
๐ Free Certificate on Completion
๐ Add the experience to your Resume/LinkedIn
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4yoXEOI
๐ฅ Perfect for Students โข Freshers โข Job Seekers
๐ Data Analyst Roadmap โ Part 19
๐ง SQL Level 9 โ Query Performance & Optimization
Knowing how to write a SQL query is one skill. Knowing how to write a query that is correct, efficient, scalable, and easy to maintain is another.
As a Data Analyst, you may eventually work with tables containing:
๐ Millions of rows
๐ฆ Large transaction datasets
๐ฅ Millions of customers
๐ Years of historical data
A query that works perfectly on 10,000 rows may become extremely slow on 100 million rows. That's why understanding SQL performance matters.
๐น 1. What Is Query Optimization?
Query optimization means improving a query so that it:
โ Returns the correct result
โ Uses fewer resources
โ Processes less unnecessary data
โ Executes faster
โ Scales better as data grows
๐น 2. Select Only the Columns You Need
Avoid:
when you only need:
โข Read unnecessary columns
โข Increase data transfer
โข Make queries harder to maintain
โข Become problematic when tables change
For analytical work, explicitly selecting required columns is usually better.
๐น 3. Filter Data Early
Suppose you only need 2026 orders:
Filtering reduces the amount of data that later operations need to process.
๐น 4. Understand Indexes
An index is a database structure that can help locate rows more efficiently. Think of it like the index of a book.
Without an index: Database โ potentially scan many rows
With a useful index: Database โ locate relevant data more efficiently
Indexes can be especially useful for columns frequently used in:
Example:
โ ๏ธ Index syntax and behavior vary by database.
๐น 5. Indexes Are Not Always Better
Indexes have costs. They can:
Consume storage, slow down
Therefore:
Don't create indexes on every column. Database systems and workloads determine which indexes are useful.
๐น 6. Index Columns Used for JOINs
The join depends on:
Appropriate indexing can improve join performance, especially for large tables. But the database optimizer decides whether an index actually provides a benefit.
๐น 7. Avoid Functions on Filtered Columns When Possible
Consider:
A range condition is often preferable:
Why?
Applying a function to every row can make it harder for some database systems to use an index efficiently. This is called maintaining sargability.
๐น 8. What Is Sargability?
A condition is generally considered sargable when the database can efficiently use an index to find matching rows.
Compare:
with:
The first form usually gives the optimizer more opportunity to use an index on
๐น 9. Use "EXPLAIN"
One of the most important tools for understanding query performance is:
๐ง SQL Level 9 โ Query Performance & Optimization
Knowing how to write a SQL query is one skill. Knowing how to write a query that is correct, efficient, scalable, and easy to maintain is another.
As a Data Analyst, you may eventually work with tables containing:
๐ Millions of rows
๐ฆ Large transaction datasets
๐ฅ Millions of customers
๐ Years of historical data
A query that works perfectly on 10,000 rows may become extremely slow on 100 million rows. That's why understanding SQL performance matters.
๐น 1. What Is Query Optimization?
Query optimization means improving a query so that it:
โ Returns the correct result
โ Uses fewer resources
โ Processes less unnecessary data
โ Executes faster
โ Scales better as data grows
๐น 2. Select Only the Columns You Need
Avoid:
SELECT * FROM Orders;
when you only need:
SELECT Order_ID, Customer_ID, Sales FROM Orders;
SELECT * can:โข Read unnecessary columns
โข Increase data transfer
โข Make queries harder to maintain
โข Become problematic when tables change
For analytical work, explicitly selecting required columns is usually better.
๐น 3. Filter Data Early
Suppose you only need 2026 orders:
SELECT Customer_ID, SUM(Sales) AS Total_Sales
FROM Orders
WHERE Order_Date >= '2026-01-01'
AND Order_Date < '2027-01-01'
GROUP BY Customer_ID;
Filtering reduces the amount of data that later operations need to process.
๐น 4. Understand Indexes
An index is a database structure that can help locate rows more efficiently. Think of it like the index of a book.
Without an index: Database โ potentially scan many rows
With a useful index: Database โ locate relevant data more efficiently
Indexes can be especially useful for columns frequently used in:
WHERE, JOIN, ORDER BY, and sometimes GROUP BY.Example:
CREATE INDEX idx_orders_customer ON Orders(Customer_ID);
โ ๏ธ Index syntax and behavior vary by database.
๐น 5. Indexes Are Not Always Better
Indexes have costs. They can:
Consume storage, slow down
INSERT, UPDATE, DELETE, and require maintenance.Therefore:
Don't create indexes on every column. Database systems and workloads determine which indexes are useful.
๐น 6. Index Columns Used for JOINs
SELECT c.Customer_Name, o.Sales
FROM Customers c
JOIN Orders o ON c.Customer_ID = o.Customer_ID;
The join depends on:
Customer_ID.Appropriate indexing can improve join performance, especially for large tables. But the database optimizer decides whether an index actually provides a benefit.
๐น 7. Avoid Functions on Filtered Columns When Possible
Consider:
WHERE YEAR(Order_Date) = 2026
A range condition is often preferable:
WHERE Order_Date >= '2026-01-01' AND Order_Date < '2027-01-01'
Why?
Applying a function to every row can make it harder for some database systems to use an index efficiently. This is called maintaining sargability.
๐น 8. What Is Sargability?
A condition is generally considered sargable when the database can efficiently use an index to find matching rows.
Compare:
WHERE Order_Date >= '2026-01-01'
with:
WHERE YEAR(Order_Date) = 2026
The first form usually gives the optimizer more opportunity to use an index on
Order_Date.๐น 9. Use "EXPLAIN"
One of the most important tools for understanding query performance is:
EXPLAIN
SELECT * FROM Orders WHERE Customer_ID = 1001;
โค4
Depending on the database, you may also see:
These tools can show how the database plans to execute or actually executes the query.
You may encounter concepts such as: Table scan, Index scan, Index seek, Join strategy, Sort, Aggregate, Estimated rows, Actual rows, Execution cost.
๐น 10. Table Scan vs Index Access
A table scan may require reading a large portion of a table. An index-based access path can sometimes locate matching records much more efficiently.
But: โ ๏ธ A table scan is not automatically bad.
If you need most of the table, scanning it may actually be the best strategy.
The optimizer chooses based on: Data size + statistics + indexes + query conditions + database engine.
๐น 11. Avoid Unnecessary "DISTINCT"
This query:
may be perfectly valid.
But don't use
๐น 12. Be Careful With JOIN Multiplication
Suppose:
Customer A โ 10 Orders and each order has 5 Order Items
Joining
If you then calculate:
This is a logic problem, not just a performance problem. Always understand the grain before joining and aggregating.
๐น 13. "UNION" vs "UNION ALL"
"UNION" removes duplicates.
"UNION ALL" simply combines the results:
If you don't need duplicate removal,
๐น 14. Avoid Repeating Expensive Logic
Suppose the same complex calculation appears multiple times.
Instead of repeating it, consider using: CTEs, Derived tables, Temporary tables, Views, Pre-aggregated tables.
The objective is: Calculate expensive logic only when necessary.
๐น 15. CTEs and Performance
CTEs make SQL much easier to organize:
A CTE is primarily a query organization technique. Whether it improves performance depends on the database engine, query structure, materialization behavior, and optimizer.
๐น 16. Reduce Data Before Expensive Operations
A useful analytical pattern is:
Raw Data โ Filter โ Select Required Columns โ Join โ Aggregate โ Window Function โ Final Result
The exact optimal order depends on the query, but the principle is: Don't process more data than necessary.
๐น 17. Avoid Correlated Subqueries When a Better Approach Exists
A correlated subquery may conceptually execute in relation to each outer row:
Sometimes a CTE + join or window function can express the same business logic more efficiently:
EXPLAIN ANALYZE
These tools can show how the database plans to execute or actually executes the query.
You may encounter concepts such as: Table scan, Index scan, Index seek, Join strategy, Sort, Aggregate, Estimated rows, Actual rows, Execution cost.
๐น 10. Table Scan vs Index Access
A table scan may require reading a large portion of a table. An index-based access path can sometimes locate matching records much more efficiently.
But: โ ๏ธ A table scan is not automatically bad.
If you need most of the table, scanning it may actually be the best strategy.
The optimizer chooses based on: Data size + statistics + indexes + query conditions + database engine.
๐น 11. Avoid Unnecessary "DISTINCT"
This query:
SELECT DISTINCT Customer_ID FROM Orders;
may be perfectly valid.
But don't use
DISTINCT simply to hide duplicate rows created by an incorrect join.๐น 12. Be Careful With JOIN Multiplication
Suppose:
Customer A โ 10 Orders and each order has 5 Order Items
Joining
customers โ orders โ order items can produce many rows. If you then calculate:
SUM(Order_Sales) you could accidentally multiply the order-level sales.This is a logic problem, not just a performance problem. Always understand the grain before joining and aggregating.
๐น 13. "UNION" vs "UNION ALL"
"UNION" removes duplicates.
SELECT Customer_ID FROM Current_Customers
UNION
SELECT Customer_ID FROM Previous_Customers;
"UNION ALL" simply combines the results:
SELECT Customer_ID FROM Current_Customers
UNION ALL
SELECT Customer_ID FROM Previous_Customers;
If you don't need duplicate removal,
UNION ALL is generally preferable because it avoids the additional deduplication work.๐น 14. Avoid Repeating Expensive Logic
Suppose the same complex calculation appears multiple times.
Instead of repeating it, consider using: CTEs, Derived tables, Temporary tables, Views, Pre-aggregated tables.
The objective is: Calculate expensive logic only when necessary.
๐น 15. CTEs and Performance
CTEs make SQL much easier to organize:
WITH Customer_Sales AS (
SELECT Customer_ID, SUM(Sales) AS Total_Sales
FROM Orders
GROUP BY Customer_ID
)
SELECT * FROM Customer_Sales WHERE Total_Sales > 10000;
A CTE is primarily a query organization technique. Whether it improves performance depends on the database engine, query structure, materialization behavior, and optimizer.
๐น 16. Reduce Data Before Expensive Operations
A useful analytical pattern is:
Raw Data โ Filter โ Select Required Columns โ Join โ Aggregate โ Window Function โ Final Result
The exact optimal order depends on the query, but the principle is: Don't process more data than necessary.
๐น 17. Avoid Correlated Subqueries When a Better Approach Exists
A correlated subquery may conceptually execute in relation to each outer row:
SELECT e.Employee, e.Salary
FROM Employees e
WHERE e.Salary > (
SELECT AVG(e2.Salary)
FROM Employees e2
WHERE e2.Department = e.Department
);
Sometimes a CTE + join or window function can express the same business logic more efficiently:
WITH Employee_Data AS (
SELECT
Employee,
Department,
Salary,
AVG(Salary) OVER (PARTITION BY Department) AS Avg_Dept_Salary
FROM Employees
)
SELECT Employee, Department, Salary
FROM Employee_Data
WHERE Salary > Avg_Dept_Salary;
โค6
๐น 18. Query Readability Also Matters
A query can be technically fast but difficult to understand.
Bad analytical SQL often contains:
โ Unclear aliases, repeated logic, huge nested queries, unnecessary columns, unnecessary joins, no explanation of business logic
Good SQL should be:
โ Correct, efficient, readable, maintainable, easy to troubleshoot
๐น 19. SQL Optimization Checklist
Before finalizing a query, ask:
1. Do I need all these columns?
2. Am I processing unnecessary rows?
3. Are my joins using the correct keys?
4. Did the join change the grain?
5. Am I accidentally multiplying values?
6. Can a date filter be written as a range?
7. Do I really need "DISTINCT"?
8. Could "UNION ALL" be used instead of "UNION"?
9. Can I inspect the execution plan?
10. Will this query still perform well on a much larger dataset?
๐ฏ SQL Interview Challenge
Question:
A query takes 30 seconds:
How could you improve it?
A better approach is:
Why?
โ Avoids unnecessary columns
โ Uses a range filter
โ Can be more index-friendly
โ Clearly defines the required period
Then use
๐ง Double Tap โค๏ธ For More
A query can be technically fast but difficult to understand.
Bad analytical SQL often contains:
โ Unclear aliases, repeated logic, huge nested queries, unnecessary columns, unnecessary joins, no explanation of business logic
Good SQL should be:
โ Correct, efficient, readable, maintainable, easy to troubleshoot
๐น 19. SQL Optimization Checklist
Before finalizing a query, ask:
1. Do I need all these columns?
2. Am I processing unnecessary rows?
3. Are my joins using the correct keys?
4. Did the join change the grain?
5. Am I accidentally multiplying values?
6. Can a date filter be written as a range?
7. Do I really need "DISTINCT"?
8. Could "UNION ALL" be used instead of "UNION"?
9. Can I inspect the execution plan?
10. Will this query still perform well on a much larger dataset?
๐ฏ SQL Interview Challenge
Question:
A query takes 30 seconds:
SELECT * FROM Orders WHERE YEAR(Order_Date) = 2026;
How could you improve it?
A better approach is:
SELECT Order_ID, Customer_ID, Order_Date, Sales
FROM Orders
WHERE Order_Date >= '2026-01-01'
AND Order_Date < '2027-01-01';
Why?
โ Avoids unnecessary columns
โ Uses a range filter
โ Can be more index-friendly
โ Clearly defines the required period
Then use
EXPLAIN/EXPLAIN ANALYZE to verify the actual execution plan.๐ง Double Tap โค๏ธ For More
โค6๐2
๐ ๐ง๐ผ๐ฝ ๐ง๐ฒ๐ฐ๐ต ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป๐ ๐๐ผ ๐๐ฎ๐ป๐ฑ ๐๐ถ๐ด๐ต-๐ฃ๐ฎ๐๐ถ๐ป๐ด ๐๐ผ๐ฏ๐ ๐ถ๐ป ๐ฎ๐ฌ๐ฎ๐ฒ๐
๐ฐ Highest Salary: โน41 LPA
๐ Average Salary: โน7.4 LPA
๐ 2,000+ Students Placed
๐ข 500+ Hiring Partners
๐ป Full Stack :- https://pdlink.in/3SuUeuD
๐ Data Analytics :- https://pdlink.in/45vk5ph
๐ซAI Engineering :- https://pdlink.in/4fWJVID
๐ฅ Take the first step towards your high-paying tech career in 2026!
๐ฐ Highest Salary: โน41 LPA
๐ Average Salary: โน7.4 LPA
๐ 2,000+ Students Placed
๐ข 500+ Hiring Partners
๐ป Full Stack :- https://pdlink.in/3SuUeuD
๐ Data Analytics :- https://pdlink.in/45vk5ph
๐ซAI Engineering :- https://pdlink.in/4fWJVID
๐ฅ Take the first step towards your high-paying tech career in 2026!
โค2
๐๐ฎ๐๐ฎ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ ๐๐ฅ๐๐ ๐ข๐ป๐น๐ถ๐ป๐ฒ ๐ ๐ฎ๐๐๐ฒ๐ฟ๐ฐ๐น๐ฎ๐๐ ๐
๐ซAccelerate your career in Data Science
๐ซDiscover the skills, tools and career roadmap needed to enter this high-demand field.
๐ฅ Beginner-friendly online sessionโno prior experience required!
๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/46adC3l
(Only few slots left )
๐ Date: September 11, 2026
โฐ Time: 7:00 PM
๐ซAccelerate your career in Data Science
๐ซDiscover the skills, tools and career roadmap needed to enter this high-demand field.
๐ฅ Beginner-friendly online sessionโno prior experience required!
๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/46adC3l
(Only few slots left )
๐ Date: September 11, 2026
โฐ Time: 7:00 PM
๐ Data Analyst Roadmap โ Part 20
๐ง SQL Level 10 โ Cohort Analysis, Retention & Customer Analytics
Now we're moving from writing SQL queries to using SQL for real analytical problems.
Customer analytics is one of the most important areas because businesses want to know:
๐ฅ Who are our customers?
๐ When did they first purchase?
๐ Do they come back?
๐ When do they stop returning?
๐ฐ Which customers generate most revenue?
๐ How does behavior change over time?
One of the most powerful techniques for this is Cohort Analysis.
๐น 1. What Is Cohort Analysis?
A cohort is a group who share a common starting point.
โข Jan 2026 Cohort = first purchase in Jan 2026
โข Feb 2026 Cohort = first purchase in Feb 2026
Instead of mixing everyone, we track each group over time.
๐น 2. Why It Matters
Suppose total monthly customers are increasing.
That sounds positive.
But what if new customers are increasing while existing customers stop returning?
A simple monthly report may hide this problem.
Cohort analysis separates New vs Returning customers.
This makes retention problems much easier to identify.
๐น 3. Step 1 โ Find Each Customer's First Purchase
This gives us the first purchase date for every customer.
๐น 4. Step 2 โ Assign a Cohort Month
We can convert the first purchase into a month-level cohort.
The exact month-truncation syntax varies between SQL databases.
๐น 5. Step 3 โ Join Cohort Back to Orders
Now we need both:
Customer's cohort
and
Customer's subsequent activity
Now every transaction knows which cohort the customer belongs to.
๐น 6. Cohort Month vs Activity Month
โข Cohort Month: When first purchased
โข Activity Month: When purchase happened
๐น 7. Measuring Retention
Retention measures how many customers from a cohort remain active in later periods.
Retention = Active in Period / Original Cohort * 100
โข Jan cohort: 100 customers
โข Feb: 60 active โ 60%
โข Mar: 40 active โ 40%
๐น 8. Retention Month
Months Since Cohort = Activity - Cohort
Eg:
๐น 9. Cohort Retention Matrix
Conceptually, the final result may look like:
This is often called a cohort retention matrix.
It immediately shows whether newer customer cohorts are retaining better or worse.
๐น 10. Customer Lifetime Value (CLV)
Another important customer metric is Customer Lifetime Value (CLV/LTV).
A simplified version can be based on:
Total Revenue Generated by Customer
A more advanced business model may consider:
โข Revenue
โข Gross margin
โข Purchase frequency
โข Retention
โข Customer lifespan
โข Acquisition cost
๐น 11. Average Order Value (AOV)
A basic customer metric is:
Average Order Value = Total Sales รท Number of Orders
In SQL:
๐ง SQL Level 10 โ Cohort Analysis, Retention & Customer Analytics
Now we're moving from writing SQL queries to using SQL for real analytical problems.
Customer analytics is one of the most important areas because businesses want to know:
๐ฅ Who are our customers?
๐ When did they first purchase?
๐ Do they come back?
๐ When do they stop returning?
๐ฐ Which customers generate most revenue?
๐ How does behavior change over time?
One of the most powerful techniques for this is Cohort Analysis.
๐น 1. What Is Cohort Analysis?
A cohort is a group who share a common starting point.
โข Jan 2026 Cohort = first purchase in Jan 2026
โข Feb 2026 Cohort = first purchase in Feb 2026
Instead of mixing everyone, we track each group over time.
๐น 2. Why It Matters
Suppose total monthly customers are increasing.
That sounds positive.
But what if new customers are increasing while existing customers stop returning?
A simple monthly report may hide this problem.
Cohort analysis separates New vs Returning customers.
This makes retention problems much easier to identify.
๐น 3. Step 1 โ Find Each Customer's First Purchase
SELECT
Customer_ID,
MIN(Order_Date) AS First_Order_Date
FROM Orders
GROUP BY Customer_ID;
This gives us the first purchase date for every customer.
Customer | First Order
C101 | 2026-01-10
C102 | 2026-01-18
C103 | 2026-02-05
๐น 4. Step 2 โ Assign a Cohort Month
We can convert the first purchase into a month-level cohort.
First Purchase Date โ Cohort Month
C101 โ 2026-01
C103 โ 2026-02
The exact month-truncation syntax varies between SQL databases.
๐น 5. Step 3 โ Join Cohort Back to Orders
Now we need both:
Customer's cohort
and
Customer's subsequent activity
WITH Customer_Cohorts AS (
SELECT Customer_ID, MIN(Order_Date) AS First_Order_Date
FROM Orders GROUP BY Customer_ID
)
SELECT o.Customer_ID, c.First_Order_Date, o.Order_Date, o.Sales
FROM Orders o
JOIN Customer_Cohorts c ON o.Customer_ID = c.Customer_ID;
Now every transaction knows which cohort the customer belongs to.
๐น 6. Cohort Month vs Activity Month
โข Cohort Month: When first purchased
โข Activity Month: When purchase happened
Customer | Cohort | Activity
C101 | Jan | Jan
C101 | Jan | Feb
C101 | Jan | Mar
๐น 7. Measuring Retention
Retention measures how many customers from a cohort remain active in later periods.
Retention = Active in Period / Original Cohort * 100
โข Jan cohort: 100 customers
โข Feb: 60 active โ 60%
โข Mar: 40 active โ 40%
๐น 8. Retention Month
Months Since Cohort = Activity - Cohort
Eg:
Customer | Cohort | Activity | Months_Since_Cohort
C101 | Jan | Jan | 0
C101 | Jan | Feb | 1
C101 | Jan | Mar | 2
๐น 9. Cohort Retention Matrix
Conceptually, the final result may look like:
Cohort | Month0 | Month1 | Month2 | Month3
Jan | 100% | 60% | 40% | 30%
Feb | 100% | 65% | 45% | โ
Mar | 100% | 70% | โ | โ
This is often called a cohort retention matrix.
It immediately shows whether newer customer cohorts are retaining better or worse.
๐น 10. Customer Lifetime Value (CLV)
Another important customer metric is Customer Lifetime Value (CLV/LTV).
A simplified version can be based on:
Total Revenue Generated by Customer
A more advanced business model may consider:
โข Revenue
โข Gross margin
โข Purchase frequency
โข Retention
โข Customer lifespan
โข Acquisition cost
๐น 11. Average Order Value (AOV)
A basic customer metric is:
Average Order Value = Total Sales รท Number of Orders
In SQL:
โค6
SELECT Customer_ID, SUM(Sales) / COUNT(DISTINCT Order_ID) AS AOV
FROM Orders GROUP BY Customer_ID;
This tells us how much a customer spends per order on average.
๐น 12. Purchase Frequency
We can also calculate the number of orders per customer:
SELECT Customer_ID, COUNT(DISTINCT Order_ID) AS Number_of_Orders
FROM Orders GROUP BY Customer_ID;
Customers can then be segmented based on activity.
For example:
โข 1 order โ One-time customer
โข 2โ5 orders โ Repeat customer
โข 6+ orders โ Highly active customer
โ ๏ธ These thresholds are business rules, not universal definitions.
๐น 13. Recency
SELECT Customer_ID, MAX(Order_Date) AS Last_Order_Date
FROM Orders GROUP BY Customer_ID;
Then compare the last order date with a chosen analysis date.
A customer who purchased recently is generally more active than someone whose last purchase was a long time ago.
๐น 14. RFM Analysis
โข R โ Recency: How recently?
โข F โ Frequency: How often?
โข M โ Monetary: How much?
Example:
Customer | Recency | Frequency | Monetary
C101 | 5 days | 12 orders | โน85,000
C102 | 20 days | 6 orders | โน42,000
C103 | 120 days| 2 orders | โน8,000
This allows businesses to identify:
โญ High-value customers
๐ Loyal customers
โ ๏ธ Customers at risk
๐ค Inactive customers
๐น 15. Segmentation With CASE
You can convert analytical metrics into business segments.
For example:
SELECT Customer_ID, Total_Sales,
CASE
WHEN Total_Sales >= 50000 THEN 'High Value'
WHEN Total_Sales >= 20000 THEN 'Medium Value'
ELSE 'Low Value'
END AS Customer_Segment
FROM Customer_Sales;
This transforms numerical analysis into a business-friendly classification.
๐น 16. Repeat Customers
SELECT Customer_ID, COUNT(DISTINCT Order_ID) AS Order_Count
FROM Orders GROUP BY Customer_ID
HAVING COUNT(DISTINCT Order_ID) > 1;
This finds customers with more than one order.
๐น 17. First vs Repeat Purchase
You can use ROW_NUMBER() to identify purchase sequence.
WITH Customer_Orders AS (
SELECT Customer_ID, Order_ID, Order_Date,
ROW_NUMBER() OVER (PARTITION BY Customer_ID ORDER BY Order_Date) AS Purchase_Number
FROM Orders
)
SELECT * FROM Customer_Orders;
Now:
Purchase_Number = 1 means the customer's first purchase.Purchase_Number = 2 means the second purchase.And so on.
This opens the door to deeper customer behavior analysis.
๐น 18. Time Between Purchases
SELECT Customer_ID, Order_Date,
LAG(Order_Date) OVER (PARTITION BY Customer_ID ORDER BY Order_Date) AS Previous_Order_Date
FROM Orders;
Now you can calculate the number of days between purchases.
โ Helps answer "How frequently do customers return?"
๐น 19. Churn Analysis
Churn means customers stop using or purchasing from a business.
SQL can help identify customers whose activity has fallen below a defined threshold.
For example:
Last Purchase โ Days Since โ Business Threshold โ Active / At Risk / Inactive
SQL finds pattern, business defines churn.
๐ฏ Interview Challenge
Find customers with โฅ3 orders and >50,000 spent:
SELECT Customer_ID, COUNT(DISTINCT Order_ID) AS Order_Count, SUM(Sales) AS Total_Sales
FROM Orders
GROUP BY Customer_ID
HAVING COUNT(DISTINCT Order_ID) >= 3 AND SUM(Sales) > 50000;
๐ง Double Tap โค๏ธ For More
โค8
๐ง๐ผ๐ฝ ๐ฑ ๐๐ฅ๐๐ ๐๐ผ๐๐ฟ๐๐ฒ๐ ๐๐ผ ๐๐ถ๐ฐ๐ธ๐๐๐ฎ๐ฟ๐ ๐ฌ๐ผ๐๐ฟ ๐๐ฎ๐๐ฎ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ ๐๐ฎ๐ฟ๐ฒ๐ฒ๐ฟ ๐
Want to start a career in Data Science without spending money?
Here are 5 beginner-friendly learning resources covering essential skills such as Python, SQL, Machine Learning and hands-on projects.
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4ilAmok
๐ฏ Perfect for Students โข Freshers โข Beginners โข Aspiring Data Scientists
๐ก Learn โ Practice โ Build Projects โ Create Your Portfolio
Want to start a career in Data Science without spending money?
Here are 5 beginner-friendly learning resources covering essential skills such as Python, SQL, Machine Learning and hands-on projects.
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4ilAmok
๐ฏ Perfect for Students โข Freshers โข Beginners โข Aspiring Data Scientists
๐ก Learn โ Practice โ Build Projects โ Create Your Portfolio
๐ 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
๐น 4. Rank Customers by Revenue
๐น 5. Top 3 Customers in Each Region
๐น 6. Finding the Second-Highest Salary
๐น 7. Find Products That Never Sold
๐น 8. Customers With No Orders
๐น 9. Customers Above Average Spending
๐น 10. Month-over-Month Sales Growth
๐ง 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
๐น 11. Calculate Growth Percentage
๐น 12. Find the Latest Order for Every Customer
๐น 13-14. Inactive Customers & Duplicates
Inactive =
Business defines the rule, SQL calculates it.
Detect duplicates:
๐น 15. Combining Multiple Tables
โ ๏ธ Every additional join can change the number of rows. Always check the grain.
๐น 16. The Most Important Analytical Pattern
1. Filter raw data โ 2. Join tables โ 3. Aggregate to correct grain โ 4. Apply window functions โ 5. Filter analytical result โ 6. Present final output
๐ผ Real-World Business Problems to Practice
Sales: Top 5 products by revenue, Top products within each category, Month with highest sales, Revenue growth by month
Customers: Customers with no orders, declining purchases, most recent purchase, repeat customers, AOV per customer
Operations: Orders taking longer than expected, Products never sold, Duplicate transactions, Most active regions
๐ฏ SQL Interview Challenge: Find the highest-selling product in each category.
๐ง SQL Resources: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Double Tap โค๏ธ For More
(Total_Sales - Previous_Sales) / NULLIF(Previous_Sales, 0) * 100NULLIF() prevents division-by-zero errors.๐น 12. Find the Latest Order for Every Customer
WITH Ranked_Orders AS (
SELECT Customer_ID, Order_ID, Order_Date,
ROW_NUMBER() OVER (PARTITION BY Customer_ID ORDER BY Order_Date DESC) AS rn
FROM Orders
)
SELECT Customer_ID, Order_ID, Order_Date FROM Ranked_Orders WHERE rn = 1;
๐น 13-14. Inactive Customers & Duplicates
Inactive =
MAX(Order_Date) vs 90-day threshold.Business defines the rule, SQL calculates it.
Detect duplicates:
WITH Duplicate_Check AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Customer_ID, Order_Date, Sales ORDER BY Order_ID) AS rn
FROM Orders
)
SELECT * FROM Duplicate_Check WHERE rn > 1;
๐น 15. Combining Multiple Tables
SELECT c.Customer_ID, c.Customer_Name, p.Product_Name, oi.Quantity, oi.Sales
FROM Customers c
JOIN Orders o ON c.Customer_ID = o.Customer_ID
JOIN Order_Items oi ON o.Order_ID = oi.Order_ID
JOIN Products p ON oi.Product_ID = p.Product_ID;
โ ๏ธ Every additional join can change the number of rows. Always check the grain.
๐น 16. The Most Important Analytical Pattern
1. Filter raw data โ 2. Join tables โ 3. Aggregate to correct grain โ 4. Apply window functions โ 5. Filter analytical result โ 6. Present final output
๐ผ Real-World Business Problems to Practice
Sales: Top 5 products by revenue, Top products within each category, Month with highest sales, Revenue growth by month
Customers: Customers with no orders, declining purchases, most recent purchase, repeat customers, AOV per customer
Operations: Orders taking longer than expected, Products never sold, Duplicate transactions, Most active regions
๐ฏ SQL Interview Challenge: Find the highest-selling product in each category.
WITH Product_Sales AS (
SELECT Product_ID, Category, SUM(Sales) AS Total_Sales
FROM Product_Sales_Data GROUP BY Product_ID, Category
),
Ranked_Products AS (
SELECT *, RANK() OVER (PARTITION BY Category ORDER BY Total_Sales DESC) AS Sales_Rank
FROM Product_Sales
)
SELECT Product_ID, Category, Total_Sales FROM Ranked_Products WHERE Sales_Rank = 1;
๐ง SQL Resources: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Double Tap โค๏ธ For More
โค10
๐ ๐ง๐ผ๐ฝ ๐ฏ ๐๐ฅ๐๐ ๐ฅ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ๐ ๐๐ผ ๐๐ฒ๐ฎ๐ฟ๐ป ๐๐ป-๐๐ฒ๐บ๐ฎ๐ป๐ฑ ๐ง๐ฒ๐ฐ๐ต ๐ฆ๐ธ๐ถ๐น๐น๐ ๐ฅ
๐ซ Artificial Intelligence (AI)
๐ Data Analytics
๐ Cybersecurity
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4y2XyN1
๐ฏ Perfect for Students โข Freshers โข Beginners โข Tech Enthusiasts
๐ก Learn for FREE โ Build Skills โ Upgrade Your Career
๐ซ Artificial Intelligence (AI)
๐ Data Analytics
๐ Cybersecurity
๐ ๐๐ป๐ฟ๐ผ๐น๐น ๐ณ๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4y2XyN1
๐ฏ Perfect for Students โข Freshers โข Beginners โข Tech Enthusiasts
๐ก Learn for FREE โ Build Skills โ Upgrade Your Career
โค5
๐ Data Analyst Roadmap โ Part 22
๐ Power BI Level 1 โ Introduction to Power BI & Business Intelligence
After learning Excel and SQL, it's time to move into one of the most important tools in the modern Data Analyst toolkit:
Microsoft Power BI
Power BI helps you transform raw data into:
๐ Interactive dashboards
๐ Reports
๐ Business insights
๐ฏ KPIs
๐ Trends and comparisons
๐ผ Decision-making tools
The goal isn't simply to create attractive charts.
The goal is to turn data into information that people can use to make better decisions.
๐น 1. What Is Power BI?
Power BI is Microsoft's business intelligence and
data visualization platform.
It allows you to:
โข Connect to different data sources
โข Clean and transform data
โข Build data models
โข Create calculations
โข Create interactive visualizations
โข Build dashboards and reports
โข Share insights with others
A typical workflow looks like:
Data Sources
โ
Power Query
โ
Data Model
โ
DAX Calculations
โ
Visualizations
โ
Report / Dashboard
โ
Business Insights
๐น 2. Why Should a Data Analyst Learn Power BI?
Companies generate huge amounts of data.
But raw tables aren't easy for business users to understand.
Imagine giving management this:
Date | Region | Product | Sales | Profit
They may have thousands or millions of rows.
Instead, Power BI can turn that data into:
Total Sales: โน12.5 Cr
Profit: โน3.1 Cr
Top Region: West
Top Product: Product A
Monthly Trend: ๐
Sales by Region: Interactive chart
Now decision-makers can understand the situation quickly.
๐น 3. Power BI vs Excel
You already learned Excel in the earlier parts of this roadmap.
Both tools are valuable, but they are commonly used differently.
Excel| Power BI
Spreadsheet-based| BI platform
Great for ad-hoc analysis| Great for interactive reporting
Cell-based calculations| Model + DAX-based calculations
Manual dashboard updates can be common| Reports can refresh from data sources
Excellent for detailed individual analysis| Excellent for scalable business reporting
This doesn't mean:
Power BI replaces Excel.
Strong Data Analysts often use both.
๐น 4. Main Components of Power BI
You should become familiar with the Power BI ecosystem.
The major concepts you'll encounter are:
Power BI Desktop
Used to build reports, transform data, create models, and write DAX.
Power BI Service
Used for publishing, sharing, collaboration, refresh, and managing reports in the cloud.
Power BI Mobile
Allows users to view and interact with reports on mobile devices.
For a beginner, Power BI Desktop is where most hands-on learning starts.
๐น 5. Power BI Desktop Interface
When you open Power BI Desktop, you'll work with several important areas.
Report View
Used to create visualizations and report pages.
Data View
Allows you to inspect the data loaded into your model.
Model View
Shows relationships between tables.
These three views are important because Power BI isn't just a visualization tool.
It's also a data modeling and analytical environment.
๐น 6. Connecting Power BI to Data
Power BI can connect to many sources.
For example:
๐ Excel
๐ CSV
๐๏ธ SQL databases
โ๏ธ Cloud data sources
๐ Web sources
๐ Other business systems
A common beginner workflow is:
Excel/CSV โ Power BI โ Dashboard
๐ Power BI Level 1 โ Introduction to Power BI & Business Intelligence
After learning Excel and SQL, it's time to move into one of the most important tools in the modern Data Analyst toolkit:
Microsoft Power BI
Power BI helps you transform raw data into:
๐ Interactive dashboards
๐ Reports
๐ Business insights
๐ฏ KPIs
๐ Trends and comparisons
๐ผ Decision-making tools
The goal isn't simply to create attractive charts.
The goal is to turn data into information that people can use to make better decisions.
๐น 1. What Is Power BI?
Power BI is Microsoft's business intelligence and
data visualization platform.
It allows you to:
โข Connect to different data sources
โข Clean and transform data
โข Build data models
โข Create calculations
โข Create interactive visualizations
โข Build dashboards and reports
โข Share insights with others
A typical workflow looks like:
Data Sources
โ
Power Query
โ
Data Model
โ
DAX Calculations
โ
Visualizations
โ
Report / Dashboard
โ
Business Insights
๐น 2. Why Should a Data Analyst Learn Power BI?
Companies generate huge amounts of data.
But raw tables aren't easy for business users to understand.
Imagine giving management this:
Date | Region | Product | Sales | Profit
They may have thousands or millions of rows.
Instead, Power BI can turn that data into:
Total Sales: โน12.5 Cr
Profit: โน3.1 Cr
Top Region: West
Top Product: Product A
Monthly Trend: ๐
Sales by Region: Interactive chart
Now decision-makers can understand the situation quickly.
๐น 3. Power BI vs Excel
You already learned Excel in the earlier parts of this roadmap.
Both tools are valuable, but they are commonly used differently.
Excel| Power BI
Spreadsheet-based| BI platform
Great for ad-hoc analysis| Great for interactive reporting
Cell-based calculations| Model + DAX-based calculations
Manual dashboard updates can be common| Reports can refresh from data sources
Excellent for detailed individual analysis| Excellent for scalable business reporting
This doesn't mean:
Power BI replaces Excel.
Strong Data Analysts often use both.
๐น 4. Main Components of Power BI
You should become familiar with the Power BI ecosystem.
The major concepts you'll encounter are:
Power BI Desktop
Used to build reports, transform data, create models, and write DAX.
Power BI Service
Used for publishing, sharing, collaboration, refresh, and managing reports in the cloud.
Power BI Mobile
Allows users to view and interact with reports on mobile devices.
For a beginner, Power BI Desktop is where most hands-on learning starts.
๐น 5. Power BI Desktop Interface
When you open Power BI Desktop, you'll work with several important areas.
Report View
Used to create visualizations and report pages.
Data View
Allows you to inspect the data loaded into your model.
Model View
Shows relationships between tables.
These three views are important because Power BI isn't just a visualization tool.
It's also a data modeling and analytical environment.
๐น 6. Connecting Power BI to Data
Power BI can connect to many sources.
For example:
๐ Excel
๐ CSV
๐๏ธ SQL databases
โ๏ธ Cloud data sources
๐ Web sources
๐ Other business systems
A common beginner workflow is:
Excel/CSV โ Power BI โ Dashboard
โค3
Later, you'll learn how to connect Power BI directly to SQL databases and other enterprise sources.
๐น 7. Importing Data
A typical process is:
Home
โ
Get Data
โ
Choose Source
โ
Select Table/File
โ
Transform Data
โ
Load
Don't immediately start creating charts.
First understand:
What data did I load?
๐น 8. Power Query
Power Query is Power BI's data preparation and transformation engine.
You'll use it to:
โข Remove unwanted columns
โข Rename columns
โข Change data types
โข Remove duplicates
โข Handle missing values
โข Split columns
โข Merge tables
โข Append tables
โข Filter rows
โข Create transformation steps
This is similar to the Power Query work you learned in Excel.
The important idea is:
Power Query prepares the data before analysis.
๐น 9. Power Query vs DAX
This distinction is extremely important.
Power Query
โ Used mainly for data preparation and transformation
DAX
โ Used mainly for calculations and analysis inside the data model
Think:
Power Query
"Prepare the data."
DAX
"Analyze the data."
You'll learn both in detail in later parts.
๐น 10. Data Modeling
Suppose you have:
Sales
โข Order_ID
โข Customer_ID
โข Product_ID
โข Date
โข Sales
Customers
โข Customer_ID
โข Customer_Name
โข Region
Products
โข Product_ID
โข Product_Name
โข Category
Date
โข Date
โข Month
โข Quarter
โข Year
Instead of putting everything into one giant table, Power BI can connect these tables through relationships.
This is called data modeling.
๐น 11. Relationships
For example:
Customers
Customer_ID
โ
Sales
โ
Product_ID
Products
The relationship allows Power BI to understand how tables are connected.
For example:
Customer โ Sales
allows you to analyze sales by customer region.
Product โ Sales
allows you to analyze sales by product category.
๐น 12. Fact Tables and Dimension Tables
A common data-modeling structure is the star schema.
At the center:
โญ Fact Table
Around it:
๐น Dimension Tables
Example:
Customers
Products โ Sales โ Date
Region
The "Sales" table contains business events or measurements.
The dimension tables provide descriptive context.
This structure is extremely important for Power BI.
๐น 13. Measures vs Columns
Another fundamental concept.
Suppose you have:
"Sales"
A calculated column could calculate something for each row.
A measure calculates a value based on the current report context.
Example measure:
Total Sales =
SUM(Sales[Sales_Amount])
When you put this measure into a visual, Power BI calculates it according to the selected:
โข Region
โข Product
โข Date
โข Customer
โข Filters
This makes measures extremely powerful.
๐น 14. Your First Visualization
Suppose you have:
Month| Sales
Jan| 100,000
Feb| 120,000
Mar| 150,000
You could create a line chart.
The chart immediately communicates:
๐ Sales are increasing over time.
But visualization choice matters.
You shouldn't select a chart because it looks attractive.
Choose it because it communicates the business message clearly.
๐น 15. Common Power BI Visuals
You should become familiar with:
๐ Bar Chart
๐ Line Chart
๐ฅง Pie / Donut Chart
๐ข Card
๐ Table
๐ Matrix
๐ฏ KPI
๐บ๏ธ Map
๐ Column Chart
๐๏ธ Slicer
Each visual serves a different analytical purpose.
๐น 16. Cards
Cards are useful for displaying important KPIs.
๐น 7. Importing Data
A typical process is:
Home
โ
Get Data
โ
Choose Source
โ
Select Table/File
โ
Transform Data
โ
Load
Don't immediately start creating charts.
First understand:
What data did I load?
๐น 8. Power Query
Power Query is Power BI's data preparation and transformation engine.
You'll use it to:
โข Remove unwanted columns
โข Rename columns
โข Change data types
โข Remove duplicates
โข Handle missing values
โข Split columns
โข Merge tables
โข Append tables
โข Filter rows
โข Create transformation steps
This is similar to the Power Query work you learned in Excel.
The important idea is:
Power Query prepares the data before analysis.
๐น 9. Power Query vs DAX
This distinction is extremely important.
Power Query
โ Used mainly for data preparation and transformation
DAX
โ Used mainly for calculations and analysis inside the data model
Think:
Power Query
"Prepare the data."
DAX
"Analyze the data."
You'll learn both in detail in later parts.
๐น 10. Data Modeling
Suppose you have:
Sales
โข Order_ID
โข Customer_ID
โข Product_ID
โข Date
โข Sales
Customers
โข Customer_ID
โข Customer_Name
โข Region
Products
โข Product_ID
โข Product_Name
โข Category
Date
โข Date
โข Month
โข Quarter
โข Year
Instead of putting everything into one giant table, Power BI can connect these tables through relationships.
This is called data modeling.
๐น 11. Relationships
For example:
Customers
Customer_ID
โ
Sales
โ
Product_ID
Products
The relationship allows Power BI to understand how tables are connected.
For example:
Customer โ Sales
allows you to analyze sales by customer region.
Product โ Sales
allows you to analyze sales by product category.
๐น 12. Fact Tables and Dimension Tables
A common data-modeling structure is the star schema.
At the center:
โญ Fact Table
Around it:
๐น Dimension Tables
Example:
Customers
Products โ Sales โ Date
Region
The "Sales" table contains business events or measurements.
The dimension tables provide descriptive context.
This structure is extremely important for Power BI.
๐น 13. Measures vs Columns
Another fundamental concept.
Suppose you have:
"Sales"
A calculated column could calculate something for each row.
A measure calculates a value based on the current report context.
Example measure:
Total Sales =
SUM(Sales[Sales_Amount])
When you put this measure into a visual, Power BI calculates it according to the selected:
โข Region
โข Product
โข Date
โข Customer
โข Filters
This makes measures extremely powerful.
๐น 14. Your First Visualization
Suppose you have:
Month| Sales
Jan| 100,000
Feb| 120,000
Mar| 150,000
You could create a line chart.
The chart immediately communicates:
๐ Sales are increasing over time.
But visualization choice matters.
You shouldn't select a chart because it looks attractive.
Choose it because it communicates the business message clearly.
๐น 15. Common Power BI Visuals
You should become familiar with:
๐ Bar Chart
๐ Line Chart
๐ฅง Pie / Donut Chart
๐ข Card
๐ Table
๐ Matrix
๐ฏ KPI
๐บ๏ธ Map
๐ Column Chart
๐๏ธ Slicer
Each visual serves a different analytical purpose.
๐น 16. Cards
Cards are useful for displaying important KPIs.
For example:
โโโโโโโโโโโโโโโโโโโ
โ TOTAL SALES โ
โ โน12.5 Cr โ
โโโโโโโโโโโโโโโโโโโ
Other examples:
Total Profit
Total Customers
Total Orders
Average Order Value
A dashboard should make its most important KPIs easy to find.
๐น 17. Slicers
Slicers allow users to interactively filter a report.
For example:
Region:
[All โผ]
Year:
[2026 โผ]
Category:
[Electronics โผ]
Selecting a region can update multiple visuals on the report page.
This is one of the features that makes Power BI dashboards interactive.
๐น 18. Filters
Power BI provides filtering at different levels.
Common concepts include:
Visual-level filter
Affects one visual.
Page-level filter
Affects visuals on a particular page.
Report-level filter
Can affect the entire report.
Understanding filter behavior becomes extremely important when building complex reports.
๐น 19. Dashboard vs Report
These terms are often confused.
A report can contain multiple pages with interactive visuals.
A dashboard in the Power BI Service is a single-page canvas made from pinned tiles.
In everyday conversation, people sometimes use "dashboard" to mean any Power BI report page.
But technically, they're different concepts.
๐น 20. The Real Purpose of a Power BI Dashboard
A good dashboard should answer business questions.
For example:
Sales Dashboard
ยซHow much are we selling?ยป
ยซWhich regions are performing best?ยป
ยซWhich products drive revenue?ยป
ยซIs revenue increasing or decreasing?ยป
ยซWhere are we underperforming?ยป
The dashboard should make these answers easy to discover.
๐น 21. Common Beginner Mistakes
Avoid:
โ Adding too many visuals
โ Using every available chart type
โ Creating unnecessary colors and decorations
โ Building dashboards before understanding the data
โ Ignoring relationships
โ Creating everything as calculated columns
โ Using measures incorrectly
โ Showing numbers without business context
A professional dashboard should be:
Clear + Accurate + Interactive + Business-focused
๐ Double Tap โค๏ธ For More
โโโโโโโโโโโโโโโโโโโ
โ TOTAL SALES โ
โ โน12.5 Cr โ
โโโโโโโโโโโโโโโโโโโ
Other examples:
Total Profit
Total Customers
Total Orders
Average Order Value
A dashboard should make its most important KPIs easy to find.
๐น 17. Slicers
Slicers allow users to interactively filter a report.
For example:
Region:
[All โผ]
Year:
[2026 โผ]
Category:
[Electronics โผ]
Selecting a region can update multiple visuals on the report page.
This is one of the features that makes Power BI dashboards interactive.
๐น 18. Filters
Power BI provides filtering at different levels.
Common concepts include:
Visual-level filter
Affects one visual.
Page-level filter
Affects visuals on a particular page.
Report-level filter
Can affect the entire report.
Understanding filter behavior becomes extremely important when building complex reports.
๐น 19. Dashboard vs Report
These terms are often confused.
A report can contain multiple pages with interactive visuals.
A dashboard in the Power BI Service is a single-page canvas made from pinned tiles.
In everyday conversation, people sometimes use "dashboard" to mean any Power BI report page.
But technically, they're different concepts.
๐น 20. The Real Purpose of a Power BI Dashboard
A good dashboard should answer business questions.
For example:
Sales Dashboard
ยซHow much are we selling?ยป
ยซWhich regions are performing best?ยป
ยซWhich products drive revenue?ยป
ยซIs revenue increasing or decreasing?ยป
ยซWhere are we underperforming?ยป
The dashboard should make these answers easy to discover.
๐น 21. Common Beginner Mistakes
Avoid:
โ Adding too many visuals
โ Using every available chart type
โ Creating unnecessary colors and decorations
โ Building dashboards before understanding the data
โ Ignoring relationships
โ Creating everything as calculated columns
โ Using measures incorrectly
โ Showing numbers without business context
A professional dashboard should be:
Clear + Accurate + Interactive + Business-focused
๐ Double Tap โค๏ธ For More
โค5
๐ ๐๐ฎ๐๐ฎ ๐๐ป๐ฎ๐น๐๐๐ถ๐ฐ๐ ๐๐ฒ๐ฟ๐๐ถ๐ณ๐ถ๐ฐ๐ฎ๐๐ถ๐ผ๐ป ๐๐ผ๐๐ฟ๐๐ฒ ๐๐ผ ๐๐ฒ๐ ๐ฎ ๐๐ถ๐ด๐ต-๐ฃ๐ฎ๐๐ถ๐ป๐ด ๐๐ผ๐ฏ ๐ถ๐ป ๐ฎ๐ฌ๐ฎ๐ฒ ๐
Build job-ready skills through live online classes, practical assignments and real-world projects.
๐ผ End-to-End Placement Support
๐ค 500+ Partner Companies
๐ 2000+ Students Placed
๐ Highest Salary: โน41 LPA
๐ Get FREE career counselling and check your eligibility!
๐ ๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐ก๐ผ๐ ๐
https://pdlink.in/45vk5ph
โกPrepare for roles such as Data Analyst, Business Analyst, BI Analyst and Reporting Analyst.
Build job-ready skills through live online classes, practical assignments and real-world projects.
๐ผ End-to-End Placement Support
๐ค 500+ Partner Companies
๐ 2000+ Students Placed
๐ Highest Salary: โน41 LPA
๐ Get FREE career counselling and check your eligibility!
๐ ๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐ก๐ผ๐ ๐
https://pdlink.in/45vk5ph
โกPrepare for roles such as Data Analyst, Business Analyst, BI Analyst and Reporting Analyst.
โค4
๐ Data Analyst Roadmap โ Part 23
๐ Power BI Level 2 โ Power Query: Data Cleaning & Transformation
Power Query is used in Power BI to clean, transform, and prepare data before building reports.
๐น 1. Open Power Query
In Power BI Desktop:
Home โ Transform Data
This opens the Power Query Editor.
You will mainly work with:
โข Queries
โข Data Preview
โข Applied Steps
๐น 2. Change Data Types
Always check whether columns have the correct data type.
For example:
Customer_ID โ Text
Quantity โ Whole Number
Sales โ Decimal Number
Order_Date โ Date
Incorrect data types can cause problems in calculations and visuals.
๐น 3. Remove Unnecessary Columns
If your dataset contains columns you don't need, remove them.
For example:
Customer_ID
Customer_Name
Email
Phone
Sales
Internal_Code
If your analysis only needs Customer ID, Customer Name, and Sales, remove the rest.
๐น 4. Filter Unnecessary Rows
Power Query can remove or filter:
โข Blank rows
โข Invalid records
โข Test data
โข Unwanted categories
โข Records outside the required period
Always understand the business rule before removing data.
๐น 5. Remove Duplicates
Power Query allows you to remove duplicate values based on selected columns.
For example, if "Customer_ID" should be unique in a Customer table, duplicate IDs should be investigated.
But don't remove duplicates blindly.
A Sales table can naturally contain many rows for the same customer.
๐น 6. Handle Missing Values
You may find:
Blank
NULL
N/A
Unknown
Depending on the situation, you can:
โข Keep the value blank
โข Replace it
โข Remove the record
Don't automatically replace blanks with zero.
For example, a blank discount doesn't always mean a discount of 0.
๐น 7. Clean Text
Data often contains unwanted spaces or inconsistent formatting.
Example:
" Mumbai"
"Mumbai "
"MUMBAI"
Useful Power Query transformations include:
Trim โ Removes unnecessary spaces
Clean โ Removes unwanted non-printable characters
You can also change text to:
โข UPPERCASE
โข lowercase
โข Proper Case
๐น 8. Replace Values
Suppose your data contains:
Mum
Mumbai
MUMBAI
You can replace and standardize values so they are represented consistently.
This is especially useful for:
โข City
โข Region
โข Category
โข Department
โข Status
๐น 9. Split Columns
Suppose you have:
Full Name
John Smith
Sarah Johnson
You can split it into:
First Name | Last Name
John | Smith
Sarah | Johnson
You can split a column using delimiters such as:
โข Space
โข Comma
โข Dash
โข Custom delimiter
๐น 10. Extract Text
You can extract specific parts of a text column.
For example:
john@gmail.com
You could extract:
john
or:
gmail.com
Power Query provides options such as:
โข Text Before Delimiter
โข Text After Delimiter
โข Text Between Delimiters
โข First Characters
โข Last Characters
๐น 11. Conditional Column
You can create categories based on conditions.
For example:
Sales >= 50,000 โ High
Sales >= 20,000 โ Medium
Otherwise โ Low
This is similar to "CASE WHEN" in SQL.
๐น 12. Custom Column
Power Query also allows you to create calculated columns.
For example:
Total Amount = Quantity ร Unit Price
Custom columns use Power Query's formula language, called M.
๐ Power BI Level 2 โ Power Query: Data Cleaning & Transformation
Power Query is used in Power BI to clean, transform, and prepare data before building reports.
๐น 1. Open Power Query
In Power BI Desktop:
Home โ Transform Data
This opens the Power Query Editor.
You will mainly work with:
โข Queries
โข Data Preview
โข Applied Steps
๐น 2. Change Data Types
Always check whether columns have the correct data type.
For example:
Customer_ID โ Text
Quantity โ Whole Number
Sales โ Decimal Number
Order_Date โ Date
Incorrect data types can cause problems in calculations and visuals.
๐น 3. Remove Unnecessary Columns
If your dataset contains columns you don't need, remove them.
For example:
Customer_ID
Customer_Name
Phone
Sales
Internal_Code
If your analysis only needs Customer ID, Customer Name, and Sales, remove the rest.
๐น 4. Filter Unnecessary Rows
Power Query can remove or filter:
โข Blank rows
โข Invalid records
โข Test data
โข Unwanted categories
โข Records outside the required period
Always understand the business rule before removing data.
๐น 5. Remove Duplicates
Power Query allows you to remove duplicate values based on selected columns.
For example, if "Customer_ID" should be unique in a Customer table, duplicate IDs should be investigated.
But don't remove duplicates blindly.
A Sales table can naturally contain many rows for the same customer.
๐น 6. Handle Missing Values
You may find:
Blank
NULL
N/A
Unknown
Depending on the situation, you can:
โข Keep the value blank
โข Replace it
โข Remove the record
Don't automatically replace blanks with zero.
For example, a blank discount doesn't always mean a discount of 0.
๐น 7. Clean Text
Data often contains unwanted spaces or inconsistent formatting.
Example:
" Mumbai"
"Mumbai "
"MUMBAI"
Useful Power Query transformations include:
Trim โ Removes unnecessary spaces
Clean โ Removes unwanted non-printable characters
You can also change text to:
โข UPPERCASE
โข lowercase
โข Proper Case
๐น 8. Replace Values
Suppose your data contains:
Mum
Mumbai
MUMBAI
You can replace and standardize values so they are represented consistently.
This is especially useful for:
โข City
โข Region
โข Category
โข Department
โข Status
๐น 9. Split Columns
Suppose you have:
Full Name
John Smith
Sarah Johnson
You can split it into:
First Name | Last Name
John | Smith
Sarah | Johnson
You can split a column using delimiters such as:
โข Space
โข Comma
โข Dash
โข Custom delimiter
๐น 10. Extract Text
You can extract specific parts of a text column.
For example:
john@gmail.com
You could extract:
john
or:
gmail.com
Power Query provides options such as:
โข Text Before Delimiter
โข Text After Delimiter
โข Text Between Delimiters
โข First Characters
โข Last Characters
๐น 11. Conditional Column
You can create categories based on conditions.
For example:
Sales >= 50,000 โ High
Sales >= 20,000 โ Medium
Otherwise โ Low
This is similar to "CASE WHEN" in SQL.
๐น 12. Custom Column
Power Query also allows you to create calculated columns.
For example:
Total Amount = Quantity ร Unit Price
Custom columns use Power Query's formula language, called M.
โค4