select model from (
select price, model from pc where price = (select max(price) from pc)
union
select price, model from laptop where price = (select max(price) from laptop)
union
select price, model from printer where price = (select max(price) from printer)
) t1 where price = (select max(price) from (
select price, model from pc where price = (select max(price) from pc)
union
select price, model from laptop where price = (select max(price) from laptop)
union
select price, model from printer where price = (select max(price) from printer)
) t2 )
I’m very new to SQL so my question is very simple, but I would like to sort out one point. Am I right that this query can not be simplified to something like this?
select model from (
select price, model from pc where price = (select max(price) from pc)
union
select price, model from laptop where price = (select max(price) from laptop)
union
select price, model from printer where price = (select max(price) from printer)
) t1 where price = (select max(price) from t1)
And if it can not be, is it a bad thing that we run two same subqueries?
I still say to go with one table, which is best practice design. (Not duplicating identical tables unnecessarily.)
Doing so enables this query…
But, if you can’t, or won’t, trust the optimiser to deal with the consequences of the UNIONs…
The optimiser will understand how to optimise this so as to remove redundant searches.
EDIT:
As a compromise, you can make a unified view, and query that…