How to query the maximum value of two columns in two different tables?
I have a requirement.
I have two tables say TableA and TableB. Both having a column called "rec_id". My requirement is to get the maximum of the values contained in these two columns using a single query.
eg: In TableA, I have "rec_id" values as {1开发者_开发百科,5,6} and in TableB, I have "rec_id" values as {1,4,2}. So after executing the query, I want "6" as the result since 6 is the maximum value from these two columns, out of these two tables.
Thanks in Advance, Anish Kurian
select max(rec_id) from
(
(select rec_id from tablea)
union all
(select rec_id from tableb)
) combined
select max(rec_id) from
(
(select MAX(rec_id) AS rec_id from tablea)
union
(select MAX(rec_id) AS rec_id from tableb)
) combined
In comparison to Nathan Feger's answer this would be more performant
精彩评论