logo

Back to questions

Top Rated Businesses

Medium

This is the same question as problem #27 in the SQL Chapter of Ace the Data Science Interview!

Assume you are given the table below containing information on user reviews. Write a query to obtain the number and percentage of businesses that are top rated. A top-rated busines is defined as one whose reviews contain only 4 or 5 stars.

Output the number of businesses and percentage of top rated businesses rounded to the 2 decimal places.

Table:

Column NameType
business_idinteger
review_idinteger
review_starsinteger
review_datedatetime

Example Input:

business_idreview_idreview_starsreview_date
5321234507/13/2022 12:00:00
8241452307/13/2022 12:00:00
8192341507/13/2022 12:00:00
7161325407/14/2022 12:00:00
4231434207/14/2022 12:00:00

Example Output:

business_numtop_business_pct
360.00

Explanation: There are 3 business with the rating at least 4 - business ids 532;819;716. The total count of the businesses is 5, resulting in a percentage of 3/5 = 60%.

PostgreSQL 14

Refer friends to get bonus content & cool prizes.