When I have to select a number of fields from different tables:
- do I always need to join tables?
- which tables do I need to join?
- which fields do I have to use for the join/s?
- do the joins effects reflect on fields specified in select clause or on where conditions?
Thanks in advance.
Think about joins as a way of creating a new table (just for the purposes of running the query) with data from several different sources. Absent a specific example to work with, let’s imagine we have a database of cars which includes these two tables:
If you wanted, say, to get a list of the license plates of all the Hondas in the database, that information is already contained in the
cartable. You can simplySELECT * FROM car WHERE make='Honda';Similarly, if you wanted a list of all the states beginning with “A” you can
SELECT * FROM state WHERE state_name LIKE 'A%';In either case, since you already have a table with the information you want, there’s no need for a join.
You may even want a list of cars with Colorado plates, but if you already know that “CO” is the state code for Colorado you can
SELECT * FROM car WHERE state_code='CO';Once again, the information you need is all in one place, so there is no need for a join.But suppose you want a list of Hondas including the name of the state where they’re registered. That information is not already contained within a table in your database. You will need to “create” one via a join:
Note that I’ve said absolutely nothing about what we’re
SELECTing. That’s a separate question entirely. We also haven’t applied aWHEREclause limiting what rows are included in the results. That too is a separate question. The only thing we’re addressing with the join is getting data from two tables together. We now, in effect, have a new table calledcar INNER JOIN statewith each row fromcarjoined to each row instatethat has the samestate_code.Now, from this new “table” we can apply some conditions and select some specific fields:
So, to answer your questions more directly, do you always need to join tables? Yes, if you intend to select data from both of them. You cannot select fields from
carthat are not in thecartable. You must first join in the other tables you need.Which tables do you need to join? Whichever tables contain the data you’re interested in querying.
Which fields do you have to use? Whichever fields are relevant. In this case, the relationship between cars and states is through the
state_codefield in both table. I could just as easily have writtenThis would, for each car, show any states whose abbreviations happen to match the car’s license plate number. This is, of course, nonsensical and likely to find no results, but as far as your database is concerned it’s perfectly valid. Only you know that
state_codeis what’s relevant.And does the join affect
SELECTed fields orWHEREconditions? Not really. You can still select whatever fields you want and you can still limit the results to whichever rows you want. There are two caveats.First, if you have the same column name in both tables (e.g.,
state_code) you cannot select it without clarifying which table you want it from. In this case I might writeSELECT car.state_code ...Second, when you’re using an
INNER JOIN(or on many database engines just aJOIN), only rows where your join conditions are met will be returned. So in my nonsensical example of looking for a state code that matches a car’s license plate, there probably won’t be any states that match. No rows will be returned. So while you can still use theWHEREclause however you’d like, if you have anINNER JOINyour results may already be limited by that condition.