WELP
PLEASE GIVE EXPERT OPINION ON MY WORK! THANKS
STEP 6: Now write a query to do a LEFT JOIN on the tables, selecting the same columns as the previous question. How are the results of this query different? Would this or the INNER JOIN be more useful for a log of reviews?
-- STEP 6:
SELECT places.name, places.average_rating,
reviews.username, reviews.rating, reviews.review_date, reviews.note
FROM places
LEFT JOIN reviews
ON places.id = reviews.place_id
WHERE places.type = 'Restaurant';
INNER JOIN: This type of join only includes rows where there is a match in both tables. So, with INNER JOIN, you’ll get only the reviews for restaurants that have at least one review.
LEFT JOIN: This type of join includes all places (restaurants) regardless of whether they have reviews or not. If a restaurant has no reviews, the columns from the “reviews” table will have NULL values.
If you want a log of all restaurants and their reviews, including those without any reviews, then a LEFT JOIN might be more useful. If you only want a log of restaurants with at least one review, then an INNER JOIN would be more appropriate.
STEP 7: What about the places without reviews in our dataset? Write a query to find all the id
s of places that currently do not have any reviews in our reviews
table.
-- STEP 7:
SELECT *
FROM places
LEFT JOIN reviews
ON places.id = reviews.place_id
WHERE reviews.place_id IS NULL;
STEP 8: Sometimes on Welp, there are some old reviews that aren’t useful anymore. Write a query using the WITH
clause to select all the reviews that happened in 2020. JOIN
the places
to your WITH
query to see a log of all reviews from 2020.
-- STEP 8:
WITH reviews_2020 AS (
SELECT *
FROM reviews
WHERE strftime('%Y', review_date) = '2020'
)
SELECT *
FROM places
LEFT JOIN reviews_2020
ON places.id = reviews_2020.place_id
LIMIT 10;
STEP 9: Businesses want to be on the lookout for …ahem… difficult reviewers. Write a query that finds the reviewer with the most reviews that are BELOW the average rating for places.
--STEP 9:
SELECT reviews.username, COUNT(*)
FROM reviews
JOIN places
ON reviews.place_id = places.id
WHERE reviews.rating < (SELECT AVG(average_rating)
FROM places)
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
