I was wondering when a row subquery is performed with a comparison operator such as >
or >=
, is the order of comparison defined using lexicographic (i.e. dictionary) order or is it defined element-wise?
That is, for ROW (A, B)
, should
(79, 48) > (75, 52)
be TRUE
(dictionary) or FALSE
(element-wise) in a row query in the WHERE
clause?
I tested this in PostgreSQL, and it seems that it's using the dictionary order, i.e. (79, 48) > (75, 52)
is TRUE
because 79 > 75
and the second component doesn't matter therefore. Searching around, it seems that this is also the case with MySQL: MySQL row subquery comparison issue, and the MySQL documentation seems to be confusing on this point. Searching for postgresql row subquery
doesn't show much about the comparison order.
While the dictionary order makes sense from a computer science perspective, it may look a bit weird to a database user because the order of rows now depends on which column you list first in the SQL. For example, using dictionary order, we should have:
(52, 75) > (48, 79)
for ROW (B,A)
. The same rows are compared, the order is exactly the opposite because column B
is listed first.
My question is:
Is this behavior (use of dictionary order in row queries) from SQL standards/cross-vendor or is it implementation specific? Any references for this?