I have a sql query which makes use of the WITH statement.
The query looks like this:
WITH topAge as (
select top 1 * from ages
order by age
)
select * from topAge where ageGroup = 1
My question is whether the Where clause gets executed after the top statement, because this query retrieves no records whereas I know that there are records in the database that should be retrieved.
Thanks in advance for any help.
The answer is: Yes, the
ageGroup = 1predicate is only applied after selectingtop 1. Your query is equivalent to thisWhat you maybe want is this