Beginning SQL
Beginning SQL Beginning SQL
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;
- Page 398: Converting Different Data Types Thi
- Page 402: CREATE Table MyTable(FirstColumn in
- Page 406: The COALESCE() Function The COALESC
- Page 410: Ideally, this first argument is the
- Page 414: LocationId Street City State 1 Main
- Page 418: 6 Grouping and Aggregating Data So
- Page 422: Running the query, complete with a
- Page 426: MemberId Street City State 11 Newis
- Page 430: Executing the SQL returns seven dis
- Page 434: For example, the following code add
- Page 438: Running the query gives the answer
- Page 442: SELECT MAX(LastName), MIN(LastName)
- Page 446: immediately after the GROUP BY stat
- Page 454: 7 Selecting Data from Different Tab
- Page 458: Selecting Data from Different Table
- Page 462: Note that MS Access insists that yo
- Page 466: City State Orange Town New State Wi
- Page 470: SELECT FM1.FilmName FROM Films AS F
- Page 474: Selecting Data from Different Table
- Page 478: effects of any WHERE clause. There
- Page 482: Now re-execute the query. You get a
- Page 486: the category list and a list of whi
- Page 490: Category FirstName LastName Horror
- Page 494: You can join the results from two o
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;