I need a query which will display top 7 users by the total indicator of reviews written and ratings given, indicating the total number of pages in the books to which they reviewed
Structure of 3 tables given:
Books
: book_id, author_id, title, num_pages, publication_date, publisher_idRatings
: rating_id, book_id, username, ratingReviews
: review_id, book_id, username, text
Queries I tried this far
SELECT
username,
SUM(reviews.reviews_id) AS total_reviews,
SUM(ratings.rating) AS total_ratings,
SUM(books.num_pages) AS total_pages
FROM
reviews
JOIN
ratings ON reviews.book_id = ratings.book_id
AND reviews.username = ratings.username
JOIN
books ON books.book_id = reviews.book_id
GROUP BY
username
ORDER BY
total_reviews + total_ratings DESC
LIMIT 7;
2
Answers
try this query :
total_ratings
andtotal_reviews
are not recognized ! Use your query as a subquery then apply the order and limit on it :