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 7398971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:56:42+00:00 2026-05-29T03:56:42+00:00

Will the following two queries be executed in the same way? SELECT COUNT(*) from

  • 0

Will the following two queries be executed in the same way?

SELECT COUNT(*) from person ORDER BY last_name;

and

SELECT COUNT(*) from person;

Either way they should display the same results, so I was curious if the ORDER BY just gets ignored.

The reason I am asking is because I am displaying a paginated table where I will get 20 records at a time from the database and then firing a second query that counts the total number of records. I want to know if I should use the same criteria that the first query used, or if I should be removing all sorting from the criteria?

  • 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-05-29T03:56:42+00:00Added an answer on May 29, 2026 at 3:56 am

    According to the execution plan, the two queries are different. For example, the query:

    select count(*) from USER
    

    Will give me:

    INDEX (FAST FULL SCAN)  3.0 3   453812  3457        1   TPMDBO  USER_PK FAST FULL SCAN  INDEX (UNIQUE)  ANALYZED
    

    As you can see, we hit USER_PK which is the primary key of that table.

    If I sort by a non-indexed column:

    select count(*) from USER ORDER BY FIRSTNAME --No Index on FIRSTNAME
    

    I’ll get:

    TABLE ACCESS (FULL) 19.0    19  1124488 3457    24199   1   TPMDBO  USER    FULL    TABLE   ANALYZED    1
    

    Meaning we did a full table scan (MUCH higher node cost)

    If I sort by the primary key (which is already index,) Oracle is smart enough to use the index to do that sort:

    INDEX (FAST FULL SCAN)  3.0 3   453812  3457    13828   1   TPMDBO  USER_PK FAST FULL SCAN  INDEX (UNIQUE)  ANALYZED
    

    Which looks very similar to the first execution plan.

    So, the answer to your question is absolutely not – they are not the same. However, ordering by an index that Oracle is already seeking anyway will probably result in the same query plan.

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

Sidebar

Related Questions

I'm trying to create a single query that will combine the following two queries.
Will the following query evaluate to true (1), false (0), or NULL? SELECT '%'
select @result=@input.query('*') for xml raw,type Above statement will generate following alert: Msg 6819, Level
If I have two queries, which I will call horrible_query_1 and ugly_query_2 , and
I want to do the following two SQL Queries in Microsoft SQL SERVER UPDATE
In my application I have two queries which will be quite frequently used. The
I've the two following queries: declare @UserId as int set @UserId = 1 --
Will the following code result in a deadlock using C# on .NET? class MyClass
Will the following append() in the catch cause the rethrown exception to see the
Will the following program cause any problem during compiling and execution process? class A{

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.