SQL multiple columns in greater than expression

Viewed 561

Saw the following SQL relating to paging results with cursors and am having trouble finding more information on how part of it works:

SELECT b.* FROM books b
WHERE (b.name, id) > (select b2.name, b2.id
                      from books b2
                      where b2.id = ?
                      )
ORDER BY b.name;

What happens when you have multiple columns within a single comparison expression? I haven't found any other examples of this.

1 Answers

The comparisons are made as "tuples", from left to right. So, the first value is compared in each tuple, then the next is compared. So:

  • (1, 2) > (1, 1) --> true
  • (1, 1) > (1, 1) --> false
  • (2, 1) > (2, 2) --> false
  • (2, 1) > (1, 10) --> true
Related