Beginning SQL
Beginning SQL Beginning SQL
Clearly this query hasn’t returned just the duplicates. That’s because you need to add a link between the subquery and the outer query. Remember, you want only those rows where the name is the same but the StarId is different. You have the same names, but you need to add a link between the query and subquery that ensures that the StarIds are different: SELECT StarId, StarName, StarAge FROM FilmStars S1 WHERE StarName IN (SELECT StarName FROM FilmStars S2 WHERE S2.StarId < S1.StarId) ORDER BY StarName; In the preceding code, a clause is added to the subquery that ensures that only rows where the StarIds are different between the subquery and outer query are returned. This is done by specifying that S2.StarId — the subquery’s StarId column — should have a value that is less than the StarId column in S1 (the outer query’s StarId table). The ORDER BY clause is there only to make the results easier to read, and it isn’t needed for the final DELETE query. When you run this query, you get the following results: StarId StarName StarAge 3 Boris Carlot 102 6 Boris Carlot 102 7 Dusty Hoffperson 48 As you can see, the query has selected the unwanted duplicate data. Having confirmed that the query works, you can now convert it to a DELETE statement: DELETE FROM FilmStars WHERE StarName IN (SELECT StarName FROM FilmStars S2 WHERE S2.StarId < FilmStars.StarId); All you have to do is change SELECT to DELETE. You can also remove the alias, as some database systems don’t allow aliases with DELETE statements. In the subquery, S1 is changed to FilmStars, which references the outer FilmStars table mentioned in the DELETE FROM statement. Execute the query and you should find that three rows are deleted. Chapter 10 Exercise 1 Solution CREATE VIEW vFirstLastDOB AS SELECT FirstName, LastName, DateOfBirth FROM MemberDetails Exercise Answers 387
- 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 806: This is the final SQL and gives the
- Page 810: The results from the query are as 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
- Page 856: Appendix B 408 Figure B-30 MyTable
- Page 860: Appendix B 410 Figure B-32 Click Ne
Clearly this query hasn’t returned just the duplicates. That’s because you need to add a link between the<br />
subquery and the outer query. Remember, you want only those rows where the name is the same but the<br />
StarId is different. You have the same names, but you need to add a link between the query and subquery<br />
that ensures that the StarIds are different:<br />
SELECT StarId, StarName, StarAge FROM FilmStars S1<br />
WHERE StarName IN (SELECT StarName FROM FilmStars S2<br />
WHERE S2.StarId < S1.StarId)<br />
ORDER BY StarName;<br />
In the preceding code, a clause is added to the subquery that ensures that only rows where the StarIds<br />
are different between the subquery and outer query are returned. This is done by specifying that<br />
S2.StarId — the subquery’s StarId column — should have a value that is less than the StarId column in<br />
S1 (the outer query’s StarId table).<br />
The ORDER BY clause is there only to make the results easier to read, and it isn’t needed for the final<br />
DELETE query. When you run this query, you get the following results:<br />
StarId StarName StarAge<br />
3 Boris Carlot 102<br />
6 Boris Carlot 102<br />
7 Dusty Hoffperson 48<br />
As you can see, the query has selected the unwanted duplicate data. Having confirmed that the query<br />
works, you can now convert it to a DELETE statement:<br />
DELETE FROM FilmStars<br />
WHERE StarName IN<br />
(SELECT StarName FROM FilmStars S2 WHERE S2.StarId < FilmStars.StarId);<br />
All you have to do is change SELECT to DELETE. You can also remove the alias, as some database systems<br />
don’t allow aliases with DELETE statements. In the subquery, S1 is changed to FilmStars, which<br />
references the outer FilmStars table mentioned in the DELETE FROM statement. Execute the query and<br />
you should find that three rows are deleted.<br />
Chapter 10<br />
Exercise 1 Solution<br />
CREATE VIEW vFirstLastDOB AS<br />
SELECT FirstName, LastName, DateOfBirth<br />
FROM MemberDetails<br />
Exercise Answers<br />
387