Beginning SQL

Beginning SQL Beginning SQL

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

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

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

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

Saved successfully!

Ooh no, something went wrong!