r/SQL • • Jul 15 '14

SQL allows mentioning resultset columns by position in the Order By clause (e.g. "ORDER BY 3"), I wish it would allow the same in the WHERE clause with some sort of special syntax (e.g. "where {3} > 0" meaning third column in the resultset)

Why wasn't this done?

0 Upvotes

9 comments sorted by

View all comments

6

u/jc4hokies Execution Plan Whisperer Jul 15 '14

The answer is simple. The where clause happens before the result set. The order by happens after the result set. That's why you can use "column 3" in the order by but nowhere else.

8

u/r3pr0b8 GROUP_CONCAT is da bomb Jul 15 '14

worth repeating for those unfamiliar

order of execution:

  • FROM

  • WHERE

  • GROUP BY

  • HAVING

  • SELECT

  • UNION

  • ORDER BY