TGViewer
Data Analyst Interview Resources Data Analyst Interview Resources @dataanalystinterview ยท 52.6K subscribers
Post #2320 1.78K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find employees who earn the same salary as at least one other employee in the same department.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช
SELECT
employee_id,
employee_name,
department,
salary
FROM employees
WHERE (department, salary) IN (
SELECT
department,
salary
FROM employees
GROUP BY department, salary
HAVING COUNT(*) > 1
)
ORDER BY department, salary DESC;

๐Ÿ’ก Explanation:
The query identifies duplicate salary values within each department.

โ€ข The subquery groups records by department and salary.
โ€ข **HAVING COUNT(*) > 1** finds salary values that appear more than once in the same department.
โ€ข The outer query returns all employees whose (department, salary) matches those duplicate combinations.

This question tests your understanding of:
โœ… GROUP BY
โœ… HAVING
โœ… Multi-column filtering
โœ… Identifying duplicate records

๐ŸŽฏ Expected Output Example
Employee Department Salary
John IT 80,000
Alice IT 80,000
David HR 65,000
Sarah HR 65,000

๐Ÿš€ Alternative Using Window Functions
SELECT
employee_id,
employee_name,
department,
salary
FROM (
SELECT
*,
COUNT(*) OVER (
PARTITION BY department, salary
) AS salary_count
FROM employees
) t
WHERE salary_count > 1;

This approach avoids a subquery with GROUP BY and is a great way to showcase your knowledge of window functions.

๐Ÿš€ When interview questions ask you to find duplicates, think of these three approaches:

1. GROUP BY + HAVING
2. Window functions COUNT() OVER
3. Self Join for specific comparison scenarios

Knowing multiple solutions demonstrates strong SQL problem-solving skills.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
  • โค 6
More from @dataanalystinterview
  1. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  2. Oct 1, 2026๐Ÿ”ฅ Top 10 Theoretical Interview Questions Every Data Analyst Must Prepare ๐Ÿ“Š Data Analystโ€ฆ
  3. Sep 29, 2026๐Ÿš€ Excel Formulas Fundamentals โ€” Part 10 ๐Ÿ“Š Conditional Functions (SUMIF, SUMIFS, COUNTIF,โ€ฆ
  4. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  5. Sep 29, 2026๐Ÿ“Š Tableau Learning Roadmap โ€” Part 2 Connecting to Data Before creating visualizations inโ€ฆ
  6. Sep 28, 2026This is useful when you want to guide someone through an analytical narrative. The Tableauโ€ฆ
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 โ†’