Beginning SQL
Beginning SQL Beginning SQL
2. The film club chairperson also wants to know how many repeat orders each customer placed with the salesperson. The SQL to do this is shown below: SELECT SalesPerson.SalesPersonId, SalesPerson.FirstName, SalesPerson.LastName, MemberDetails.MemberId, MemberDetails.FirstName, MemberDetails.LastName, COUNT(*) - 1 FROM (Orders INNER JOIN MemberDetails ON Orders.MemberId = MemberDetails.MemberId) INNER JOIN SalesPerson ON Orders.SalesPersonId = SalesPerson.SalesPersonId GROUP BY SalesPerson.SalesPersonId, SalesPerson.FirstName, SalesPerson.LastName, MemberDetails.MemberId, MemberDetails.FirstName, MemberDetails.LastName HAVING COUNT(Orders.MemberId) > 1 ORDER BY SalesPerson.SalesPersonId; How It Works First, you need to think about what the question involves. Basically, it involves counting how many times a customer has ordered from the same salesperson. If the customer makes repeat orders from the same salesperson, then display that record in the results. Now you need to work out where the data to answer the question comes from. Details about orders are found in the Orders table. Start simple and create a SELECT list that obtains the SalesPersonId and MemberId — the customer who placed the order: SELECT SalesPersonId, MemberId FROM Orders; This basic query gives the following results: SalesPersonId MemberId 2 7 2 4 2 9 2 7 2 7 2 10 2 4 2 4 Advanced Queries Table continued on following page 277
- 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 562: Summary Queries within Queries This
- Page 568: Chapter 9 264 Figure 9-1 The SQL ne
- Page 572: Chapter 9 266 INSERT INTO OrderItem
- Page 576: Chapter 9 268 VALUES (5,7, 2,’200
- Page 580: Chapter 9 Now that you’ve added t
- Page 584: Chapter 9 272 FirstName LastName Mo
- Page 588: Chapter 9 274 DVDPrice column is in
- Page 592: Chapter 9 SalesPersonId FirstName L
- Page 598: Executing the preceding query provi
- Page 602: GROUP BY SalesPerson.SalesPersonId,
- Page 606: Executing the preceding SQL creates
- Page 610: ❑ Use IN with your subqueries. Wh
- Page 614: 10 Views Views, as the name implies
- Page 618: Try It Out Building a View 1. The p
- Page 622: FROM dbo.MemberDetails INNER JOIN d
- Page 626: For example, you might want a list
- Page 630: However, if you take the case of Me
- Page 634: However, someone writes a query tha
- Page 638: Summary As you learned, views are l
2. The film club chairperson also wants to know how many repeat orders each customer placed<br />
with the salesperson. The <strong>SQL</strong> to do this is shown below:<br />
SELECT SalesPerson.SalesPersonId,<br />
SalesPerson.FirstName,<br />
SalesPerson.LastName,<br />
MemberDetails.MemberId,<br />
MemberDetails.FirstName,<br />
MemberDetails.LastName,<br />
COUNT(*) - 1<br />
FROM (Orders INNER JOIN MemberDetails<br />
ON Orders.MemberId = MemberDetails.MemberId)<br />
INNER JOIN SalesPerson<br />
ON Orders.SalesPersonId = SalesPerson.SalesPersonId<br />
GROUP BY SalesPerson.SalesPersonId,<br />
SalesPerson.FirstName,<br />
SalesPerson.LastName,<br />
MemberDetails.MemberId,<br />
MemberDetails.FirstName,<br />
MemberDetails.LastName<br />
HAVING COUNT(Orders.MemberId) > 1<br />
ORDER BY SalesPerson.SalesPersonId;<br />
How It Works<br />
First, you need to think about what the question involves. Basically, it involves counting how many<br />
times a customer has ordered from the same salesperson. If the customer makes repeat orders from the<br />
same salesperson, then display that record in the results.<br />
Now you need to work out where the data to answer the question comes from. Details about orders are<br />
found in the Orders table. Start simple and create a SELECT list that obtains the SalesPersonId and<br />
MemberId — the customer who placed the order:<br />
SELECT SalesPersonId, MemberId<br />
FROM Orders;<br />
This basic query gives the following results:<br />
SalesPersonId MemberId<br />
2 7<br />
2 4<br />
2 9<br />
2 7<br />
2 7<br />
2 10<br />
2 4<br />
2 4<br />
Advanced Queries<br />
Table continued on following page<br />
277