Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting First and Last record from a table in SQL Server

I am using the following queries to find first as well as last inserted records from a table

SELECT TOP (1) titlenumber 
FROM cataloguebase

SELECT TOP(1) titlenumber 
FROM cataloguebase
ORDER BY titlenumber DESC; 

but how can I get first and last record using single query? At list is possible using HQL query?

like image 995
user2617574 Avatar asked Aug 22 '13 12:08

user2617574


1 Answers

Use MAX and MIN:

SELECT 
  MAX(titlenumber) AS MaxTitleNumber, 
  MIN(titlenumber) AS MinTitleNumber
FROM cataloguebase
like image 59
Mahmoud Gamal Avatar answered Sep 28 '22 18:09

Mahmoud Gamal