Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the last row with SQL Server

Tags:

sql

sql-server

What is the most efficient way to read the last row with SQL Server?

The table is indexed on a unique key -- the "bottom" key values represent the last row.

like image 384
rp. Avatar asked Oct 07 '08 05:10

rp.


People also ask

How do I get the last row of data in SQL?

To get the last record, the following is the query. mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1; The following is the output. The above output shows that we have fetched the last record, with Id 4 and Name Carol.

How do I get the last value in a column in SQL Server?

We could use LAST_VALUE() in SQL Server to find the last value from any table. LAST_VALUE() function used in SQL server is a type of window function that results the last value in an ordered partition of the given data set.

How do I select the first and last row in SQL?

To get the first and last record, use UNION. LIMIT is also used to get the number of records you want.

How do I select the last 3 rows in SQL?

Try only this:- SELECT * FROM reset ORDER BY ASC LIMIT (FOUND_ROWS() - 3), 3 and check if it is giving the last 3 rows from your table in ascending order!!!


Video Answer


2 Answers

If you're using MS SQL, you can try:

SELECT TOP 1 * FROM table_Name ORDER BY unique_column DESC  
like image 127
EggyBach Avatar answered Sep 22 '22 06:09

EggyBach


select whatever,columns,you,want from mytable  where mykey=(select max(mykey) from mytable); 
like image 43
Adam Pierce Avatar answered Sep 22 '22 06:09

Adam Pierce