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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:47:36+00:00 2026-05-30T06:47:36+00:00

I have a two tables that look like this (this is an example of

  • 0

I have a two tables that look like this (this is an example of what I actually have).

Table 1:

Yr, Value
1990, 1
1990, 2
1991, 2
1992, 3

Table 2:

Dte, Value2, ID
1/1/1990, 10, 1
1/2/1990, 11, 1
1/3/1990, 12, 2
1/1/1991, 20, 1
1/2/1991, 21, 2

I would like to join the two tables first using a left join first, then discard some of the values from the joined set, then group by ID. The code I have written looks something like this:

    select avg(Value2) v2 
     from table2 
left join table1 on (year(dte)=yr) 
    where Value>1 
 group by ID;

Does the join get performed first and then the filter condition in the where statement discard the rows from the merged table or does the where condition get evaluated first and then the join? The example above is just for illustration and this is a more general question about how the SQL does the operation.

  • 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-30T06:47:37+00:00Added an answer on May 30, 2026 at 6:47 am

    this depends on the order in which your tables are stated and the order of your wheres. in this case, the where is executed afterwards. so it would be more efficient to declare the tables in the inverse order, since you have the where clause for table1.

    the way you do it, all the rows from table1 are joined with the rows from table2 using the on-clause to narrow it down. then, last, the where clause is used on the result of the join. however, like this:

       select avg(Value2) v2 
         from table1 
    left join table2 on (year(dte)=yr) 
        where Value>1 
     group by ID;
    

    the where clause is executed first on table1, then the join is performed using the on-clause. this is done on the already reduced version of table1, and therefor it’s more efficient.

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

Sidebar

Related Questions

I have two tables in my database that look like this CREATE TABLE IF
Let's say I have two tables that look like this: TH TH TH TH
I have two tables that look something like this: Checkpoint with field: Checkpoint_id Checkpoint_name
I have two tables in a teradata database that look like this accounts account_number
I have two tables that look like this Train +----------+-------------+------+-----+---------+-------+ | Field | Type
I have two tables that are related via a mapping table: keywords titles I
Say I have two tables, user and comment . They have table definitions that
I have two tables like that: Products(productID,categoryID,price) MarginOfProfit(categoryID,rate) Anytime we want to change prices
Feel like this should be something easy that I'm missing. I have a table
I have two tables that are joined together. A has many B Normally you

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.