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.

Chapter 3<br />

112<br />

Next, link the MemberDetails table:<br />

SELECT<br />

MemberDetails.MemberId,<br />

MemberDetails.FirstName,<br />

MemberDetails.LastName,<br />

Attendance.MeetingDate,<br />

Location.City<br />

FROM<br />

(MemberDetails INNER JOIN Attendance<br />

ON MemberDetails.MemberId = Attendance.MemberId)<br />

INNER JOIN Location ON Location.LocationId = Attendance.LocationId<br />

ORDER BY MeetingDate, Location.City, LastName, FirstName<br />

Order the results by meeting date, location, last name, and finally, first name. Remember, however, that<br />

you require a list of members who have attended a meeting. Your results set from the Attendance table<br />

should include only those records where the MemberAttended field contains a Y. So, to finalize your<br />

query, add a WHERE clause:<br />

SELECT<br />

MemberDetails.MemberId,<br />

MemberDetails.FirstName,<br />

MemberDetails.LastName,<br />

Attendance.MeetingDate,<br />

Location.City<br />

FROM<br />

(MemberDetails INNER JOIN Attendance<br />

ON MemberDetails.MemberId = Attendance.MemberId)<br />

INNER JOIN Location ON Location.LocationId = Attendance.LocationId<br />

WHERE Attendance.MemberAttended = ‘Y’<br />

ORDER BY MeetingDate, Location.City, LastName, FirstName;<br />

Executing the final query provides the results shown in the following table:<br />

MemberId FirstName LastName MeetingDate City<br />

6 Jenny Jones 2004-01-01 Windy Village<br />

5 John Jones 2004-01-01 Windy Village<br />

1 Katie Smith 2004-01-01 Windy Village<br />

4 Steve Gee 2004-03-01 Orange Town<br />

1 Katie Smith 2004-03-01 Orange Town<br />

One final note before moving on: The vital point to remember is that <strong>SQL</strong> doesn’t compare just one<br />

record in one set to just one record in the second set; <strong>SQL</strong> compares each record in one set to every record<br />

in the other set, and vice versa. If the ON condition is true for a particular record, then the final results set

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

Saved successfully!

Ooh no, something went wrong!