Beginning SQL
Beginning SQL Beginning SQL
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)
- Page 510: 8 Queries within Queries This chapt
- Page 514: There are no films with a value in
- Page 518: The error wording depends on the da
- Page 522: Now execute the query. You receive
- Page 526: The query provides the following re
- Page 530: If you execute this query, you can
- Page 534: To obtain the same results as you w
- Page 538: MemberId 1 10 14 6 5 8 7 11 15 4 13
- Page 542: Now NOT EXISTS returns true, and as
- Page 546: This is the same as the query just
- Page 550: clause specifying that the film’s
- Page 554: 4. Next, quickly double-check a few
- Page 558: Now that you’ve confirmed that th
- Page 566: 9 Advanced Queries In database prog
- Page 570: FOREIGN KEY (OrderId) REFERENCES Or
- Page 574: INSERT INTO Orders(OrderId, MemberI
- Page 578: INSERT INTO OrderItems(OrderId,Film
- Page 582: A more complex question could be,
- Page 586: Advanced Queries What you really wa
- Page 590: FirstName and LastName. You need to
- Page 594: 2. The film club chairperson also w
- Page 598: Executing the preceding query provi
- Page 602: GROUP BY SalesPerson.SalesPersonId,
- Page 606: Executing the preceding SQL creates
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)