Find first and last record from table

Solution 1:

Find first last record table

To get the first record:

select col1 from tab1 order by col1 asc limit 1;
To get the last record:

select col1 from tab1 order by col1 desc limit 1;

Solution 2:

Find first last record table

SELECT
(SELECT column FROM table WHERE [condition] ORDER BY column LIMIT 1) as ‘first’,
(SELECT column FROM table WHERE [condition] ORDER BY column DESC LIMIT 1) as ‘last’

Solution 3:

Find first last record table

select * from table where id= (select id from tab1 order by col1 asc limit 1;)
or id=(select id from tab1 order by col1 desc limit 1; )

Leave a Reply

Your email address will not be published. Required fields are marked *