Search code examples
sqloracle-sqldeveloperoracle-xe-18.4

Oracle SQL print first and last entry in a table?


So I need to print the oldest and youngest result in a table my code is

SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client
order by (client.clientdob) DESC
FETCH first 1 row only


SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client
order by (client.clientdob) ASC
FETCH first 1 ROW ONLY;

these 2 by themselves will return the right value but I cant get them both to run at once, it seems I cant use a Union with this setup, if possible a single select statement where I can get both the first and last would be even better. I am also using SQL developer if there is way to get both of these to show in the same SQL query result window that would be fine.


Solution

  • You can try the below -

    SELECT client.clientfirstname, client.clientlastname, client.clientdob
    FROM client where 
    client.clientdob=(select max(client.clientdob) from client)
    or 
    client.clientdob=(select min(client.clientdob) from client)