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

  • SEARCH
  • Home
  • 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 7962727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:25:15+00:00 2026-06-04T05:25:15+00:00

I have a query like: SELECT R.* FROM (SELECT A, B, (SELECT smth from

  • 0

I have a query like:

SELECT 
    R.*     
FROM 
    (SELECT A, B, 
            (SELECT smth from another table) as C,
     ROW_NUMBER() OVER (ORDER BY C DESC) AS RowNumber 
     FROM SomeTable) R
WHERE 
     RowNumber BETWEEN 10 AND 20

This gives me an error on ORDER BY C DESC.

I understand why this error is caused, so I’ve thought of adding another SELECT with ORDER BY and only than selecting rows from 10 to 20. But I don’t think it’s good to have 3 nested SELECT commands.

How else is it possible to select these rows?

  • 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-04T05:25:18+00:00Added an answer on June 4, 2026 at 5:25 am

    A column cannot refer to an alias on same level, you have to table-derive it first, or use CTE.

    SELECT 
        R.* , ROW_NUMBER() OVER (ORDER BY C DESC) AS RowNumber     
    FROM 
        (SELECT A, B, (SELECT smth from another table) as C
         FROM SomeTable) R
    -- WHERE 
         -- but you still cannot do this
         -- RowNumber BETWEEN 10 AND 20
    

    Need to do this:

    select S.*
    from
    (
        SELECT 
            R.* , ROW_NUMBER() OVER (ORDER BY C DESC) AS RowNumber     
        FROM 
            (SELECT A, B, 
                    (SELECT smth from another table) as C
             FROM SomeTable) R
    ) as s
    where s.RowNumber between 10 and 20
    

    To avoid deep nesting and to make it at least look pleasant, use CTE:

    with R as
    (
         SELECT A, B, (SELECT smth from another table) as C
         FROM SomeTable
    )
    ,S AS 
    (
        SELECT R.*, ROW_NUMBER() OVER (ORDER BY C DESC) AS RowNumber
        FROM R
    )
    SELECT S.*
    FROM S
    WHERE S.RowNumber BETWEEN 1 AND 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like so : SELECT * FROM table WHERE premium =
I like to have a query like this: select data from table where (x
if i have a query like : SELECT * FROM table WHERE id IN
I have a query like this: SELECT field FROM table WHERE column1 = 'x'
I have an SQL query like: SELECT blah FROM table WHERE field1 % functCall(otherField1,
I have a query like this: SELECT * FROM table WHERE id IN (2,4,1,5,3);
I have a query like: SELECT ID FROM SomeTable WHERE ZipCode IN (SELECT Zip
I have a simple query like: select count(*) from table Can I speed up
Here I have a query like below: SELECT field FROM table WHERE value IN
I have a query like SELECT * FROM myTable WHERE key LIKE 'XYZ' The

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.