Home >Database >Mysql Tutorial >How Can I Efficiently Retrieve the Last Row in SQL Server?
Reading the Last Row in SQL Server Efficiently
In SQL Server, accessing the last row can be achieved efficiently when an index is present on a unique key. The "bottom" values of this unique key typically indicate the last row in the table.
To retrieve the last row in such scenarios, you can utilize the following SQL query:
SELECT TOP 1 * FROM table_Name ORDER BY unique_column DESC;
The TOP 1 clause specifies that only the top (first) row should be returned. The ORDER BY clause sorts the table's rows in descending order based on the unique_column. This sorting brings the last row to the front of the result set, making it the first and only row returned by the query.
This approach leverages the index on the unique key to quickly identify and fetch the last row without scanning the entire table, ensuring efficient performance.
The above is the detailed content of How Can I Efficiently Retrieve the Last Row in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!