We use INNER JOIN so employees without managers are excluded automatically.
This self-join pattern is very common in SQL interviews.
Full explanation:
https://t.co/BnscLEIjwI
#SQL#DataAnalytics#SQLInterviewQuestions#TheCodingCo
SQL Interview Question #10 🧵
Find employees who earn more than their manager.
Sounds simple… but many candidates freeze.
The tricky part is not the comparison.
It’s understanding the relationship.
Employees and managers are stored in the SAME table.
manager_id → employee_id
So we need two roles:
Employee
Manager
From the same table.
Solution:
Self join Employees table
e.manager_id = m.employee_id
Then compare:
e.salary > m.salary
This gives employees earning more than their manager.
Important:
RANK() → multiple rows if tie
ROW_NUMBER() → single row
This “aggregate + rank” pattern appears in many SQL interviews.
Full explanation:
https://t.co/P0GG17yHkK
#SQL#DataAnalytics#TheCodingCo#SQLInterviewQuestions
SQL Interview Question #9 🧵
Find the top-selling product each month.
Sounds like GROUP BY…
but it’s more than that.
Many candidates calculate total sales correctly…
but get stuck finding the top product per month.
SQL Interview Question #8 🧵
Find days where temperature was higher than the previous day.
Sounds simple… but many candidates struggle.
The tricky part is not the comparison.
It’s accessing the previous row correctly.
If calculation is per customer:
Add PARTITION BY customer_id
This pattern is very common in time-based SQL problems.
Full explanation:
https://t.co/hgZWtrHVh3
#SQL#DataAnalytics#TheCodingCo#SQLInterviewQuestions
SQL Interview Question #7 🧵
Show current amount + previous amount in the same row.
Sounds simple… but many people overcomplicate it.
Most candidates think about self joins.
But self joins can get messy quickly.
The real idea:
Access the previous row directly.
Use LAG()
LAG lets you look at a value from the previous row without writing a join.
Example:
amount + LAG(amount) OVER (ORDER BY transaction_date)
First row returns NULL (no previous row).
If needed, use ISNULL or COALESCE.
SQL Interview Question #6 🧵
Find customers who never placed an order.
Sounds simple… but many candidates still get it wrong.
Most people immediately write:
INNER JOIN Customers and Orders.
LAG and LEAD
Used to compare rows with other rows nearby.
LAG → previous row
LEAD → next row
These five functions appear in many SQL interview questions.
Full explanation here:
https://t.co/Zc01JFftvV
#SQL#DataAnalytics#TheCodingCo#SQLInterviewQuestions
SQL Window Functions every Data Analyst should know 🧵
ROW_NUMBER
RANK
DENSE_RANK
LAG
LEAD
These functions show up in many SQL interviews.
Example question:
“Find the second highest salary in each department.”