Sql query questions

select *, quantity * unit_price as final
from order_items
where order_id = 6 and final > 30

im not getting the desired output for the above query. is there any error?

Hi puneet,

You cannot refer to a column alias (such as “final”) in the WHERE clause because the WHERE clause is evaluated before the SELECT clause. So you cannot use the column alias in the same query level where it was defined.

To fix your code you can reference the expression instead of using the alias ie:

select *, quantity * unit_price as final
from order_items
where order_id = 6 and (quantity * unit_price) > 30

1 Like

got it, thanks for letting me know. much appreciate it (: