Beyond the Fundamentals of Subqueries

This section covers aspects of subqueries that you might consider to be beyond the fundamentals. I provide it as optional reading in case you feel very comfortable with the material covered so far in this chapter.

Returning Previous or Next Values

Suppose that you need to query the Orders table in the TSQLFundamentals2008 database and return, for each order, information about the current order, and also the previous order ID. The concept previous implies logical ordering, but because you know that the rows in a table have no order, you need to come up with a logical equivalent to the concept of "previous" that can be phrased with a T-SQL expression. One example of such a logical equivalent is "the maximum value ...

Get Microsoft® SQL Server® 2008 T-SQL Fundamentals now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.