Advertisement
Guest User

fc

a guest
Mar 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 28.25 KB | None | 0 0
  1. /*
  2. SOURCE /Users/davidayliffe/Google Drive/AGU/GD Docs/Blockbusters/answers.sql
  3. */
  4.  
  5. /* save as CRLF for windows */
  6.  
  7.  
  8. show databases;
  9. /*
  10. +--------------------+
  11. | Database           |
  12. +--------------------+
  13. | information_schema |
  14. | blockbusters       |
  15. +--------------------+
  16. 6 rows in set (0.06 sec)
  17. */
  18.  
  19. use blockbusters;
  20.  
  21. select customer_id, first_name, last_name
  22. from customer
  23. where active = 1;
  24.  
  25.  
  26. show tables;
  27. /*
  28. +----------------------------+
  29. | Tables_in_blockbusters     |
  30. +----------------------------+
  31. | actor                      |
  32. | actor_info                 |
  33. | address                    |
  34. | category                   |
  35. | city                       |
  36. | country                    |
  37. | customer                   |
  38. | customer_list              |
  39. | film                       |
  40. | film_actor                 |
  41. | film_category              |
  42. | film_list                  |
  43. | film_text                  |
  44. | inventory                  |
  45. | language                   |
  46. | nicer_but_slower_film_list |
  47. | payment                    |
  48. | rental                     |
  49. | sales_by_film_category     |
  50. | sales_by_store             |
  51. | staff                      |
  52. | staff_list                 |
  53. | store                      |
  54. +----------------------------+
  55. 23 rows in set (0.04 sec)
  56. */
  57.  
  58. desc customer;
  59. /*
  60. +-------------+----------------------+------+-----+-------------------+-----------------------------+
  61. | Field       | Type                 | Null | Key | Default           | Extra                       |
  62. +-------------+----------------------+------+-----+-------------------+-----------------------------+
  63. | customer_id | smallint(5) unsigned | NO   | PRI | NULL              | auto_increment              |
  64. | store_id    | tinyint(3) unsigned  | NO   | MUL | NULL              |                             |
  65. | first_name  | varchar(45)          | NO   |     | NULL              |                             |
  66. | last_name   | varchar(45)          | NO   | MUL | NULL              |                             |
  67. | email       | varchar(50)          | YES  |     | NULL              |                             |
  68. | address_id  | smallint(5) unsigned | NO   | MUL | NULL              |                             |
  69. | active      | tinyint(1)           | NO   |     | 1                 |                             |
  70. | create_date | datetime             | NO   |     | NULL              |                             |
  71. | last_update | timestamp            | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
  72. +-------------+----------------------+------+-----+-------------------+-----------------------------+
  73. 9 rows in set (0.01 sec)
  74. */
  75.  
  76. /*
  77.             SELECT STATEMENTS
  78. Usage:
  79.     select <the information I want>
  80.     from <table/tables>
  81.     where <criteria>;
  82.  
  83. For example:
  84.     select customer_id, first_name, last_name
  85.     from customer
  86.     where active = 1;
  87.  
  88. */
  89.  
  90. /*  ***************************************************
  91.     *** LEVEL 1 - Academy ninja                     ***
  92.     ***                                             ***
  93.     *** Focus:  simple select statements            ***
  94.     ***         simple aggregation of data          ***
  95.     ***         using count(), sum(), min()         ***
  96.     ***         and avg(), NULL and NOT NULL        ***
  97.     ***                                             ***
  98.     ***         ordering data                       ***
  99.     ***                                             ***
  100.     *** Limits: Joining not more than two tables    ***
  101.     ***         Single clause WHERE statements      ***
  102.     ***                                             ***
  103.     *** Estimated time: 1 hour                      ***
  104.     *************************************************** */
  105.  
  106. /*
  107. 1.1. How many categories of films are there?
  108. (±2 lines, 1 table)
  109. Answer: 16
  110. */
  111.  
  112.  
  113. select count(*)
  114. from category;
  115.  
  116.  
  117.  
  118. /*
  119. 1.2. Which database table has the most records?  How many?
  120. (±2 lines, 1 table)
  121. Answer:  payment = 205305
  122. */
  123.  
  124.  
  125.  
  126.  
  127.  
  128. /*
  129. 1.3. In which year was the film ‚"Gold River" released?
  130. (±3 lines, 1 table)
  131. Answer = 1998
  132. */
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /*
  139. 1.4. How many films are exactly 3 hours in length?
  140. (±3 lines, 1 table)
  141. Answer: 7
  142. */
  143.  
  144. select * from film WHERE length = 180;
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /*
  151. 1.5. Which are the THREE longest films?
  152. (±3 lines, 1 table)
  153. Answer: POND SEATTLE 190, CRYSTAL BREAKING 188, WORST BANGER 186
  154. */
  155.  
  156.  
  157.  
  158.  
  159.  
  160. /*
  161. 1.6. Which are the TWO most expensive films to replace?
  162. (±4 lines, 1 table)
  163. Answer: FANTASIA PARK   34.99, FLOATS GARDEN    34.99
  164. */
  165.  
  166. SELECT title, replacement_cost FROM film ORDER BY replacement_cost DESC;
  167.  
  168.  
  169.  
  170. /*
  171. 1.7. How many films does blockbusters have in all of their stores?
  172. (±4 lines, 2 tables, hint: it's NOT 1000)
  173. Answer: 71025
  174. */
  175.  
  176. SELECT count(*) FROM inventory;
  177.  
  178.  
  179.  
  180. /*
  181. 1.8. How many films are out on rent now?
  182. (±4 lines, 1 table, hint: use the "now()" function )
  183. Answer: 0
  184. */
  185. SELECT * from rental WHERE returned_date > NOW() and returned_date is null;
  186.  
  187.  
  188.  
  189.  
  190.  
  191. /*
  192. 1.9. What are the oldest films?  What are the most recent films?
  193. (±3 lines, 1 table)
  194. Answer: MENAGERIE RUSHMORE 1959, ARMY FLINTSTONES 2017
  195. */
  196.  
  197. SELECT * FROM film ORDER BY release_year LIMIT 1;
  198. SELECT * FROM film ORDER BY release_year DESC LIMIT 1;
  199.  
  200.  
  201. /*
  202. 1.10. What is the average price of a film rental?
  203. (±2 lines, 1 table)
  204. Answer: 19.994000
  205. */
  206.  
  207. SELECT avg(replacement_cost) FROM film;
  208.  
  209.  
  210.  
  211.  
  212. /*
  213. 1.11. How many customers gave not provided an email?
  214. (±3 lines, 1 table)
  215. Answer: 10
  216. */
  217.  
  218. SELECT count(*) FROM customer WHERE email IS null;
  219.  
  220. /*
  221. 1.12. How many customers did not return the DVD they rented?
  222. (±3 lines, 1 table)
  223. Answer: 1446
  224. */
  225.  
  226.   SELECT count(*) FROM rental WHERE returned_date is NULL;
  227.  
  228.  
  229.  
  230. /*
  231. 1.13. How many films were returned late?
  232. (±4 lines, 1 table)
  233. Answer: 1141
  234. */
  235.  
  236. SELECT count(*) FROM rental WHERE due_date < returned_date;
  237.  
  238.  
  239.  
  240. /*
  241. 1.14. How many Turkish cities are in the database?
  242. (±4 lines, 2 tables)
  243. Answer: 15
  244. */
  245.  
  246. SELECT count(*) FROM city WHERE country_id = 97;
  247.  
  248.  
  249.  
  250. /*
  251. 1.15. Which store has the most films?
  252. (±3 lines, 1 table)
  253. Answer: 2
  254. */
  255. SELECT store_id, count(*) AS total FROM inventory GROUP BY store_id ORDER BY total;
  256.  
  257.  
  258.  
  259.  
  260. /*
  261. 1.16. Which film can be rented for the shortest length of time?
  262. (±3 lines, 1 table)
  263. Answer: AGENT TRUMAN
  264. */
  265.  
  266. SELECT * FROM film ORDER BY rental_duration LIMIT 1;
  267.  
  268.  
  269. /*  ***************************************************
  270.     *** LEVEL 2 - Genin                             ***
  271.     ***                                             ***
  272.     *** Focus:  simple select statements            ***
  273.     ***         simple aggregation of data          ***
  274.     ***         using count(), sum(), min()         ***
  275.     ***         and avg(), NULL and NOT NULL        ***
  276.     ***                                             ***
  277.     ***         grouping data                       ***
  278.     ***                                             ***
  279.     *** Limits: Joining not more than five tables   ***
  280.     ***         Multi parameter WHERE clause        ***
  281.     ***                                             ***
  282.     *** Estimated time: 1 hour                      ***
  283.     *************************************************** */
  284.  
  285. use blockbusters;
  286.  
  287. /* save as CRLF for windows */
  288.  
  289. /*
  290. 2.0. Which staff member has rented the most films?
  291. (±4 lines, 2 tables)
  292. Answer:
  293. */
  294. select staff.staff_id, first_name, last_name, count(*) as films_rented
  295. from staff, rental
  296. where staff.staff_id = rental.staff_id
  297. group by staff.staff_id
  298. order by films_rented;
  299.  
  300.  
  301. /*
  302. 2.1. How many Turkish cities are in the database?
  303. (±4 lines, 2 tables)
  304. Answer: 15
  305. */
  306. SELECT country.country, count(*) AS no_of_cities
  307.   FROM city, country
  308.   WHERE city.country_id = country.country_id AND country = "Turkey"
  309.   GROUP BY country.country
  310.   ORDER BY country.country DESC;
  311.  
  312.  
  313.  
  314. /*
  315. 2.3. What is the most popular language of films?
  316. (±4 lines, 2 tables)
  317. Answer: Japanese
  318. */
  319.  
  320. SELECT language.name, count(*) AS no_of_pop
  321.   FROM film, language
  322.   WHERE film.language_id = language.language_id
  323.   GROUP BY language.name
  324.   ORDER BY no_of_pop DESC;
  325.  
  326. /*
  327. 2.4. Which customers have rented the most films?
  328. (±4 lines, 2 tables)
  329. Answer: BOBBY   BOUDREAU
  330. */
  331.  
  332.   SELECT customer.first_name, customer.last_name, count(*) AS number_of_rent
  333.     FROM customer, rental
  334.     WHERE customer.customer_id = rental.customer_id
  335.   GROUP BY customer.first_name, customer.last_name
  336.   ORDER BY number_of_rent DESC;
  337.  
  338.  
  339. /*
  340. 2.5. Which is the busiest store?
  341. (±6 lines, 3 tables)
  342. Answer: Rentertainment
  343. */
  344.  
  345. SELECT store.name, count(*) AS work
  346.   FROM store, rental, inventory
  347.   WHERE rental.inventory_id = inventory.inventory_id AND inventory.store_id = store.store_id
  348.   GROUP BY store.name
  349.   ORDER BY work DESC;
  350.  
  351.  
  352. /*
  353. 2.6. Find all actors in the film 'Sky Miracle'.
  354. (±7 lines, 3 tables)
  355. Answer: 12
  356.   ELVIS MARX
  357. SISSY   SOBIESKI
  358. WOODY   JOLIE
  359. KIRSTEN AKROYD
  360. WALTER  TORN
  361. WARREN  JACKMAN
  362. MERYL   GIBSON
  363. GRETA   MALDEN
  364. LAURA   BRODY
  365. LAURENCE    BULLOCK
  366. JON CHASE
  367. AUDREY  BAILEY
  368. */
  369.  
  370. SELECT title, count(*)
  371.   FROM film, film_actor
  372.   WHERE film_actor.film_id = film.film_id
  373.   AND title = "Sky Miracle"
  374.   GROUP BY title;
  375.  
  376.  
  377. SELECT first_name, last_name, title
  378.   FROM actor, film_actor, film
  379.   WHERE film.film_id = film_actor.film_id AND film_actor.actor_id = actor.actor_id
  380.   AND title = "Sky Miracle";
  381.  
  382. /*
  383. 2.7. In how many films was "Jeff Silverstone" an actor?
  384. (±7 lines, 3 tables)
  385. Answer: 25
  386. */
  387. SELECT first_name, last_name, count(*)
  388.   FROM actor, film_actor, film
  389.   WHERE film.film_id = film_actor.film_id AND film_actor.actor_id = actor.actor_id
  390.   AND first_name = "Jeff" AND last_name = "Silverstone";
  391.  
  392.  
  393.  
  394.  
  395. /*
  396. 2.8. Which country has the most customers? How many?
  397. (±7 lines, 4 tables)
  398. Answer: India   60
  399. */
  400.  
  401. SELECT country, count(*) AS numb
  402. FROM country, city, address, customer
  403. WHERE address.city_id = city.city_id
  404.   AND city.country_id = country.country_id
  405.   AND address.address_id = customer.address_id
  406.   GROUP BY country.country
  407.   ORDER BY numb DESC;
  408.  
  409.  
  410. /*
  411. 2.9. What is the most popular comedy film?
  412. (±9 lines; 5 tables)
  413. Answer: STRICTLY SCARFACE   325
  414. */
  415.  
  416. SELECT film.title, count(*) AS rent_numb
  417.   FROM film, inventory, rental, category, film_category
  418.   WHERE rental.inventory_id = inventory.inventory_id
  419.   AND film.film_id = inventory.film_id
  420.   AND film_category.film_id = film.film_id
  421.   AND film_category.category_id = category.category_id
  422.   AND category.name = "comedy"
  423.   GROUP BY title
  424.   ORDER BY rent_numb DESC;
  425.  
  426. /*
  427. 2.10. How many customers live in Turkey?
  428. (±7 lines, 4 tables)
  429. Answer: 15
  430. */
  431.  
  432. SELECT count(*)
  433.   FROM customer, address, city, country
  434.   WHERE address.address_id = customer.address_id
  435.   AND address.city_id = city.city_id
  436.   AND city.country_id = country.country_id
  437.   AND country = "Turkey";
  438.  
  439.  
  440.  
  441. /*  ***************************************************
  442.     *** LEVEL 3 - Chunin                            ***
  443.     ***                                             ***
  444.     *** Focus:  Joining not more than six tables    ***
  445.     ***         Multi parameter WHERE clause        ***
  446.     ***         Functions beyond count(*)           ***
  447.     ***                                             ***
  448.     *** Estimated time: 2 classes                   ***
  449.     *************************************************** */
  450.  
  451. use blockbusters;
  452.  
  453. /* save as CRLF for windows */
  454.  
  455. /*
  456. 3.0. suitable / How / Bangkok / films / for / are / children / store / at / the / many / of / branch / the ?
  457. (±8 lines, 5 tables)
  458. Question:  How many films are suitable for children at the Bangkok branch of the store?
  459. Answer: 1162
  460. */
  461.  
  462.  
  463. select count(*)
  464. from film, inventory, store, address, city
  465. where film.film_id = inventory.film_id
  466. and inventory.store_id = store.store_id
  467. and store.address_id = address.address_id
  468. and address.city_id = city.city_id
  469. and city.name = 'Bangkok'
  470. and film.rating = 'PG';
  471.  
  472.  
  473. /*
  474. 3.1. starred / Which / in / has / films / the / actor / most ?
  475. (±7 lines; 3 tables)
  476. Question: Which actor has starred in the most films?
  477. Answer: 42  GINA    DEGENERES
  478. */
  479. SELECT actor.actor_id, count(*) AS numb, actor.first_name, actor.last_name
  480.   FROM actor, film, film_actor
  481.   WHERE film_actor.actor_id = actor.actor_id
  482.   AND film.film_id = film_actor.film_id
  483.   GROUP BY actor.actor_id
  484.   ORDER BY numb DESC;
  485.  
  486.  
  487.  
  488.  
  489. /*
  490. 3.2.  were / DVDs / returned / How / late / many / (or not at all) ?
  491. (±4 lines, 1 table, hint: use the now() function; is null)
  492. Question: How many DVDs were returned late?
  493. Answer: 2591
  494. */
  495.  
  496. SELECT count(*)
  497.   FROM rental
  498.   WHERE due_date < returned_date
  499.   OR returned_date IS null;
  500.  
  501. /*
  502. 3.3. rented / they / not / returned / has / DVD(s) / the / Who ?
  503. (±8 lines, 4 tables, hint: use the now() function; is null)
  504. Question: Who rented the DVD(s) they has not returned?
  505. Answer:
  506. */
  507. SELECT customer.first_name, customer.last_name
  508.   FROM customer, rental
  509.   WHERE customer.customer_id = rental.customer_id
  510.   AND rental.returned_date IS null
  511.   AND due_date <= now();
  512.  
  513.  
  514.  
  515. /*
  516. 3.4. popular / is / to / most / Which / the / film / steal?
  517. (±8 lines, 4 tables, hint: use the now() function; is null)
  518. Question: Which film is the most popular to steal?
  519. Answer: SUMMER SCARFACE 8
  520. */
  521.  
  522. SELECT film.title, count(*) AS times
  523.   FROM rental, film, inventory
  524.   WHERE film.film_id = inventory.film_id
  525.   AND rental.inventory_id = inventory.inventory_id
  526.   AND rental.returned_date IS null
  527.   AND due_date <= now()
  528.   GROUP BY film.title
  529.   ORDER BY times DESC;
  530.  
  531.  
  532.  
  533. /*
  534. 3.5. many / English / How / documentaries / films / are ?
  535. (±8 lines, 4 tables)
  536. Question: How many films are English documentaries?
  537. Answer: 11
  538. */
  539.  
  540. SELECT count(*)
  541.   FROM film, film_category, category, language
  542.   WHERE language.name= "English" AND category.name = "Documentary"
  543.   AND film_category.category_id = category.category_id
  544.   AND film_category.film_id = film.film_id
  545.   AND film.language_id = language.language_id;
  546.  
  547.  
  548.  
  549. /*
  550. 3.6. $1200 / have / Which / customers / film / more / on / than / rentals / spent ?
  551. (±8 lines, 3 tables; hint:use the sum() function; use having() )
  552. Question: Which customers have spent more than $1200 on film rentals?
  553. Answer:
  554. 526 KARL    SEAL    1260.10
  555. 148 ELEANOR HUNT    1257.13
  556. 331 ERIC    ROBERT  1246.27
  557. 16  SANDRA  MARTIN  1240.07
  558. 21  MICHELLE    CLARK   1220.10
  559. 146 JAMIE   RICE    1204.31
  560. 210 ELLA    OLIVER  1202.25
  561. 293 MAE FLETCHER    1201.39
  562. */
  563.  
  564. SELECT customer.customer_id, customer.first_name, customer.last_name, SUM(payment.amount) AS spent
  565.   FROM customer, payment
  566.   WHERE payment.customer_id = customer.customer_id
  567.   GROUP BY customer.customer_id
  568.   ORDER BY spent DESC;
  569.  
  570. /*
  571. 3.7. popular / category / the / of / is / film / Which / most ?
  572. (±8 lines, 5 tables)
  573. Question: Which category film is the most popular?
  574. Answer: Sports, 14873 film.
  575. */
  576.  
  577. SELECT category.name, count(category.name) AS amo
  578.   FROM film, rental, inventory, category, film_category
  579.   WHERE film.film_id = inventory.film_id
  580.   AND inventory.inventory_id = rental.inventory_id
  581.   AND film.film_id = film_category.film_id
  582.   AND film_category.category_id = category.category_id
  583.   GROUP BY category.name
  584.   ORDER BY amo DESC;
  585.  
  586.  
  587.  
  588.  
  589.  
  590. /*
  591. 3.8. customers / rented / movies / horror / Which / have / >= 30  ?
  592. (±10 lines; 6 tables)
  593. Question: Which customers have rented >=30 horror movies?
  594. Answer:
  595. 346 ARTHUR  SIMPKINS    Horror  38
  596. 16  SANDRA  MARTIN  Horror  35
  597. 592 TERRANCE    ROUSH   Horror  32
  598. 275 CAROLE  BARNETT Horror  31
  599. 578 WILLARD LUMPKIN Horror  31
  600. 406 NATHAN  RUNYON  Horror  30
  601. 374 JEREMY  HURTADO Horror  30
  602. 508 MILTON  HOWLAND Horror  30
  603. */
  604.  
  605. SELECT customer.customer_id, customer.first_name, customer.last_name, category.name, count(category.name) AS per
  606.   FROM customer, film, rental, inventory, category, film_category
  607.   WHERE rental.customer_id = customer.customer_id
  608.   AND rental.inventory_id = inventory.inventory_id
  609.   AND inventory.film_id = film.film_id
  610.   AND film.film_id = film_category.film_id
  611.   AND film_category.category_id = category.category_id
  612.   AND category.name = "HORROR"
  613.   GROUP BY customer.customer_id
  614.   ORDER BY per DESC;
  615.  
  616.  
  617.  
  618.  
  619. /*
  620. 3.9. Which / income / generated / rental / category / film / the / highest ?
  621. (±9 lines, 6 tables)
  622. Question: Which category generated the highest rental income?
  623. Answer: Sports  47090.27
  624. */
  625.  
  626. SELECT category.name, sum(payment.amount) AS dadada
  627.   FROM category, film_category, film, inventory, rental, payment
  628.   WHERE film.film_id = film_category.film_id
  629.   AND film_category.category_id = category.category_id
  630.   AND film.film_id = inventory.film_id
  631.   AND inventory.inventory_id = rental.inventory_id
  632.   AND payment.rental_id = rental.rental_id
  633.   GROUP BY category.name
  634.   ORDER BY dadada DESC;
  635.  
  636.  
  637. /*
  638. 3.10. blockbusters / actor / has / money / the / earned / Which / most ?
  639. (±9 lines, 6 tables; hint use the 'sum()' function; see Question 3.1 for help)
  640. Question: Which ghostbusters actor has earned the most money?
  641. Answer: 107 GINA    DEGENERES   28360.98
  642. */
  643.  
  644.  
  645. SELECT actor.actor_id, actor.first_name, actor.last_name, SUM(payment.amount) AS sasas
  646.   FROM actor, film_actor, film, inventory, rental, payment
  647.   WHERE actor.actor_id = film_actor.actor_id
  648.   AND film_actor.film_id = film.film_id
  649.   AND film.film_id = inventory.film_id
  650.   AND inventory.inventory_id = rental.inventory_id
  651.   AND rental.rental_id = payment.rental_id
  652.   GROUP BY actor.actor_id
  653.   ORDER BY sasas DESC;
  654.  
  655.  
  656.  
  657.  
  658. /*  *******************************************************************************
  659.     *** LEVEL 4 - Jonin                                                         ***
  660.     ***                                                                         ***
  661.     *** Focus:  in()/not in()                                                   ***
  662.     ***                                                                         ***
  663.     *** Limits: Joining not more than six tables                                ***
  664.     ***         Multi parameter WHERE clause                                    ***
  665.     ***                                                                         ***
  666.     *** Estimated time: 2 lessons                                               ***
  667.     ******************************************************************************* */
  668.  
  669. use blockbusters;
  670.  
  671. /* save as CRLF for windows */
  672.  
  673. /*
  674. 4.0. or / or / this / customers / rented / Which / from / have / not / month / a movie / "Movie Madness Video" / "Cinefile" / "Video Paradiso" ?
  675. (±14 lines, 5 tables, hint: not in(); date_format() )
  676. Question: Which customers have not rented a movie from "Movie Madness Video" or "Cinefile" or "Video Paradiso" this month?
  677. Answer: 537 customers
  678. */
  679.  
  680.  
  681. select customer.customer_id, first_name, last_name
  682. from customer
  683. where (customer_id, first_name, last_name) not in
  684. (
  685.     select distinct customer.customer_id, first_name, last_name
  686.     from rental, customer, inventory, store
  687.     where rental.customer_id = customer.customer_id
  688.     and rental.inventory_id = inventory.inventory_id
  689.     and inventory.store_id = store.store_id
  690.     and date_format(rental_date, '%Y-%m') = date_format(now(), '%Y-%m')
  691.     and store.name in ('Movie Madness Video', 'Cinefile', 'Video Paradiso')
  692. )
  693. order by customer_id, first_name, last_name;
  694.  
  695.  
  696. /*
  697. 4.1. any / actors / not / Which / Animation / have / acted / or / Sports / in / films?
  698. (±10 lines, 6 tables, hint: not in)
  699. Question: Which actors have not acted in and Animation or Sports films?
  700. Answer: ADAM    HOPPER
  701.         MINNIE  KILMER
  702.         CHRIS   DEPP
  703. */
  704.  
  705. SELECT actor.actor_id, actor.first_name, actor.last_name
  706.   FROM actor
  707.   WHERE (actor.actor_id, actor.first_name, actor.last_name) NOT IN
  708. (  
  709. SELECT actor.actor_id, actor.first_name, actor.last_name
  710.   FROM actor, film, category, film_actor, film_category
  711.   WHERE actor.actor_id = film_actor.actor_id
  712.   AND category.category_id = film_category.category_id
  713.   AND film_actor.film_id = film_category.film_id
  714.   AND category.name in ("Sports", "Animation")
  715. );
  716.  
  717.  
  718. /*
  719. 4.2. films / Per / yet / rented / store / not / which / have / been ?
  720. (±9 lines, 7 tables, hint: not in)
  721. Question:Per store which films have not been rented yet?
  722. Answer: 2
  723. */
  724.  
  725. SELECT store.name, film.title
  726.   FROM film, store, inventory
  727.   WHERE (store.name, film.title) NOT IN
  728.   (
  729. SELECT store.name, film.title
  730.   FROM store, film, inventory, rental
  731.   WHERE inventory.store_id = store.store_id
  732.   AND inventory.film_id = film.film_id
  733.   AND inventory.inventory_id = rental.inventory_id
  734.   )
  735.   AND film.film_id = inventory.film_id
  736.   AND inventory.store_id = store.store_id;
  737.  
  738.  
  739.  
  740. /*
  741. 4.3. "Mr. Video" / copies / film / not / Which / does / have / any / of ?
  742. (±10 lines, 4 tables, hint: not in)
  743. Question: Which film does "Mr. Video" not have any copy of ?
  744. Answer:SPLASH GUMP, DISTURBING SCARFACE
  745. */
  746.  
  747. SELECT film.title
  748.   FROM film
  749.   WHERE (film.title) NOT IN
  750.   (
  751. SELECT film.title
  752.   FROM film, store, inventory
  753.   WHERE store.name = "Mr. Video"
  754.   AND film.film_id = inventory.film_id
  755.   AND store.store_id = inventory.store_id
  756.   );
  757.  
  758.  
  759. /*
  760. 4.4. available / only / film / Which / is / in / "Movie Star Planet"?
  761. (±10 lines, 5 tables, hint: not in)
  762. Question: Which film is available only in "Movie Star Planet"?
  763. Answer: SPLASH GUMP
  764. */
  765. SELECT film.title
  766.   FROM film
  767.   WHERE (film.title) NOT IN
  768.   (
  769. SELECT film.title
  770.   FROM film, inventory, store
  771.   WHERE film.film_id = inventory.film_id
  772.   AND inventory.store_id = store.store_id
  773.   AND store.name != "Movie Star Planet"
  774.   );
  775.  
  776.  
  777.  
  778. /*
  779. 4.5. Singapore / What / China / income / is / rented / the / films / total / from / the / which / been / have / in / the / stores / and ?
  780. (±10 lines, 7 tables, hint: in)
  781. Question: What is the total income from the films which have been rented in the store Singapore and China?
  782. Answer: 40023.97
  783. */
  784.  
  785. SELECT sum(payment.amount) AS totall
  786.   FROM payment, rental, inventory, store, address, country, city
  787.   WHERE payment.rental_id = rental.rental_id
  788.   AND rental.inventory_id = inventory.inventory_id
  789.   AND store.store_id = inventory.store_id
  790.   AND store.address_id = address.address_id
  791.   AND address.city_id = city.city_id
  792.   AND city.country_id = country.country_id
  793.   AND country.name = "China" OR "Singapore";
  794.  
  795.  
  796.  
  797.  
  798. /*  *******************************************************************************
  799.     *** LEVEL 5 - ANBU                                                          ***
  800.     ***                                                                         ***
  801.     *** Focus:  joining to the same table many times                            ***
  802.     ***                                                                         ***
  803.     *** Limits:                                                                 ***
  804.     ***                                                                         ***
  805.     ***                                                                         ***
  806.     *** Estimated time: 2 lessons                                               ***
  807.     ******************************************************************************* */
  808.  
  809. use blockbusters;
  810.  
  811. /* save as CRLF for windows */
  812.  
  813.  
  814. /*
  815. 5.0. Which films star both "SUSAN DAVIS" and "HARVEY HOPE"?
  816. (±8 lines, 5 tables)
  817. Answer:
  818. */
  819.  
  820. select film.film_id, title
  821. from film, film_actor as fa1, actor as a1, film_actor as fa2, actor as a2
  822.  
  823. where a1.first_name = 'SUSAN' and a1.last_name = 'DAVIS'
  824. and a1.actor_id = fa1.actor_id
  825. and fa1.film_id = film.film_id
  826.  
  827. and a2.first_name = 'HARVEY' and a2.last_name = 'HOPE'
  828. and a2.actor_id = fa2.actor_id
  829. and fa2.film_id = film.film_id;
  830.  
  831.  
  832.  
  833.  
  834. /*
  835. 5.1. Which actor starred in both "SECRETARY ROUGE" and "ELIZABETH SHANE"?
  836. (±8 lines, 5 tables)
  837. Answer:
  838. */
  839.  
  840. SELECT concat(actor.first_name, " ", actor.last_name) AS fullname
  841.   FROM actor, film_actor AS ift1, film AS maiftl1, film_actor AS ift2, film AS maiftl2
  842.   WHERE actor.actor_id = ift1.actor_id
  843.   AND ift1.film_id = maiftl1.film_id
  844.   AND maiftl1.title = "SECRETARY ROUGE"
  845.  
  846.   AND actor.actor_id = ift2.actor_id
  847.   AND ift2.film_id = maiftl2.film_id
  848.   AND maiftl2.title = "ELIZABETH SHANE";
  849.  
  850. /*
  851. 5.2. List the customers for each store.  Like this:
  852. (±9 lines, 8 tables; hint: use the concat() function)
  853. Answer:
  854. +----------+-----------------+------------+----------------+-------------+----------------+-------------+--------------------+
  855. | store_id | name            | store_city | store_country  | customer_id | name           | cust_city   | cust_name          |
  856. +----------+-----------------+------------+----------------+-------------+----------------+-------------+--------------------+
  857. |        1 | Five Star Films | Edinburgh  | United Kingdom |          84 | SARA PERRY     | Atlixco     | Mexico             |
  858. |        1 | Five Star Films | Edinburgh  | United Kingdom |         106 | CONNIE WALLACE | Ivanovo     | Russian Federation |
  859. |        1 | Five Star Films | Edinburgh  | United Kingdom |         133 | PAULINE HENRY  | Torren      | Mexico             |
  860. |        1 | Five Star Films | Edinburgh  | United Kingdom |         148 | ELEANOR HUNT   | Saint-Denis | Runion             |
  861. |        1 | Five Star Films | Edinburgh  | United Kingdom |         152 | ALICIA MILLS   | Nagaon      | India              |
  862. +----------+-----------------+------------+----------------+-------------+----------------+-------------+--------------------+
  863. */
  864. SELECT store.store_id, store.name, store_city.name AS store_city, store_country.name AS store_country, customer_id,
  865.   concat(customer.first_name, " ", customer.last_name) AS full_name,
  866.   cust_city.name AS cust_city, cust_country.name AS cust_country
  867.   FROM store, customer, city AS store_city, country AS store_country, address AS store_address, city AS cust_city, country AS cust_country, address AS cust_address
  868.   WHERE customer.store_id = store.store_id
  869.   AND store.address_id = store_address.address_id
  870.   AND store_address.city_id = store_city.city_id
  871.   AND store_city.country_id = store_country.country_id
  872.  
  873.   AND customer.address_id = cust_address.address_id
  874.   AND cust_address.city_id = cust_city.city_id
  875.   AND cust_city.country_id = cust_country.country_id;
  876.  
  877.  
  878. /*
  879. 5.3. For each member of staff, find out who she/he reports to.  Like this:
  880. (±3 lines, 2 tables; hint: use the concat() function)
  881. Answer:
  882. +----------------------+----------------------+
  883. | staff_name           | manager_name         |
  884. +----------------------+----------------------+
  885. | Jon Stephens         | Mike Hillyer         |
  886. | Lajuana Borda        | Mike Hillyer         |
  887. | Sherrell Mcanally    | Mike Hillyer         |
  888. | Reyes Gwyn           | Mike Hillyer         |
  889. +----------------------+----------------------+
  890. */
  891.  
  892. SELECT concat(staff.first_name, " ", staff.last_name), concat(man.first_name, " ", man.last_name)
  893.   FROM staff, staff AS man
  894.   WHERE staff.manager_id = man.staff_id;
  895.  
  896. /*
  897. 5.4. How many Indonesian customers have rented "TOURIST PELICAN" *AND* "AIRPORT POLLOCK" *AND* "SPOILERS HELLFIGHTERS"?
  898. (±24 lines, 13 tables)
  899. Answer: 2
  900. */
  901. SELECT
  902.   customer.first_name,
  903.   customer.last_name
  904. FROM customer,
  905.      address,
  906.      city,  
  907.      country,
  908.      film AS film1,
  909.      film AS film2,
  910.      film AS film3,
  911.      rental AS rental1,
  912.      rental AS rental2,
  913.      rental AS rental3,
  914.      inventory AS inventory1,
  915.      inventory AS inventory2,
  916.      inventory AS inventory3
  917. WHERE customer.address_id = address.address_id
  918. AND address.city_id = city.city_id
  919. AND city.country_id = country.country_id
  920. AND country.name = "Indonesia"
  921.  
  922. AND rental1.customer_id = customer.customer_id
  923. AND rental1.inventory_id = inventory1.inventory_id
  924. AND inventory1.film_id = film1.film_id
  925. AND film1.title = "TOURIST PELICAN"
  926.  
  927. AND rental2.customer_id = customer.customer_id
  928. AND rental2.inventory_id = inventory2.inventory_id
  929. AND inventory2.film_id = film2.film_id
  930. AND film2.title = "AIRPORT POLLOCK"
  931.  
  932. AND rental3.customer_id = customer.customer_id
  933. AND rental3.inventory_id = inventory3.inventory_id
  934. AND inventory3.film_id = film3.film_id
  935. AND film3.title = "SPOILERS HELLFIGHTERS";
  936.  
  937.  
  938.  
  939.  
  940.  
  941. /*
  942. 5.5. Which Chinese customer rented an action film *AND* a travel film *AND* a Music film in the first 7 days of April 2016?
  943. (±8 lines, 19 tables; hint: '2016-04-01 00:00:00' & '2016-04-08 00:00:00')
  944. Answer: KELLY KNOTT
  945. */
  946.  
  947. SELECT
  948.   CONCAT(customer.first_name, " ", customer.last_name)
  949. FROM customer,
  950.      address,
  951.      city,
  952.      country,
  953.      category AS category1,
  954.      category AS category2,
  955.      category AS category3,
  956.      film_category AS film_category1,
  957.      film_category AS film_category2,
  958.      film_category AS film_category3,
  959.      film AS film1,
  960.      film AS film2,
  961.      film AS film3,
  962.      rental AS rental1,
  963.      rental AS rental2,
  964.      rental AS rental3,
  965.      inventory AS inventory1,
  966.      inventory AS inventory2,
  967.      inventory AS inventory3
  968. WHERE customer.address_id = address.address_id
  969. AND address.city_id = city.city_id
  970. AND city.country_id = country.country_id
  971. AND country.name = "China"
  972.  
  973. AND film1.film_id = film_category1.film_id
  974. AND film_category1.category_id = category1.category_id
  975. AND category1.name = "ACTION"
  976. AND rental1.customer_id = customer.customer_id
  977. AND rental1.inventory_id = inventory1.inventory_id
  978. AND inventory1.film_id = film1.film_id
  979. AND rental1.rental_date >= "2016-04-01 00:00:00"
  980. AND rental1.rental_date < "2016-04-08 00:00:00"
  981.  
  982. AND film2.film_id = film_category2.film_id
  983. AND film_category2.category_id = category2.category_id
  984. AND category2.name = "TRAVEL"
  985. AND rental2.customer_id = customer.customer_id
  986. AND rental2.inventory_id = inventory2.inventory_id
  987. AND inventory2.film_id = film2.film_id
  988. AND rental2.rental_date >= "2016-04-01 00:00:00"
  989. AND rental2.rental_date < "2016-04-08 00:00:00"
  990.  
  991. AND film3.film_id = film_category3.film_id
  992. AND film_category3.category_id = category3.category_id
  993. AND category3.name = "MUSIC"
  994. AND rental3.customer_id = customer.customer_id
  995. AND rental3.inventory_id = inventory3.inventory_id
  996. AND inventory3.film_id = film3.film_id
  997. AND rental3.rental_date >= "2016-04-01 00:00:00"
  998. AND rental3.rental_date < "2016-04-08 00:00:00";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement