Hi everyone! I’ve been working on a database that stores information about employees. My table includes columns that record each employee’s full name and date of birth. Below is a sample of my table:
emp_name | dob
-----------------
Laura | 1985-03-10
Mark | 1990-06-15
Anna | 1990-06-15
Kevin | 1982-12-05
I am looking for a SQL query that will return only those records where two or more employees share the same birthday. I’ve tried a few ideas but haven’t managed to get it working. Any help or suggestions would be very appreciated. Thanks a lot for your time!
interesting challenge! u tried a self-join on the table? group by dob and having count(*)>1 finds shared birthdays. wonder if u considered a subquery? what do u think is the best method?
To identify employees with identical birth dates, you can utilize a combination of GROUP BY and HAVING clauses. Here’s an efficient query that should solve your problem:
SELECT dob, COUNT() as employee_count
FROM employee_table
GROUP BY dob
HAVING COUNT() > 1
ORDER BY dob
This query groups the records by date of birth, counts the occurrences, and returns only those dates with more than one employee. The results are sorted by date for easier reading. You can modify it further to include employee names if needed.
Remember to replace ‘employee_table’ with your actual table name. This approach is scalable and will work well even with large datasets.
hey owen, u can try this:
SELECT dob, GROUP_CONCAT(emp_name) as employees
FROM your_table
GROUP BY dob
HAVING COUNT(*) > 1
this’ll show u the shared bdays and who’s got em. lmk if it works for ya!