Selecting One Record Before and After a Value

Suppose you want to select records from a table (say orders from a Northwind database), before and after a certain value (orderid). For instance, one order before and after 10250. You’d write a query like this:

SELECT TOP 3* FROM ordersWHERE OrderId >=(SELECT MAX(OrderId) FROM orders WHERE OrderId < 10250)ORDER BY OrderId 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: