TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2911 5.08K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.
Find all employees who have the same manager.

Assume the table structure:
employees(employee_id, employee_name, manager_id)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
e.employee_id,
e.employee_name,
e.manager_id,
m.employee_name AS manager_name
FROM employees e
JOIN employees m
ON e.manager_id = m.employee_id
WHERE e.manager_id IN (
SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING COUNT(*) > 1
)
ORDER BY e.manager_id, e.employee_name;

๐Ÿ’ก Explanation:
This query identifies managers who supervise more than one employee and returns all employees reporting to those managers.

โœ… The subquery groups records by manager_id.

โœ… HAVING COUNT(*) > 1 finds managers with multiple direct reports.

โœ… A self join retrieves the manager's name.

โœ… The outer query returns every employee reporting to those managers.

This question tests your understanding of:
โœ… Self Joins
โœ… GROUP BY and HAVING
โœ… Subqueries
โœ… Organizational Hierarchies

๐ŸŽฏ Expected Output Example
Employee Manager
John David
Alice David
Sarah Michael
Bob Michael

๐Ÿš€ Alternative Using Window Functions
SELECT
employee_id,
employee_name,
manager_id
FROM (
SELECT
*,
COUNT(*) OVER (
PARTITION BY manager_id
) AS team_size
FROM employees
WHERE manager_id IS NOT NULL
) t
WHERE team_size > 1;

This approach uses a window function to count the number of employees under each manager without using GROUP BY.

๐Ÿš€ Tip for SQL Job Seekers:
Hierarchy-based questions are very common in interviews. Practice problems involving:
Employees and managers
Parent-child relationships
Organizational charts
Category trees
Recursive queries WITH RECURSIVE or recursive CTEs

โค๏ธ React with โค๏ธ for more SQL interview challenges!
  • โค 15
More from @sqlspecialist
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  3. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
  4. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 5 Guys, let's continue our Data Analyst Interviewโ€ฆ
  5. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  6. Oct 7, 2026๐Ÿ”Ÿ What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns frโ€ฆ
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 โ†’