how to retrieve the Top and Bottom records from a table

The following code shows how to retrieve the Top and Bottom records from a table .


WITH TopandBottom
AS
(
SELECT TOP 1 *
FROM Employee
ORDER BY Record_ID
UNION ALL
SELECT TOP 1 *
FROM Employee
ORDER BY Record_ID DESC
)
SELECT *
FROM TopandBottom


Comments

Author: ketha11 Jun 2009 Member Level: Silver   Points : 1

(SELECT TOP 1 *
FROM Employees where Id in (select top 2 Id from BloodGroups)
UNION all
SELECT TOP 1 *
FROM Employees where Id in (select max(Id) from BloodGroups))



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: