Top Rated Businesses
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.
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%.
First, we need to identify businesses having reviews consisting of only 4 or 5 stars. We can do so by using a CTE to find the reviews with 4 or 5 stars across all its reviews.
Then, we can use a to find the total number of top-rated businesses with 4 and 5 stars, then divide this by the total number of businesses to find the percentage of top-rated businesses.