MatthewDapcic_HW2

.sql

School

University of Miami *

*We aren’t endorsed by this school

Course

628

Subject

Computer Science

Date

Dec 6, 2023

Type

sql

Pages

7

Uploaded by CountGorillaMaster665

/* INSTRUCTIONS: 1 Write a query to answer each question. 2 You do not have to additionally write an answer or comment, the query output is sufficient. 3 If you spot a typo or a question is worded ambiguously, just solve it however you understand it (leave a comment if you're nervous about it) 4 Alias column headers appropriately - no "code" in column headers 5 Round or format all numbers appropriately - Currencies should be formatted as currencies - Percentages should be formatted as percentages, rounded to two decimals - Large numbers should include the thousands separator SUBMISSIONS: * 5 pts Rename this file with your name (e.g. DougLehmann_HW1.sql) * 5 pts Submit the correct SQL file to Blackboard * Please refer to syllabus for grading policy * YOU GET ONE SUBMISSION (So stay organized, and know what you have where / what you're submitting the first time) * Late submission -20% per day / we're all busy, just submit what you have by the deadline and move on. */ -- Answer 1-13 the following using the films database USE films; SELECT * FROM films LIMIT 5; SELECT * FROM people LIMIT 5; SELECT * FROM reviews LIMIT 5; SELECT * FROM roles LIMIT 5; -- 1. Which 5 countries have the most films represented in the database? SELECT country AS 'Country', FORMAT(COUNT(title), 0) AS 'Number of Films' FROM films GROUP BY 1 ORDER BY COUNT(title) + 0 DESC LIMIT 5 ; -- 2. Which 5 films had the highest net profit (gross - budget)? SELECT title AS 'Film', CONCAT('$',FORMAT(gross - budget, 2)) AS 'Net Profit' FROM films ORDER BY (gross - budget) DESC LIMIT 5; -- 3. Return the year and number of films per year, and sort from most recent to oldest release year. Only return years that had fewer than 25 films released. SELECT
release_year AS 'Year', COUNT(title) AS 'Number of Films' FROM films GROUP BY 1 HAVING COUNT(title) < 25 ORDER BY 1 DESC; -- 4. Return the title and imdb score for the 3 films with the highest IMDB score. SELECT f.title AS 'Title', FORMAT(r.imdb_score,2) AS 'IMDB Score' FROM films f JOIN reviews r ON f.film_id = r.film_id ORDER BY 2 DESC LIMIT 3; -- 5. Obtain the total number of facebook likes by release year, order from most to least recent SELECT f.release_year AS 'Release Year', FORMAT(SUM(r.facebook_likes),0) AS 'Number of Facebook Likes' FROM films f JOIN reviews r ON f.film_id = r.film_id GROUP BY 1 ORDER BY 1 DESC; -- 6. What is the only film with more than one million facebook likes? SOLVE THIS WITH A JOIN SELECT f.title AS 'Film', FORMAT(SUM(r.facebook_likes),0) AS 'Number of Facebook Likes' FROM films f JOIN reviews r ON f.film_id = r.film_id GROUP BY 1 HAVING SUM(r.facebook_likes) > 1000000; -- 7. What is the only film with more than one million facebook likes? SOLVE THIS USING A SUBQUERY IN A WHERE STATEMENT SELECT title FROM films WHERE film_id IN( SELECT film_id FROM reviews WHERE facebook_Likes >1000000 ); -- 8. Return a table that provides the language and average number of facebook likes per film for Spanish, French, and Portuguese films. Round the number of likes to an integer, and order the table high to low SELECT f.title AS 'Film', f.language AS 'Language', FORMAT(AVG(r.facebook_likes), 0) AS 'Average Number of Facebook Likes' FROM films f JOIN reviews r ON f.film_id = r.film_id
WHERE Language = 'Spanish' OR Language = 'French' OR Language = 'Portuguese' GROUP BY 1 ORDER BY AVG(r.facebook_likes) DESC; -- 9. Return a table that provides the language and average IMDB score. Only include languages with an average IMDB score between 7 and 8. Order the results high to low based on average IMDB score. SELECT f.language, FORMAT(AVG(r.imdb_score), 2) AS 'Average IMDB Score' FROM films f JOIN reviews r ON f.film_id = r.film_id GROUP BY f.language HAVING AVG(r.imdb_score) BETWEEN 7 AND 8 ORDER BY AVG(r.imdb_score) DESC; -- 10. Which rating receives the most Facebook likes, on average, and what is that average number of likes? Round your answer to an integer. SELECT f.rating, FORMAT(AVG(r.facebook_likes), 0) AS 'Average Facebook Likes' FROM films f JOIN reviews r ON f.film_id = r.film_id GROUP BY 1 ORDER BY AVG(r.facebook_likes) DESC LIMIT 1; /* ecom Please use the 5 tables below in the ecom database for questions 11-17 The table contain the following: order_items - and itemized breakdown of each order orders - orders and order totals pageviews - tracks each page that a visitor views products - a list of our company's products visits - tracks website visitors, with information on where they came from or how they arrived to our website */ USE ecom; SELECT * FROM order_items LIMIT 5; SELECT * FROM orders LIMIT 5; SELECT * FROM pageviews LIMIT 5; SELECT * FROM products LIMIT 5; SELECT * FROM visits LIMIT 5; -- 11. What percent of website visits come from each source? Return a table that lists the Soure, Total Visits from that source, and % of Website Traffic that represents SELECT v.source AS 'Source', FORMAT(COUNT(p.page),0) AS 'Total Visits', CONCAT(ROUND(100*COUNT(p.page)/(SELECT COUNT(page) FROM pageviews),2),'%') AS 'Percent of Website Traffic'
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help