devxlogo

Selecting One Record Before and After a Value

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 
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist