Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 6 SELECT Category, COUNT(FavCategory.CategoryId) AS Popularity FROM FavCategory INNER JOIN Category ON FavCategory.CategoryId = Category.CategoryId GROUP BY Category.Category HAVING COUNT(FavCategory.CategoryId) > 3 ORDER BY Popularity DESC; If you’re using MS Access, you need to change the ORDER BY clause as shown below: ORDER BY COUNT(FavCategory.CategoryId) DESC; How It Works 204 The best way to deal with queries that seem a bit tricky at first (recall the discussion of the COUNT() function) is to break them up into smaller stages. You know that the FavCategory table contains data on each member’s favorite film categories, so that is your starting point; this leads you to the following SQL: SELECT FavCategory.CategoryId, COUNT(FavCategory.CategoryId) AS Popularity FROM FavCategory GROUP BY FavCategory.CategoryId; While numbers are fine for a computer, humans prefer names. In order to make the answer more readable, you need to display the category name. To obtain the category name, you need to access the Category table, linking the results set so far with the data in the Category table via the CategoryId field. To do this, you need to join the tables with an inner join, resulting in the following SQL: SELECT Category.Category, COUNT(FavCategory.CategoryId) AS Popularity FROM FavCategory INNER JOIN Category ON FavCategory.CategoryId = Category.CategoryId GROUP BY Category.Category; Next, to ensure that the results are ordered correctly (by popularity of category), add an ORDER BY clause: SELECT Category.Category, COUNT(FavCategory.CategoryId) AS Popularity FROM FavCategory INNER JOIN Category ON FavCategory.CategoryId = Category.CategoryId GROUP BY Category.Category ORDER BY Popularity DESC; Remember, though, that you want only groups where four or more members chose that category as their favorite, which is where a HAVING clause comes in. You can use a HAVING clause to restrict the final results to just those groups with four or more records inside them by adding HAVING COUNT(FavCategory.CategoryId) > 3: SELECT Category, COUNT(FavCategory.CategoryId) AS Popularity FROM FavCategory INNER JOIN Category ON FavCategory.CategoryId = Category.CategoryId GROUP BY Category.Category HAVING COUNT(FavCategory.CategoryId) > 3 ORDER BY Popularity DESC;

Chapter 6<br />

SELECT Category, COUNT(FavCategory.CategoryId) AS Popularity<br />

FROM FavCategory INNER JOIN Category<br />

ON FavCategory.CategoryId = Category.CategoryId<br />

GROUP BY Category.Category<br />

HAVING COUNT(FavCategory.CategoryId) > 3<br />

ORDER BY Popularity DESC;<br />

If you’re using MS Access, you need to change the ORDER BY clause as shown below:<br />

ORDER BY COUNT(FavCategory.CategoryId) DESC;<br />

How It Works<br />

204<br />

The best way to deal with queries that seem a bit tricky at first (recall the discussion of the COUNT()<br />

function) is to break them up into smaller stages.<br />

You know that the FavCategory table contains data on each member’s favorite film categories, so that is<br />

your starting point; this leads you to the following <strong>SQL</strong>:<br />

SELECT FavCategory.CategoryId, COUNT(FavCategory.CategoryId) AS Popularity<br />

FROM FavCategory<br />

GROUP BY FavCategory.CategoryId;<br />

While numbers are fine for a computer, humans prefer names. In order to make the answer more readable,<br />

you need to display the category name. To obtain the category name, you need to access the Category table,<br />

linking the results set so far with the data in the Category table via the CategoryId field. To do this, you<br />

need to join the tables with an inner join, resulting in the following <strong>SQL</strong>:<br />

SELECT Category.Category, COUNT(FavCategory.CategoryId) AS Popularity<br />

FROM FavCategory INNER JOIN Category<br />

ON FavCategory.CategoryId = Category.CategoryId<br />

GROUP BY Category.Category;<br />

Next, to ensure that the results are ordered correctly (by popularity of category), add an ORDER BY<br />

clause:<br />

SELECT Category.Category, COUNT(FavCategory.CategoryId) AS Popularity<br />

FROM FavCategory INNER JOIN Category<br />

ON FavCategory.CategoryId = Category.CategoryId<br />

GROUP BY Category.Category<br />

ORDER BY Popularity DESC;<br />

Remember, though, that you want only groups where four or more members chose that category as their<br />

favorite, which is where a HAVING clause comes in. You can use a HAVING clause to restrict the final<br />

results to just those groups with four or more records inside them by adding HAVING<br />

COUNT(FavCategory.CategoryId) > 3:<br />

SELECT Category, COUNT(FavCategory.CategoryId) AS Popularity<br />

FROM FavCategory INNER JOIN Category<br />

ON FavCategory.CategoryId = Category.CategoryId<br />

GROUP BY Category.Category<br />

HAVING COUNT(FavCategory.CategoryId) > 3<br />

ORDER BY Popularity DESC;

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

Saved successfully!

Ooh no, something went wrong!