20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4. Next, quickly double-check a few of the results to see whether they really are correct. Having<br />

confirmed that they are, you can add the INSERT INTO bit:<br />

INSERT INTO FavCategory (CategoryId, MemberId)<br />

SELECT 9, MemberId FROM MemberDetails AS MD1<br />

WHERE EXISTS<br />

(SELECT * from FavCategory FC1<br />

WHERE FC1.CategoryId = 1 AND FC1.MemberId = MD1.MemberId<br />

AND NOT EXISTS<br />

(SELECT * FROM FavCategory AS FC2<br />

WHERE FC2.MemberId = FC1.MemberId AND<br />

FC2.CategoryId = 9));<br />

How It Works<br />

The INSERT INTO inserts the literal value 9 (representing the Film Noir’s CategoryId) and<br />

MemberId into the FavCategory table. When you execute the <strong>SQL</strong>, you should find that four<br />

rows are added. Execute it a second, a third, or however many times, and no more rows will be<br />

added as the query checks for duplication, a good safeguard.<br />

In the first step, you selected 9, or the CategoryId of the Thriller category, and also MemberId from the<br />

MemberDetails table.<br />

In the next step, you added a WHERE clause that uses a subquery. Using the EXISTS operator, it checks to<br />

see whether there are any rows when selecting records from the FavCategory table where the MemberId<br />

is the same as that in the MemberDetails table and where CategoryId is equal to 1 (the Thriller ID).<br />

Using FC1.MemberId = MD1.MemberId joins MemberDetails and FavCategory, making sure that the<br />

current rows in the MemberDetails and FavCategory tables are the same; otherwise the subquery would<br />

just return rows where any member had selected a CategoryId of 1. You also gave an alias to the<br />

FavCategory table of the inner query. The alias is then used in the inner query’s WHERE clause and in the<br />

third step when another query is nested inside the inner query from Step 2.<br />

Step 3 sees an inner, inner query added to the previous subquery’s WHERE clause. Its role is to look for<br />

members who already have category 9 as one of their favorites. It does this by checking that the innermost<br />

query doesn’t return rows where the CategoryId is 9. The inner subquery, with its FavCategory<br />

table with an alias of FC1, is linked to the innermost query where the FavCategory table has been given<br />

the alias FC2. They are linked by MemberId, because you want to make sure when checking for a member’s<br />

current favorites that it’s the same member. The second condition in the innermost query’s WHERE<br />

clause checks to see whether the favorite category is one with an ID of 9.<br />

In the final step, the outer query is used to provide the data for an INSERT INTO statement that adds<br />

favorite category 9 as a member’s favorite.<br />

Using Subqueries with the UPDATE Statement<br />

Queries within Queries<br />

As with INSERT INTO, you can use subqueries to supply values for updating or to determine the WHERE<br />

clause condition in the same way that subqueries can be used with regular queries’ WHERE clauses. An<br />

example makes this clear. Imagine that the film club chairperson has decided to boost profits by selling<br />

DVDs. To maximize profits, she wants films that are rated higher than 3 and that appear in four or more<br />

members’ favorite categories to have their prices hiked up to that of the highest price in the database.<br />

257

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

Saved successfully!

Ooh no, something went wrong!