Beginning SQL
Beginning SQL Beginning SQL
Appendix A 382 You should use the following SQL if you’re using MS Access: SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName FROM (((FavCategory AS FC1 INNER JOIN FavCategory AS FC2 ON FC1.CategoryId = FC2.CategoryId) INNER JOIN Category AS C1 ON C1.CategoryId = FC1.CategoryId) INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId) INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId WHERE FC1.MemberId < FC2.MemberId; It might look horrendously long, but broken down into its parts it’s not that bad. The very first step is to pair up people with the same favorite category. To get pairs of people in the same row, you need to use a self-join of the FavCategory table: SELECT FC1.CategoryId, FC1.MemberId, FC2.MemberId FROM FavCategory FC1 INNER JOIN FavCategory FC2 ON FC1.CategoryId = FC2.CategoryId WHERE FC1.MemberId < FC2.MemberId; The WHERE clause is there to prevent the same person from being listed as a pair of themselves on the same row. This gives you the CategoryId and MemberIds involved, but the question requires the category name and the first and last names of the people. First, get the category title from the Category table by joining it to the current results: SELECT C1.Category, FC1.MemberId, FC2.MemberId FROM FavCategory FC1 INNER JOIN FavCategory FC2 ON FC1.CategoryId = FC2.CategoryId INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId WHERE FC1.MemberId < FC2.MemberId; Now you need to get the first and last names. For this, you need to join the MemberDetails table to the current results. However, on the same row are two different MemberIds: for FC1 and for FC2. You can’t just join MemberDetails once; you need two copies of the table joined. The first copy provides the first and last names for FC1’s MemberId, and the second copy provides the lookup for the first and last names from FC2’s MemberId: SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName FROM FavCategory FC1 INNER JOIN FavCategory FC2 ON FC1.CategoryId = FC2.CategoryId INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId WHERE FC1.MemberId < FC2.MemberId;
- Page 752: Chapter 13 Again, the shotgun appro
- Page 756: Chapter 13 358 Figure 13-3 Leaf Ind
- Page 760: Chapter 13 Try It Out Dropping Inde
- Page 764: Chapter 13 64k Block 64k Block Figu
- Page 768: Chapter 13 ❑ Inner joins look lik
- Page 774: Chapter 1 Exercise 1 Solution A Exe
- Page 778: Chapter 2 Exercise 1 Solution There
- Page 782: Email, DateOfJoining ) VALUES ( 7,
- Page 786: Chapter 3 Exercise 1 Solution First
- Page 790: As well as joining the tables, you
- Page 794: Figure A-2 Notice that Table1 is re
- Page 798: Executing the query provides the fo
- Page 802: Category COUNT(DVDPrice), SUM(DVDPr
- Page 808: Appendix A Your answer may differ,
- Page 812: Appendix A WHERE (SELECT COUNT(*) f
- Page 816: Appendix A Exercise 2 Solution CREA
- Page 820: Appendix A Chapter 13 Exercise 1 So
- Page 824: Appendix B 392 Click the database i
- Page 828: Appendix B 394 Click Install to beg
- Page 832: Appendix B 396 Select the Detailed
- Page 836: Appendix B 398 Leave it at the defa
- Page 840: Appendix B 400 Having entered your
- Page 844: Appendix B 402 Figure B-20 The inst
- Page 848: Appendix B 404 At the very top of t
- Page 852: Appendix B 406 Having done that, ma
Appendix A<br />
382<br />
You should use the following <strong>SQL</strong> if you’re using MS Access:<br />
SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName<br />
FROM (((FavCategory AS FC1 INNER JOIN FavCategory AS FC2<br />
ON FC1.CategoryId = FC2.CategoryId)<br />
INNER JOIN Category AS C1 ON C1.CategoryId = FC1.CategoryId)<br />
INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId)<br />
INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId<br />
WHERE FC1.MemberId < FC2.MemberId;<br />
It might look horrendously long, but broken down into its parts it’s not that bad. The very first step is to<br />
pair up people with the same favorite category. To get pairs of people in the same row, you need to use a<br />
self-join of the FavCategory table:<br />
SELECT FC1.CategoryId, FC1.MemberId, FC2.MemberId<br />
FROM FavCategory FC1 INNER JOIN FavCategory FC2<br />
ON FC1.CategoryId = FC2.CategoryId<br />
WHERE FC1.MemberId < FC2.MemberId;<br />
The WHERE clause is there to prevent the same person from being listed as a pair of themselves on the<br />
same row. This gives you the CategoryId and MemberIds involved, but the question requires the category<br />
name and the first and last names of the people. First, get the category title from the Category table<br />
by joining it to the current results:<br />
SELECT C1.Category, FC1.MemberId, FC2.MemberId<br />
FROM FavCategory FC1 INNER JOIN FavCategory FC2<br />
ON FC1.CategoryId = FC2.CategoryId<br />
INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId<br />
WHERE FC1.MemberId < FC2.MemberId;<br />
Now you need to get the first and last names. For this, you need to join the MemberDetails table to the<br />
current results. However, on the same row are two different MemberIds: for FC1 and for FC2. You can’t<br />
just join MemberDetails once; you need two copies of the table joined. The first copy provides the first<br />
and last names for FC1’s MemberId, and the second copy provides the lookup for the first and last<br />
names from FC2’s MemberId:<br />
SELECT C1.Category, MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName<br />
FROM FavCategory FC1 INNER JOIN FavCategory FC2<br />
ON FC1.CategoryId = FC2.CategoryId<br />
INNER JOIN Category C1 ON C1.CategoryId = FC1.CategoryId<br />
INNER JOIN MemberDetails MD1 ON MD1.MemberId = FC1.MemberId<br />
INNER JOIN MemberDetails MD2 ON MD2.MemberId = FC2.MemberId<br />
WHERE FC1.MemberId < FC2.MemberId;