0

I'm trying to learn Postgresql with the help of this website https://www.postgresqltutorial.com. But I'm confused in the order of operations.So I compared the order they say in this site with others, all are same to each other. In some queries the order of operations works as they mentioned like this one:

select customer_id C
from payment
where C > 10

and I got error that the alias (C) is not recognized as I expected because of the order of execution:

FROM -> WHERE -> SELECT

The problem is here:

select 
customer_id C
from payment
where customer_id > 10
group by C

and it works fine without any error! Postgresql knows what C is inside the group by clause But the operation execution order here is not what it should be:

FROM -> WHERE -> GROUP BY -> SELECT

So Postgresql knows what C is before select clause executes? how?

  • In theory the database _could_ allow to re-use the column alias in the WHERE clause. However the restriction that this isn't possible isn't really technically, but mandated by the SQL standard. –  Jun 30 '22 at 08:52
  • @a_horse_with_no_name I think you could construct interesting self-contradictory examples with aliases in the `WHERE` clause, but I have no example at hand. – Laurenz Albe Jun 30 '22 at 09:17

0 Answers0