World's Most Challening MySQL SQL Query (least I think so...)

Posted by keruilin on Stack Overflow See other posts from Stack Overflow or by keruilin
Published on 2010-04-17T11:52:04Z Indexed on 2010/04/17 12:03 UTC
Read the original article Hit count: 282

Filed under:
|
|

Whoever answers this question can claim credit for solving the world's most challenging SQL query, according to yours truly.

Working with 3 tables: users, badges, awards.

Relationships: user has many awards; award belongs to user; badge has many awards; award belongs to badge. So badge_id and user_id are foreign keys in the awards table.

The business logic at work here is that every time a user wins a badge, he/she receives it as an award. A user can be awarded the same badge multiple times. Each badge is assigned a designated point value (point_value is a field in the badges table). For example, BadgeA can be worth 500 Points, BadgeB 1000 Points, and so on. As further example, let's say UserX won BadgeA 10 times and BadgeB 5 times. BadgeA being worth 500 Points, and BadgeB being worth 1000 Points, UserX has accumulated a total of 10,000 Points ((10 x 500) + (5 x 1000)).

The end game here is to return a list of top 50 users who have accumulated the most badge points.

Can you do it?

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql