Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

Chapter 3 The overlapping portions of the three data sets form the final results set. The ON statements define the overlap area, which is summarized as follows: ❑ Every record in the FavCategory table must have a matching record in the Category table with the same value in the CategoryId field. ❑ Every record in the Category table must have a matching record in the FavCategory table with the same value in the CategoryId field. ❑ Every record in the FavCategory table must have a matching record in the MemberDetails table with the same value in the MemberId field. ❑ Every record in the MemberDetails table must have a matching record in the Category table with the same value in the MemberId field. Documenting the remaining steps in the SQL in diagram form would consume precious pages, so hopefully SQL’s set-based nature is clear. By now, you should be familiar enough with sets and inner joins to try out a few. Try It Out Using Inner Joins to Form Sets 1. Using the following SQL, generate a list for the film club chairperson of all the members who don’t live in a city in which the club holds meetings. Don’t forget to create a set diagram. SELECT MemberDetails.FirstName, MemberDetails.LastName, MemberDetails.City, MemberDetails.State FROM MemberDetails INNER JOIN Location ON (MemberDetails.City Location.City AND MemberDetails.State = Location.State) OR (MemberDetails.City = Location.City AND MemberDetails.State Location.State) ORDER BY MemberDetails.LastName; 2. The club’s chairperson also wants a list of all the members who have attended meetings, the date of attendance, and where the meeting was held. To create the list, use the following SQL: SELECT MemberDetails.MemberId, MemberDetails.FirstName, MemberDetails.LastName, Attendance.MeetingDate, Location.City FROM (MemberDetails INNER JOIN Attendance ON MemberDetails.MemberId = Attendance.MemberId) INNER JOIN Location ON Location.LocationId = Attendance.LocationId WHERE Attendance.MemberAttended = ‘Y’ ORDER BY MeetingDate, Location.City, LastName, FirstName; How It Works 108 First, you need to create a list of all members not living in a city in which club meetings are held. The data for meeting locations resides in the Location table. This information provides one results set. Not

Chapter 3<br />

The overlapping portions of the three data sets form the final results set. The ON statements define the<br />

overlap area, which is summarized as follows:<br />

❑ Every record in the FavCategory table must have a matching record in the Category table with<br />

the same value in the CategoryId field.<br />

❑ Every record in the Category table must have a matching record in the FavCategory table with<br />

the same value in the CategoryId field.<br />

❑ Every record in the FavCategory table must have a matching record in the MemberDetails table<br />

with the same value in the MemberId field.<br />

❑ Every record in the MemberDetails table must have a matching record in the Category table<br />

with the same value in the MemberId field.<br />

Documenting the remaining steps in the <strong>SQL</strong> in diagram form would consume precious pages, so hopefully<br />

<strong>SQL</strong>’s set-based nature is clear.<br />

By now, you should be familiar enough with sets and inner joins to try out a few.<br />

Try It Out Using Inner Joins to Form Sets<br />

1. Using the following <strong>SQL</strong>, generate a list for the film club chairperson of all the members who<br />

don’t live in a city in which the club holds meetings. Don’t forget to create a set diagram.<br />

SELECT MemberDetails.FirstName, MemberDetails.LastName,<br />

MemberDetails.City, MemberDetails.State<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 />

2. The club’s chairperson also wants a list of all the members who have attended meetings, the<br />

date of attendance, and where the meeting was held. To create the list, use the following <strong>SQL</strong>:<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 />

How It Works<br />

108<br />

First, you need to create a list of all members not living in a city in which club meetings are held. The<br />

data for meeting locations resides in the Location table. This information provides one results set. Not

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

Saved successfully!

Ooh no, something went wrong!