From 2280b621e5c8dfeeb6cce1bf3290aea0fabaf78a Mon Sep 17 00:00:00 2001 From: Maurobalas Date: Fri, 22 Nov 2024 16:39:25 +0100 Subject: [PATCH] sesion2 --- .DS_Store | Bin 0 -> 6148 bytes SESIONES/.DS_Store | Bin 0 -> 6148 bytes SESIONES/Session_2/create.sql | 36 +++++++++ SESIONES/Session_2/join.sql | 102 ++++++++++++++++++++++++ SESIONES/Session_2/rating.sql | 144 ++++++++++++++++++++++++++++++++++ 5 files changed, 282 insertions(+) create mode 100644 .DS_Store create mode 100644 SESIONES/.DS_Store create mode 100644 SESIONES/Session_2/create.sql create mode 100644 SESIONES/Session_2/join.sql create mode 100644 SESIONES/Session_2/rating.sql diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1e24720b075798c51eec96b2a635a99c3f71e0a4 GIT binary patch literal 6148 zcmeHKU279T6upzic9v2IqRMUtTBD)gBH3{A5`LQHt5prPT1WvNC@t; z{vm&fe@D-JL^h4$n~2PXJLm45duEt3$<7WDvB4~ULewH64p-Rl(EP$wzw!-h*vvMN z=`kjh(;|m~VoxvV~QJ;%bfXaZ*>4ILU z=uZ8YKh;O`Vrtj&HJYYLQFgoESYxYsZ+nOD@Lm2k_#o#&8J4qR5azGd(@QDSXcdRi zRWg~6S`VJdtPGQEl50W|Pf+saRg%SWK9I94E;KhV9o*yIsI|XX^iFzhadg~Yw#DM) zsM{9(-tltj@rMVGPcKK;=}jg-V+}Bd|CP2qiz`rT+|c1R$g@;t@6os7t?C^*1)Ks- zfw}_r9l>|&Nf$c>oB~b(D4^aC!lG}nGN@l2X!I2T*hE?zV)c2bxWb}uu`-Ar7&BC$ zp)z~LV1|youywx0%AlbWvzHHMXJ+<>!u0GIU+8jTzCqVI1)Kuw3T&CjmOB5>et-X8 zC%Gr5fK%YVQh+sw!Ek^j*|T+Haq6rU@ON-!s;)BllY&4W#i&(B@jhG|@qy>0x9AOM1K|U;%6{pTPV?m28jlZPu|(} zdT#HgIG@eTS8tEkW(zZ$!-@9JVQSo`PwcEB4us<#&o$A0T(JJ%!hh)hk4aid0V(jW6!6)4vtHqqsLQZ@f33h5W>OsF literal 0 HcmV?d00001 diff --git a/SESIONES/Session_2/create.sql b/SESIONES/Session_2/create.sql new file mode 100644 index 0000000..61094e4 --- /dev/null +++ b/SESIONES/Session_2/create.sql @@ -0,0 +1,36 @@ +/* + Create table reviews with columns: + - film_id + - customer_id + - review_date + - review_description +*/ + +CREATE TABLE IF NOT EXISTS public.reviews_ng ( + film_id int2 NOT NULL, + customer_id int2 NOT NULL, + review_date date NOT NULL, + review_description text NOT NULL, + CONSTRAINT reviews_ng_pkey PRIMARY KEY (film_id, customer_id) +) + + +/* Insert data into reviews_ng */ + +INSERT INTO + public.reviews_ng (film_id, customer_id, review_description, review_date) +VALUES + (4, 7, 'The movie is a bit boring', '10-11-2023') + +/* Update */ + +UPDATE + public.reviews_ng +SET + review_date = '01-01-2024' +WHERE + film_id = 4 + + +SELECT * +FROM public.reviews_ng \ No newline at end of file diff --git a/SESIONES/Session_2/join.sql b/SESIONES/Session_2/join.sql new file mode 100644 index 0000000..f0cbac1 --- /dev/null +++ b/SESIONES/Session_2/join.sql @@ -0,0 +1,102 @@ +/* + Left Join: + Every Movie (even if it's not linked to any language) +*/ + +SELECT + * -- SELECT COUNT(*) +FROM + film f +LEFT JOIN + language l ON l.language_id = f.language_id + + +/* + Left Join: + Every language being 'French' (even if it's not linked to any movie) +*/ + +SELECT + l.name, f.title -- SELECT COUNT(*) +FROM + language l +LEFT JOIN + film f ON l.language_id = f.language_id +WHERE + l.name = 'French' + + +/* + Right Join equivalent: + Every language being 'French' (even if it's not linked to any movie) +*/ + +SELECT + l.name, f.title -- SELECT COUNT(*) +FROM + film f +RIGHT JOIN + language l ON l.language_id = f.language_id +WHERE + l.name = 'French' + + +/* + Join film, film_actor and actor tables + to show every movie an actor appears in +*/ + +SELECT + f.title, a.first_name, a.last_name -- SELECT COUNT(*) +FROM + film f +LEFT JOIN + film_actor fa ON f.film_id = fa.film_id +LEFT JOIN + actor a ON a.actor_id = fa.actor_id + + +/* + Join film, film_actor and actor tables + to show number of movies an actor appears in descending order + if number of movies > 40 + (name an last name together in one column) +*/ + +SELECT + a.first_name || ' ' || a.last_name AS "Actor", COUNT(*) AS "Movies" -- SELECT COUNT(*) +FROM + film_actor fa +LEFT JOIN + film f ON f.film_id = fa.film_id +LEFT JOIN + actor a ON a.actor_id = fa.actor_id +GROUP BY + a.first_name, a.last_name +HAVING + COUNT(*) > 40 +ORDER BY + COUNT(*) DESC + + +/* + Join film, film_actor and actor tables + to show number of actors appearing in a movie in descending order + if number of actors > 10 + (name an last name together in one column) +*/ + +SELECT + f.title AS "Film", COUNT(*) AS "Actors" -- SELECT COUNT(*) +FROM + film f +LEFT JOIN + film_actor fa ON f.film_id = fa.film_id +LEFT JOIN + actor a ON a.actor_id = fa.actor_id +GROUP BY + f.title +HAVING + COUNT(*) > 10 +ORDER BY + COUNT(*) DESC \ No newline at end of file diff --git a/SESIONES/Session_2/rating.sql b/SESIONES/Session_2/rating.sql new file mode 100644 index 0000000..48bd223 --- /dev/null +++ b/SESIONES/Session_2/rating.sql @@ -0,0 +1,144 @@ +/* Count movies by rating */ + +SELECT + COUNT(*) AS "Total" +FROM + film f +WHERE + f.rating IS NOT null +GROUP BY + f.rating + + +/* Average rental rate by rating with only 1 decimal place */ + +SELECT + ROUND(AVG(f.rental_rate), 1) AS "Avg. Rental Rate", f.rating AS "Rating" -- SELECT COUNT(*) +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating + +/* Minimum rental rate by rating */ + +SELECT + MIN(f.rental_rate) AS "Min. Rental Rate", f.rating AS "Rating" -- SELECT COUNT(*) +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating + +/* Maximum rental rate by rating */ + +SELECT + MAX(f.rental_rate) AS "Min. Rental Rate", f.rating AS "Rating" -- SELECT COUNT(*) +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating + +/* Average movie duration by rating */ + +SELECT + ROUND(AVG(f.length), 0) AS "Avg. Movie Duration", f.rating AS "Rating" -- SELECT COUNT(*) +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating + +/* Year of oldest movie by rating */ + +SELECT + MIN(f.release_year) AS "Oldest Movie", f.rating AS "Rating" -- SELECT COUNT(*) +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating + +/* Year of oldest movie by rating */ + +SELECT + MAX(f.release_year) AS "Oldest Movie", f.rating AS "Rating" -- SELECT COUNT(*) +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating + + +/* Grouped stats by rating */ + +SELECT + f.rating AS "Rating", + COUNT(*) AS "Total", + ROUND(AVG(f.rental_rate), 1) AS "Avg. Rental Rate", + MIN(f.rental_rate) AS "Min. Rental Rate", + MAX(f.rental_rate) AS "Min. Rental Rate", + ROUND(AVG(f.length), 0) AS "Avg. Movie Duration", + MIN(f.release_year) AS "Oldest Movie", + MAX(f.release_year) AS "Oldest Movie" +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating + + +/* Number of movies grouped by rating having at least 200 movies */ + +SELECT + f.rating AS "Rating", + COUNT(*) AS "Total" +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating +HAVING + COUNT(*) > 200 + + +/* Average rental rate grouped by rating having at least an average rental rate of 3 */ + +SELECT + f.rating AS "Rating", + ROUND(AVG(f.rental_rate), 1) AS "Avg. Rental Rate" +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating +HAVING + AVG(f.rental_rate) > 3 + + +/* Average movie duration grouped by rating having at least an average movie duration of 115 */ + +SELECT + f.rating AS "Rating", + ROUND(AVG(f.length), 0) AS "Avg. Movie Duration" +FROM + film f +WHERE + f.rental_rate IS NOT null +GROUP BY + f.rating +HAVING + ROUND(AVG(f.length), 0) > 115 + + +/* Join */ \ No newline at end of file