Reputation: 1
select *
from
(select * from person) as t1; /*ok*/
select *
from
(select * from person) as t1
where t1.birthday >= '1987-04-09'; /*ok*/
select *
from
(select * from person) as t1
where
t1.birthday = (select max(birthday) from t1); /* fails with 't1 doesn't exist' */
I know that the correct SQL for the 3rd query is
select *
from person
where person.birthday = (select max(birthday) from person) /*ok*/
Would anyone help me understand why the 3rd SQL query failed?
Thanks a lot
Upvotes: 0
Views: 51
Reputation: 31
The SQL Query Execution Order is:
In your case, 'from t1' will be executed first and it will throw an error because t1 hasn't yet been recognised by the DB.
Better use the table name instead of t1.
Upvotes: 1