20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

surprisingly, the MemberDetails table holds member details, the second results set. Now it’s necessary to<br />

combine the two sets to get the result you want: a list of all members whose city is not listed in the<br />

Location table. More than one city may have the same name, so assume that such cities in the same state<br />

are the same. Therefore, you joined the MemberDetails table to the Location table using an INNER JOIN<br />

based on the City and State columns:<br />

FROM MemberDetails INNER JOIN Location<br />

ON (MemberDetails.City Location.City AND MemberDetails.State = Location.State)<br />

OR (MemberDetails.City = Location.City AND MemberDetails.State Location.State)<br />

ORDER BY MemberDetails.LastName;<br />

The ON clause is the key to this query, so you need results where the cities don’t match but the states do<br />

(same state but different city name). You also want to include records where the city name is the same<br />

but the state name is different (same city name but in a different state). It seems obvious when written<br />

down, but oftentimes looking at the two results sets and making the comparison in your mind first is<br />

helpful. The key fields in both tables are City and State, so you need to make your comparison with<br />

those fields. Figure 3-5 shows the set diagram.<br />

Figure 3-5<br />

SELECT City, State FROM Location<br />

City<br />

Big Apple City<br />

Windy Village<br />

Big City<br />

State<br />

New State<br />

Golden State<br />

Mega State<br />

SELECT City, State FROM MemberDetails<br />

City<br />

Townsville<br />

Townsville<br />

Windy Village<br />

New Town<br />

Orange Town<br />

Orange Town<br />

Big City<br />

Big City<br />

Windy Village<br />

Big Apple City<br />

Windy Village<br />

Big City<br />

Dover<br />

Extracting Information<br />

State<br />

Mega State<br />

Mega State<br />

Golden State<br />

New State<br />

New State<br />

New State<br />

Mega State<br />

Mega State<br />

Golden State<br />

New State<br />

Golden State<br />

Mega State<br />

Golden State<br />

Check if the Townsville record at the top of the MemberDetails table appears in the City field in any of<br />

the records in the Location set. Townsville doesn’t appear, so you know that Townsville is not a valid<br />

meeting location, and therefore it should form part of the results. You also need to take into account data<br />

that isn’t there but should be. For example, say that a city called Big City in Sunny State appears in the<br />

Location table. In this case, simply comparing the City columns in each table means that Big City, Sunny<br />

State, would not be included in the results even though clearly Big City, Mega State, and Big City, Sunny<br />

State, are totally different cities.<br />

109

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!