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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:32:14+00:00 2026-05-24T02:32:14+00:00

Lets say I have 2 tables. The first table, table_1, contains each posted content

  • 0

Lets say I have 2 tables. The first table, table_1, contains each posted content including

table_1
   title, 
   author name, 
   email, 
   city_name, etc. 

The second table provides a lookup for table_1. It has 2 columns,

table_2
   city_id and 
   city_name. 

For instance, city_id =1 corresponds to New York, city_id =2 corresponds to Chicago… and so on. Under the ‘city’ column in table1, the city_id is listed which can easily be joined with table 2, producing a readable city name.

Would the following statement be as efficient as using a WHERE with city_id? Reason being is that I would be filtering results based on a city which is a string and I don’t want (or need?) to correlate each input to its matching ID number in table2.

SELECT table1.city, table2.city_name 
FROM table1 
WHERE table2.city_name=(input city name)
JOIN table2.city_name ON table2.city_id = table1.city
  • 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-24T02:32:15+00:00Added an answer on May 24, 2026 at 2:32 am

    Because the join is an inner join the following should lead to equivalent execution plans. (That is, they should exhibit the same performance characteristics — write the SQL clearly and let the SQL engine do the dirty optimization work.)

    As presented in the other answers:

    SELECT table1.*, table2.city_name 
    FROM table1 
    JOIN table2 ON table1.city_id = table2.city_id
    WHERE table2.city_name = (city_input);
    

    And, as what I believe is the “optimized form” presented in the question:

    SELECT table1.*, t2.city_name 
    FROM table1 
    JOIN (SELECT * FROM table2
          WHERE table2.city_name = (city_input)) AS t2
    ON table1.city_id = t2.city_id
    

    This is because of the Relation Algebra Model that SQL follows; at least under RA the equality (“selection”) here can be moved across the join (a “natural join” in RA) while keeping the same semantics. Of course, “to make for certain”, run a query analysis. The most basic form is using EXPLAIN.

    Happy coding.

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

Sidebar

Related Questions

I have three tables, we'll call them table1, table2, and table3. Lets say each
i am working with MsSql server 2008, lets say i have table invoices contains
Let's say I have 2 tables. The first table is a list of personas.
So lets say I have TABLE1 and TABLE2. CREATE TABLE TABLE1 ( first_id int
Let's say I have a table like this: name |order_id ======================= first_record | 0
Lets say I have five tables named table1, table2 ... table5. I have already
Lets say I have two tables - Cat and Cat owner that are linked
Lets say I have two tables tblA ( tableAID INT IDENTITY(1,1), foo VARCHAR(100)) tblB
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
Lets say I have data in two tables. In one I have Order ID

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.