Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 8 260 Now you need to find a list of cities in which a meeting has never been held: SELECT LocationId, City FROM Location WHERE LocationId NOT IN (SELECT LocationId FROM Attendance); The subquery is used to find all LocationIds from the Location table that are not in the Attendance table. This subquery provides the following results: LocationId City 3 Big City Now you need to combine the WHERE clauses and add them to a DELETE statement. Doing so combines the WHERE clause of the preceding query, which included the following condition: LocationId NOT IN (SELECT LocationId FROM Attendance); Likewise, it combines the SELECT statement of the first example as a subquery: SELECT COUNT(*), City FROM MemberDetails GROUP BY City, State; The condition and the subquery are merged into the WHERE clause of the DELETE statement: DELETE FROM Location WHERE (SELECT COUNT(*) FROM MemberDetails WHERE Location.City = MemberDetails.City AND Location.State = MemberDetails.State GROUP BY City, State)

Chapter 8<br />

260<br />

Now you need to find a list of cities in which a meeting has never been held:<br />

SELECT LocationId, City<br />

FROM Location<br />

WHERE LocationId NOT IN (SELECT LocationId FROM Attendance);<br />

The subquery is used to find all LocationIds from the Location table that are not in the Attendance table.<br />

This subquery provides the following results:<br />

LocationId City<br />

3 Big City<br />

Now you need to combine the WHERE clauses and add them to a DELETE statement. Doing so combines<br />

the WHERE clause of the preceding query, which included the following condition:<br />

LocationId NOT IN (SELECT LocationId FROM Attendance);<br />

Likewise, it combines the SELECT statement of the first example as a subquery:<br />

SELECT COUNT(*), City<br />

FROM MemberDetails<br />

GROUP BY City, State;<br />

The condition and the subquery are merged into the WHERE clause of the DELETE statement:<br />

DELETE FROM Location<br />

WHERE (SELECT COUNT(*) FROM MemberDetails<br />

WHERE Location.City = MemberDetails.City<br />

AND<br />

Location.State = MemberDetails.State<br />

GROUP BY City, State)

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

Saved successfully!

Ooh no, something went wrong!