搜索此博客

2013年2月23日星期六

Examples of Group By Query


Table: persons
id
first_name
last_name
favorite_ride_id
Table: rides
id
name
Write a query that will return the names and votes for the top 10 most popular rides in order from most to least popular.
Note: to use group by, the attributes after the select keyword must appear in the GROUP BY clause or be used in an aggregate function.
 
SELECT rid.name, cnt.votes
FROM rides AS rid
 INNER JOIN
   (
     SELECT favorite_ride_id, count(1AS votes
     FROM persons
     GROUP BY favorite_rid_id
   ) AS cnt
       ON cnt.favorite_ride_id = rid.id
ORDER BY cnt.votes DESC;

没有评论:

发表评论