Average Review Ratings — SQL Question Asked By Amazon

  • Post last modified:September 12, 2022
  • Reading time:2 mins read
Photo by Caspar Camille Rubin on Unsplash

Introduction

  • In this article we will solve Average Review Ratings SQL question which is asked by Amazon as per DataLemur website.

If you don’t know DatLemur then please do visit , it’s one of the best website to practice SQL questions and improve your SQL skills.

Question

  • We have been given reviews table, and our job is get average rating for each product for every month.
  • As we can see in the output , each product along with month has avg_stars column.

Solution

  • At first , we need to group all the records by product_id and month, and then we can perform average operation on the stars for that product during that month.
  • We have been given submit_date as datetime column, so we need to first extract month from submit date.
  • Once we have month column , we can use it in select as well as group by clause.
  • Since we have to round the average stars value upto 2 decimal , we can use round function.
  • We will also use order by month and product_id , because it was asked in question
  • Here we can see we get the desire output.

Submission

  • Our solution gets accepted by platform.

Conclusion

  • In this article we solved SQL question asked by Amazon. We used simple group by and order by approach to get the desired result. We also had to extract month from datetime column.

Leave a Reply