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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:24:59+00:00 2026-05-17T17:24:59+00:00

I have two different queries which produce the same results. I wonder which one

  • 0

I have two different queries which produce the same results. I wonder which one is more efficent. The second one, I am using one select clause less, but I am moving the where to the outter select. Which one is executed first? The left join or the where clause?

Using 3 “selects”:

select * from
(
  select * from
  (
    select
      max(t.PRICE_DATETIME) over (partition by t.PRODUCT_ID) as LATEST_SNAPSHOT,
      t.*  
    from
      PRICE_TABLE t
  ) a
  where
    a.PRICE_DATETIME = a.LATEST_SNAPSHOT;
) r
left join
  PRODUCT_TABLE l on (r.PRODUCT_ID = l.PRODUCT_ID and r.PRICE_DATETIME = l.PRICE_DATETIME)

Using 2 selects:

select * from
(
  select
    max(t.PRICE_DATETIME) over (partition by t.PRODUCT_ID) as LATEST_SNAPSHOT,
    t.*
  from
    PRICE_TABLE t
) r
left join
  PRODUCT_TABLE l on (r.PRODUCT_ID = l.PRODUCT_ID and r.PRICE_DATETIME = l.PRICE_DATETIME)
where
  r.PRICE_DATETIME = r.LATEST_SNAPSHOT;

ps: I know, I know, “select star” is evil, but I’m writing it this way only here to make it smaller.

  • 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-17T17:25:00+00:00Added an answer on May 17, 2026 at 5:25 pm

    “I wonder which one is more efficent”

    You can answer this question yourself pretty easily by turning on statistics.

    set statistics io on
    set statistics time on
    
    -- query goes here
    
    set statistics io off
    set statistics time off
    

    Do this for each of your two queries and compare the results. You’ll get some useful output about how many reads SQL Server is doing, how many milliseconds each takes to complete, etc.

    You can also see the execution plan SQL Server generates viewing the estimated execution plan (ctrl+L or right-click and choose that option) or by enabling “Display Actual Execution Plan” (ctrl+M) and running the queries. That could help answer the question about order of execution; I couldn’t tell you off the top of my head.

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

Sidebar

Related Questions

I have two insert statements, almost exactly the same, which run in two different
I have two different grid controls on the same form. They share the same
So let's say I have two different functions. One is a part of the
I have two completely different forms in one template. How to process them in
I'm trying to determine the relative performance of two different queries and have two
In my application I have two queries which will be quite frequently used. The
I have a table where one or more entries with the same 'id' value
I have two queries below. how do i put them in one stored proc.
I have two nearly identical queries operating on essentially the same fields in two
I am trying to profile two different queries that do the same thing to

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.