0

I understand that I can't reference an alias in the WHERE clause, but why is that? Is it interpreted differently?

Something like this generates an error:

declare @myTable table 
(
    num numeric(5,2),
    den numeric(5,2)
)
insert into @mytable 
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1

select num/den as 'calc' from @myTable
where calc is not null
order by calc

But this returns rows:

declare @myTable table 
(
    num numeric(5,2),
    den numeric(5,2)
)
insert into @mytable 
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1

select num/den as 'calc' from @myTable
--where calc is not null
order by calc
fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110

1 Answers1

0

As mentioned in Cannot use Alias name in WHERE clause but can in ORDER BY, it's due to the natural query processing order:

  1. FROM
  2. ON
  3. OUTER
  4. WHERE
  5. GROUP BY
  6. CUBE | ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
  10. ORDER BY
  11. TOP
fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110