UPDATE movies SET rating = 10; -- ๋ชจ๋ rating ๊ฐ์ด 10์ผ๋ก ๋ณ๊ฒฝ๋จ
DELETE movies; -- ๋ชจ๋ movies ๋ฅผ ์ญ์ ํจ
-- WHERE ๋ฅผ ์ฌ์ฉํด ์์น๋ฅผ ์ขํ & AND ๋ฑ ๋ค์ํ ์กฐ๊ฑด์ ์ถ๊ฐํ ์ ์
UPDATE movies SET rating = 10 WHERE title = 'The Lord of The Rings';
UPDATE movies SET rating = rating + 2 WHERE title = 'The Lord of The Rings';
-- ใด์ ๋ฐ์ดํธํ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ ์ ์์
DELETE movies WHERE movie_id = 2;
SELECT director, SUM(revenue) as TOTAL_revenue-- 4rd
FROM movies -- 1st
WHERE -- 2nd
director IS NOT NULL
AND revenue IS NOT NULL
GROUP BY director -- 3th
ORDER BY total_revenue DESC; -- 5th
SELECT release_date, ROUND(AVG(rating), 2) AS avg_rating -- 4rd
FROM movies -- 1st
WHERE -- 2nd
rating IS NOT NULL AND release_date IS NOT NULL
GROUP BY release_date -- 3th
ORDER BY avg_rating DESC; -- 5th
HAVING - WHERE์ ๋น์ทํ๊ฒ row๋ฅผ ํํฐ๋งํ๋ ๊ธฐ๋ฅ, ์คํํ๋ ์๊ฐ์ด ๋ค๋ฆ
ใด WHERE ๋ค์, GROUP BY ๋ค์์ ์คํ๋จ
ใด GROUP BY์ ํจ๊ป ์ฌ์ฉ
SELECT release_date, ROUND(AVG(rating), 2) AS avg_rating -- 5th
FROM movies -- 1st
WHERE -- 2nd
rating IS NOT NULL AND release_date IS NOT NULL
GROUP BY release_date -- 3th
HAVING avg_rating > 6 -- 4rd
ORDER BY avg_rating DESC; -- 6th
-- Q1. What is the average rating of each director??
SELECT director, AVG(rating) AS avg_rating
FROM movies
WHERE director IS NOT NULL AND rating IS NOT NULL
GROUP BY director
ORDER BY avg_rating DESC;
-- * that has more than 5 movies
SELECT director, AVG(rating) AS avg_rating
FROM movies
WHERE director IS NOT NULL AND rating IS NOT NULL
GROUP BY director
HAVING COUNT(*) > 5
ORDER BY avg_rating DESC;
-- Q2. How many movies are in each genre?
SELECT genres, COUNT(genres) AS cnt_genres
FROM movies
WHERE genres IS NOT NULL
GROUP BY genres
ORDER BY cnt_genres DESC;
-- Q3. How many movies have a rating greater than 6? what is the most common?
SELECT rating
FROM movies
WHERE rating > 6
GROUP BY rating
ORDER BY count(*) DESC;
#4.15 Super Practice Part 2
-- Q1. Find the number of movies released each year.
SELECT release_date, COUNT(*)
FROM movies
WHERE release_date IS NOT NULL
GROUP BY release_date
ORDER BY release_date;
-- Q2. List the top 10 years with the highest average movie runtime.
SELECT release_date, AVG(runtime) AS avg_runtime
FROM movies
GROUP BY release_date
ORDER BY avg_runtime DESC
LIMIT 10;
-- Q3. Calculate the average rating for movies released in the 21st century.
SELECT AVG(rating)
FROM movies
WHERE release_date > 2000;
-- Q4. Find the director with the highest average movie runtime.
SELECT director
FROM movies
WHERE director IS NOT NULL AND RUNTIME IS NOT NULL
GROUP BY director
ORDER BY avg(runtime) DESC
LIMIT 1;
-- Q5. List the top 5 most prolific directors.
-- (those who have directed the most movies)
SELECT director
FROM movies
WHERE director IS NOT NULL
GROUP BY director
ORDER BY COUNT(*) DESC
LIMIT 5;
-- Q6. Find the highest and lowest rating of each director.
SELECT director, MAX(rating) AS high_rating, MIN(rating) AS low_rating
FROM movies
WHERE director IS NOT NULL AND rating IS NOT NULL
GROUP BY director;
-- Q7. Find the director that has made the most money (revenue - budget)
SELECT director, SUM(revenue)-SUM(budget) AS earn
FROM movies
WHERE director IS NOT NULL AND revenue IS NOT NULL AND budget IS NOT NULL
GROUP BY director
ORDER BY earn DESC;
-- Q8. Calculate the average rating for movies longer than 2 hours.
SELECT AVG(rating)
FROM movies
WHERE runtime >= 120 AND rating IS NOT NULL
-- Q9. Find the year with the most movies released.
SELECT release_date
FROM movies
WHERE release_date IS NOT NULL
GROUP BY release_date
ORDER BY COUNT(*) DESC
LIMIT 1;
-- Q10. Find the average runtime of movies for each decade.
SELECT (release_date/10)*10 AS decade, avg(runtime)
FROM movies
WHERE release_date IS NOT NULL AND runtime IS NOT NULL
GROUP BY decade
ORDER BY COUNT(*) DESC;
-- Q11. List the top 5 years
-- where the difference between the highest and lowest rated movie
-- was the greatest.
SELECT release_date, MAX(rating) - MIN(rating) AS difference
FROM movies
WHERE release_date IS NOT NULL AND RATING IS NOT NULL
GROUP BY release_date
ORDER BY difference DESC
LIMIT 5;
-- Q12. List directors who have never made a movie shorter than 2 hours.
SELECT director
FROM movies
GROUP BY director
HAVING MIN(runtime) >= 120;
#4.16 Super Practice Part 3
-- Q13. Calculate the percentage of movies with a rating above 8.0.
SELECT
COUNT(CASE WHEN rating > 8 THEN 1 END) * 100 / COUNT(*) AS percentage
FROM movies;
-- Q14. Find the director with the highest ratio of movies rated above 7.0.
SELECT director,
COUNT(CASE WHEN rating > 7 THEN 1 END) * 100 / COUNT(*) AS ratio
FROM movies
WHERE director IS NOT NULL
GROUP BY director
HAVING COUNT(*) >= 5
ORDER BY ratio DESC;
-- Q15. Categorize and group movies by length.
SELECT
CASE WHEN runtime < 90 THEN 'Short'
WHEN runtime BETWEEN 90 AND 120 THEN 'Normal'
WHEN runtime > 120 THEN 'LONG'
END AS runtime_category,
COUNT(*) AS cnt_movies
FROM movies
WHERE runtime IS NOT NULL
GROUP BY runtime_category
ORDER BY cnt_movies DESC;
-- Q16. Categorize and group movies by flop or not
-- flop is more cost then income
SELECT
CASE WHEN revenue < budget THEN 'flop' ELSE 'Sucess'
END AS flop_category,
count(*) as cnt_movies
FROM movies
WHERE budget IS NOT NULL AND revenue IS NOT NULL
GROUP BY flop_category;