Find top SQL resources from global universities, cool projects, and learning materials for data analytics.
Admin: @coderfun
Useful links: heylink.me/DataAnalytics
Promotions: @love_data
Post #2831
1.98K
SQL Interview Series โ Part 2
๐ Question 2: Find Duplicate Records
Suppose you have an Employee table:
Employee
โข employee_id
โข employee_name
โข department
โข email
โ Find all email addresses that appear more than once in the Employee table.
Example:
Expected result:
๐ก Approach
We need to:
1๏ธโฃ Group records by email.
2๏ธโฃ Count how many times each email appears.
3๏ธโฃ Keep only the emails whose count is greater than 1.
๐ SQL Solution
๐ Why use HAVING instead of WHERE?
"WHERE" filters individual rows before grouping.
"HAVING" filters groups after "GROUP BY".
Since we want to filter based on "COUNT(*)", we use "HAVING".
This means:
"Group employees by email and return only those groups containing more than one record."
๐ฏ Double Tap โค๏ธ For Part-3
๐ Question 2: Find Duplicate Records
Suppose you have an Employee table:
Employee
โข employee_id
โข employee_name
โข department
โข email
โ Find all email addresses that appear more than once in the Employee table.
Example:
employee_id | employee_name | email
------------|---------------|-------------------
1 | Amit | amit@gmail.com
2 | Rahul | rahul@gmail.com
3 | Priya | amit@gmail.com
4 | Neha | neha@gmail.com
5 | Raj | rahul@gmail.com
Expected result:
amit@gmail.com
rahul@gmail.com
๐ก Approach
We need to:
1๏ธโฃ Group records by email.
2๏ธโฃ Count how many times each email appears.
3๏ธโฃ Keep only the emails whose count is greater than 1.
๐ SQL Solution
SELECT email, COUNT(*) AS occurrence_count
FROM Employee
GROUP BY email
HAVING COUNT(*) > 1;
๐ Why use HAVING instead of WHERE?
"WHERE" filters individual rows before grouping.
"HAVING" filters groups after "GROUP BY".
Since we want to filter based on "COUNT(*)", we use "HAVING".
GROUP BY email
HAVING COUNT(*) > 1
This means:
"Group employees by email and return only those groups containing more than one record."
๐ฏ Double Tap โค๏ธ For Part-3
- โค 13
- ๐ 1




