Birthdate Query

There is a table friends having following details

Name Birthday
John 28-jun-1985
Peter 27-aug-1987
Tom 13-aug-1986
Nick 29-apr-1987
Natalia 25-apr-1984
Kate 28-jun-1984
Wilson 11-nov-1987
krish 31-jul-1987
robert 25-apr-1984



SELECT names of the friends which come on the same day.. condition you don't know which birthday's repeat
SELECT distinct birthday and the names of the friends on those birthdays


Questions by sqllover

Showing Answers 1 - 31 of 31 Answers

puritm

  • Oct 5th, 2010
 

  1  SELECT * FROM dob
  2  WHERE TO_CHAR(dob,'DD') IN
  3  (
  4  SELECT TO_CHAR(dob,'DD') --,COUNT(name)
  5  FROM dob
  6  GROUP BY TO_CHAR(dob,'DD')
  7  HAVING COUNT(TO_CHAR(dob,'DD')) > 1
  8* )
SQL> /

NAME       DOB
---------- -----------
John       28-JUN-1985
Natalia    25-APR-1984
Kate       28-JUN-1984
robert     25-APR-1984

  Was this answer useful?  Yes

This can be resolved as we have only two rows that are having the same birth date. But dont know how to do it in SQL when we have more number of people with same birth date.

select a.name||','||b.name  from birthdate a,birthdate b
where a.name < b.name and to_char(a.birthday,'DD/MM') = to_char(b.birthday,'DD/MM');

  Was this answer useful?  Yes

puritm

  • Nov 16th, 2010
 

This query will retrieve the correct record if more pepole having same birth date.

SELECT * FROM dob
WHERE TO_CHAR(dob,'DDMM') IN
(
SELECT TO_CHAR(dob,'DDMM') -- COUNT(name)
FROM dob
GROUP BY TO_CHAR(dob,'DDMM')
HAVING COUNT(TO_CHAR(dob,'DDMM')) > 1
);

NAME       DOB
---------- -----------
John       28-JUN-1985
Natalia    25-APR-1984
Kate       28-JUN-1984
robert     25-APR-1984
Sam        25-APR-1995
Jon        25-APR-1989
Man        25-APR-2000

The following query retuns the names of all people having birthday(including the year) on the same date. If only day and month need to be considered, we need to use to_char(birthdate,'DD-MM') instead of birthdate

select * from bday_tab where (birthdate) in (select birthdate from bday_tab group by birthdate having count(birthdate) > 1) order by birthdate

Result:

William07-JUN-94
Susan07-JUN-94
Hermann07-JUN-94
Shelley07-JUN-94
Alberto10-MAR-97
Lindsey10-MAR-97
Laura20-AUG-97
Peter20-AUG-97
Winston24-JAN-98
Tayler24-JAN-98
  
  
  
  
  
  

  Was this answer useful?  Yes

SELECT * FROM employees e
 WHERE convert(varchar,BirthDate, 103) IN
 (
 SELECT convert(varchar,ee.BirthDate, 103)
 FROM employees ee
 GROUP BY convert(varchar,ee.BirthDate, 103)
 HAVING COUNT(convert(varchar,ee.BirthDate, 103)) > 1)

  Was this answer useful?  Yes

binita007

  • Dec 17th, 2010
 

CREATE table birthday(name varchar(20),DOA Date);
insert into birthday values('John',to_date('28-06-1985','dd-mm-yyyy'));
insert into birthday values('Peter',to_date('27-08-1987','dd-mm-yyyy'));
insert into birthday values('Tom',to_date('13-08-1986','dd-mm-yyyy'));
insert into birthday values('Nick',to_date('29-04-1987','dd-mm-yyyy'));
 insert into birthday values('Natalia',to_date('25-04-1984','dd-mm-yyyy'));
insert into birthday values('Kate',to_date('28-06-1984','dd-mm-yyyy'));
insert into birthday values('Wilson',to_date('11-11-1987','dd-mm-yyyy'));
 insert into birthday values('Krish',to_date('31-07-1987','dd-mm-yyyy'));
insert into birthday values('Robert',to_date('25-04-1984','dd-mm-yyyy'));

SELECT*
FROM birthday
where TO_CHAR(doa,'DDMM')IN(SELECT TO_CHAR(doa,'DDMM')--COUNT(name)
from birthday
GROUP BY TO_CHAR(doa,'DDMM')
HAVING COUNT(TO_CHAR(doa,'DDMM'))>1);

Result :

NAME                 DOA
----------              -------------
Natalia              25-APR-84
Robert               25-APR-84
John                  28-JUN-85
Kate                   28-JUN-84

  Was this answer useful?  Yes

Try this:

SELECT to_char (dob, 'DD-MON-YY') date_of_birth,  XMLAGG( XMLELEMENT  (E, Name || ',')).EXTRACT ('//text()').GETSTRINGVAL() names
FROM bday GROUP BY TO_CHAR (dob, 'DD-MON-YY')

Tested in oracle 10g.

  Was this answer useful?  Yes

Select Name from Tab_NB where To_Char(Birthday,'DD') in (select distinct(to_char(Birthday,'DD')) from Tab_NB);


Using Subquery, we are getting distinct birthdates from table, that dates are compared to where condition dates, is there any matching dates that are displayed.


Balakrishna. D

  Was this answer useful?  Yes

Prabakaran

  • Jul 27th, 2011
 

select name from btab where bday in (select distinct bday from btab)

  Was this answer useful?  Yes

Jonno

  • Feb 1st, 2018
 

Code
  1. --20180201 JL

  2. SELECT a.name, a.DOB FROM birthday a

  3.         INNER JOIN birthday b ON DAY(a.DOB) = DAY(b.DOB) AND MONTH(a.DOB) = MONTH(b.DOB)

  4. GROUP BY a.name, a.DOB

  5. HAVING COUNT(1) > 1

  6. ORDER BY a.DOB

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions