Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:57:24+00:00 2026-06-16T06:57:24+00:00

Title says it all, why can’t I use a windowed function in a where

  • 0

Title says it all, why can’t I use a windowed function in a where clause in SQL Server?

This query makes perfect sense:

select id, sales_person_id, product_type, product_id, sale_amount
from Sales_Log
where 1 = row_number() over(partition by sales_person_id, product_type, product_id order by sale_amount desc)

But it doesn’t work. Is there a better way than a CTE/Subquery?

EDIT

For what its worth this is the query with a CTE:

with Best_Sales as (
    select id, sales_person_id, product_type, product_id, sale_amount, row_number() over (partition by sales_person_id, product_type, product_id order by sales_amount desc) rank
    from Sales_log
)
select id, sales_person_id, product_type, product_id, sale_amount
from Best_Sales
where rank = 1

EDIT

+1 for the answers showing with a subquery, but really I’m looking for the reasoning behind not being able to use windowing functions in where clauses.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:57:25+00:00Added an answer on June 16, 2026 at 6:57 am

    why can’t I use a windowed function in a where clause in SQL Server?

    One answer, though not particularly informative, is because the spec says that you can’t.

    See the article by Itzik Ben Gan – Logical Query Processing: What It Is And What It Means to You and in particular the image here. Window functions are evaluated at the time of the SELECT on the result set remaining after all the WHERE/JOIN/GROUP BY/HAVING clauses have been dealt with (step 5.1).

    really I’m looking for the reasoning behind not being able to use
    windowing functions in where clauses.

    The reason that they are not allowed in the WHERE clause is that it would create ambiguity. Stealing Itzik Ben Gan’s example from High-Performance T-SQL Using Window Functions (p.25)

    Suppose your table was

    CREATE TABLE T1
    (
    col1 CHAR(1) PRIMARY KEY
    )
    
    INSERT INTO T1 VALUES('A'),('B'),('C'),('D'),('E'),('F')
    

    And your query

    SELECT col1
    FROM T1
    WHERE ROW_NUMBER() OVER (ORDER BY col1) <= 3
    AND col1 > 'B'
    

    What would be the right result? Would you expect that the col1 > 'B' predicate ran before or after the row numbering?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title pretty much says it all, but in SQL Server 2005 Management Studio,
The title pretty much says it all. How can I view the original SQL
The title basically says it all. I'm aware this can't be done using traditional
The title more or less says it all: I have a function which takes
Title basically says it all. This is my script: keepProgramRunning = True a =
The title pretty much says it all. I'm trying to use jQuery's ability (hoping
The title basically says it all. I mainly want to do this so that
Title kind of says it all. I just can't seem to find a DictionaryOrDefault
Title pretty much says it all... is there an easy way I can simulate
Update: The question title can be misleading. This was not Go's fault at all.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.