TGViewer
Data Analytics Data Analytics @sqlspecialist · 111K subscribers
Post #3075 3.04K
This asks:



Which employees earn more than the average salary of their own department?



The inner query depends on the current employee's department. This is more advanced than a basic subquery.

2️⃣4️⃣ Why Correlated Subqueries Matter

Suppose: IT Average salary = ₹80,000, HR Average salary = ₹60,000

An employee earning ₹75,000: Could be below IT average, Could be above HR average

So comparing everyone to the overall company average isn't enough. A correlated subquery allows you to compare each employee to the relevant group.

2️⃣5️⃣ Subquery vs JOIN

Sometimes the same problem can be solved using either a subquery or a JOIN.

For example, finding customers with orders can be done with: WHERE EXISTS (...) or: JOIN Orders ...

Neither is universally better.

The right choice depends on: What result you need, Whether duplicates matter, Query readability, Database optimizer, Data structure

Focus on understanding the logic rather than memorizing one preferred method.

2️⃣6️⃣ A Very Common Interview Problem

Question:



Find employees earning more than their department's average salary.



A correlated subquery solution:

SELECT
e.Name,
e.Department,
e.Salary
FROM Employees e
WHERE e.Salary > (
SELECT AVG(e2.Salary)
FROM Employees e2
WHERE e2.Department = e.Department
);


This is an excellent interview question because it tests: Subqueries, Aggregation, Correlation, Business logic

2️⃣7️⃣ Another Interview Problem

Question:



Find customers whose total sales are greater than ₹1,00,000.



Using a derived table:

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


Or using a CTE:

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 > 100000;


Both approaches produce the same analytical idea.

🧪 Practical Interview Challenge

Suppose you have Employees table

Q1. Find employees earning above the overall average.

SELECT
Name,
Salary
FROM Employees
WHERE Salary > (
SELECT AVG(Salary)
FROM Employees
);


Q2. Find employees earning above their department average.

SELECT
e.Name,
e.Department,
e.Salary
FROM Employees e
WHERE e.Salary > (
SELECT AVG(e2.Salary)
FROM Employees e2
WHERE e2.Department = e.Department
);


Q3. Create a CTE containing average salary by department.

WITH Department_Salary AS (
SELECT
Department,
AVG(Salary) AS Average_Salary
FROM Employees
GROUP BY Department
)
SELECT *
FROM Department_Salary;


Q4. Find departments whose average salary exceeds ₹70,000.

WITH Department_Salary AS (
SELECT
Department,
AVG(Salary) AS Average_Salary
FROM Employees
GROUP BY Department
)

SELECT
Department,
Average_Salary
FROM Department_Salary
WHERE Average_Salary > 70000;


Q5. Find customers who have placed at least one order.

SELECT
c.Customer_ID,
c.Customer_Name
FROM Customers c
WHERE EXISTS (
SELECT 1
FROM Orders o
WHERE o.Customer_ID = c.Customer_ID
);


🏆 Double Tap ❤️ For More
  • ❤ 8
More from @sqlspecialist
  1. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  2. Oct 7, 2026🔟 What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns fr…
  3. Oct 7, 2026📊 Data Analyst Interview Series — Part 4 Guys, let's continue our Data Analyst Interview…
  4. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  5. Oct 4, 20269️⃣ How would you calculate month-over-month growth? Sample Answer: “I would first retriev…
  6. Oct 4, 2026📊 Data Analyst Interview Series — Part 3 Guys, let's continue our Data Analyst Interview…
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 →