# @aartii.py on Instagram

- **Type:** Video
- **Original URL:** https://www.instagram.com/p/DSnQ-kcDfNT
- **Gondola URL:** https://gondola.cc/posts/59916550-aartiipy-instagram
- **Thumbnail:** https://img.gondola.cc/tr:w-,h-,fo-auto/postThumbnails/daa58408a0.jpg
- **Posted:** 2025-12-23T16:59:58.000+00:00
- **Account Owner:** Aarti Sinha (@aartii.py) — https://gondola.cc/aartii.py

## Caption

Day 5/7 - SQL Challenge 🎯
TODAY’S QUESTION:
Find all employees who earn more than their manager.
TABLE:
Employees (id, name, salary, manager_id)
SAMPLE DATA:
id | name | salary | manager_id
—|———|———|————
1 | Alice | 90000 | NULL (CEO)
2 | Bob | 80000 | 1
3 | Charlie | 95000 | 1 ← Earns more than Alice!
4 | Diana | 70000 | 2
THE SOLUTION:
SELECT e.name as employee_name
FROM Employees e
JOIN Employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;
BREAKDOWN:
Employees e - Think of this as the “employee table”
Employees m - Think of this as the “manager table”
e.manager_id = m.id - Connect employee to their manager
e.salary > m.salary - Find where employee earns more
VISUAL:
Employee Table (e)  Manager Table (m)
name | manager_id  id | name | salary
———|———— —|-——|-——
Bob | 1 →  1 | Alice | 90000
Charlie | 1 →  1 | Alice | 90000 ✓ (95k > 90k)
Diana | 2 →  2 | Bob | 80000
WHY SELF JOINS ARE HARD:
Same table appears twice. You need to:
Alias differently (e vs m)
Think of them as separate tables
Define the relationship clearly
COMMON MISTAKES:
❌ Not using aliases (confusing which is which)
❌ Wrong join condition
❌ Forgetting employees can BE managers too
ALTERNATIVE (with subquery):
SELECT e.name
FROM Employees e
WHERE e.salary > (
 SELECT m.salary
  FROM Employees m
  WHERE m.id = e.manager_id
);
WHY FAANG LOVES THIS:
Tests understanding of:
Self-referential data structures
Complex join logic
Hierarchical relationships
INTERVIEW TIP:
When you see “manager_id” or “parent_id” in a table, expect SELF JOIN questions.
Day 5/7 complete ✅
Self joins = Mind bender but powerful
#SQLChallenge #Day5 #SelfJoin #DataScience #SQL FAANG HardSQL

## Stats

- **Views:** 3,468
- **Likes:** 208
- **Shares:** 0
- **Comments:** 20

## Tags

sqlchallenge, selfjoin, day5, sql, datascience

---
Copyright (c) Gondola