1

So I have some results where the query is like

SELECT * FROM employees ORDER BY birth_date ASC;  

This simply puts the birth date in ascending order. I want to only display the elder most employees. How can I edit this query to display that

Note : More than 1 employee was born on the same day, therefore i have 6 users who are the eldest but how can I only display the eldest

Imran Ali
  • 2,223
  • 2
  • 28
  • 41
user1860036
  • 31
  • 1
  • 7
  • 1
    add LIMIT - SELECT * FROM employees ORDER BY birth_date ASC LIMIT 1; – Bernd Buffen May 19 '16 at 22:11
  • A caveat to the comment by @BerndBuffen. That will work, but with multiple eldest employees, which one gets returned each time is not guaranteed consistent. – Uueerdo May 19 '16 at 22:28
  • @Uueerdo to garantthis you easy add the name at the end of the order by : **ORDER BY birth_date,Name_field ASC LIMIT 1;** – Bernd Buffen May 20 '16 at 03:43
  • @BerndBuffen As long as name is unique among those people; as we know it isn't as a generalized rule. ;) – Uueerdo May 20 '16 at 16:31

2 Answers2

2

Maybe with a subquery like:

SELECT * FROM employees WHERE birth_date = (SELECT min(birth_date) FROM employees);
Kame
  • 21
  • 2
0

You can combine solutions:

The LIMIT clause is used to specify the number of records to return. Choose one and only SELECT rows with that birth_date.

SELECT * FROM employees where birth_date = (
SELECT birth_date FROM employees ORDER BY birth_date ASC LIMIT 1
);
EoinS
  • 5,405
  • 1
  • 19
  • 32