Wk2 DY2
Wk2 DY2
```sql
/*
Part 1 (10 points) asks you to generate and interpret an ER diagram of the new database.
Part 2 (80 points) asks you to write SQL queries to answer questions about the data.
Part 3 (10 points) asks you to look at new SQL code and attempt to understand what it does.
*/
/*
Part 0 (0 points): Create the "chinook_autoincrement" database (hereafter referred to as
the "Chinook" database by running the
`Chinook_MySql_AutoIncrementPKs.sql` script from the resources in Brightspace.
Once you have done that, run the following line:
*/
USE chinook_autoincrement;
/*
Part 1 (15 points): Generate an ER diagram of the Chinook database and answer the
following questions:
11
The playlist and track table are connected by the PlaylistTrack table.
Cardinality of Playlist to PlaylistTrack is 1:N, meaning one playlist can have many tracks.
Cardinality of PlaylistTrack to Track is N:1, meaning each entry in PlaylistTrack refers to one
track.
*/
/*
Part 2 (80 points): Write SQL queries AND provide answers for each of the following
questions.
/*
Question 2.1:
Write an SQL query to retrieve the first name and last name of all customers.
(TABLE: customer)
*/
SELECT firstname, lastname
FROM customer;
/*
Question 2.2:
Write an SQL query to retrieve the names of all tracks with a UnitPrice greater than 0.99.
(TABLE: track)
*/
SELECT name
FROM track
WHERE unitprice > 0.99;
/*
Question 2.3:
Write an SQL query to find the average unit price of all tracks.
(TABLE: track)
*/
SELECT AVG(unitprice) AS avg_price
FROM track;
/*
Question 2.4:
Write an SQL query to retrieve the names of all albums along with the names of their artists.
(TABLES: album, artist)
*/
SELECT AL.title AS AlbumTitle, AR.Name AS ArtistName
FROM album AS AL
JOIN artist AS AR
ON AL.artistid = AR.artistid;
/*
Question 2.5:
Write an SQL query to find the total sales (sum of Total) for each country in the Invoice table.
(TABLE: invoice)
*/
SELECT BillingCountry, SUM(Total) AS TotalSales
FROM invoice
GROUP BY BillingCountry;
/*
Question 2.6:
Write an SQL query to find the top 5 most expensive tracks.
(TABLE: track)
*/
SELECT *
FROM track
ORDER BY UnitPrice DESC
LIMIT 5;
/*
Question 2.7:
Write an SQL query to find the distinct billing cities in the Invoice table.
(TABLE: invoice)
*/
SELECT DISTINCT BillingCity
FROM invoice;
/*
Question 2.8:
Write an SQL query to find all tracks that belong to the genres 'Rock' and 'Metal'.
You should use the IN operator in your SQL.
(TABLES: track, genre)
*/
SELECT T.name AS TrackName
FROM track AS T
JOIN genre AS G
ON T.genreid = G.genreid
WHERE G.name IN ('Rock', 'Metal');
/*
Question 2.9 a:
Write an SQL query to find all customers whose first name starts with 'A'.
(TABLE: customer)
*/
SELECT *
FROM customer
WHERE firstname LIKE 'A%';
/*
Question 2.9 b: What is the total query cost for the query you just wrote?
-- The total query cost would typically be provided by the database system's execution plan.
Since this is an example, I'll explain how you can find it in MySQL.
-- In MySQL, you can use EXPLAIN to find the query cost. Run EXPLAIN before your query:
EXPLAIN SELECT *
FROM customer
WHERE firstname LIKE 'A%';
-- The query cost will be shown in the output, under the 'rows' column.
*/
/*
Question 2.10:
Write an SQL query to find genres that have more than 100 tracks.
(TABLES: track, genre)
*/
SELECT G.name AS GenreName, COUNT(T.trackid) AS TrackCount
FROM track AS T
JOIN genre AS G
ON T.genreid = G.genreid
GROUP BY G.name
HAVING COUNT(T.trackid) > 100;
/*
Question 2.12:
Write an SQL query to retrieve the 10 most recent invoices.
(TABLE: invoice)
*/
SELECT *
FROM invoice
ORDER BY invoicedate DESC
LIMIT 10;
/*
Question 2.13:
Write an SQL query to find all customers who do not have a company listed.
(TABLE: customer)
*/
SELECT *
FROM customer
WHERE company IS NULL;
/*
Question 2.14 a:
a) Write an SQL query to find the total number of tracks in each genre.
(TABLES: track, genre)
*/
SELECT G.name AS GenreName, COUNT(T.trackid) AS TrackCount
FROM track AS T
JOIN genre AS G
ON T.genreid = G.genreid
GROUP BY G.name;
/*
Question 2.14 b: What is the total query cost for the query you just wrote?
-- As with question 2.9b, the total query cost can be found using EXPLAIN in MySQL.
EXPLAIN SELECT G.name AS GenreName, COUNT(T.trackid) AS TrackCount
FROM track AS T
JOIN genre AS G
ON T.genreid = G.genreid
GROUP BY G.name;
-- The query cost will be shown in the output.
*/
/*
Question 2.15:
Write an SQL query to retrieve the names of all tracks along with their album titles and artist
names.
(TABLES: track, album, artist)
*/
SELECT T.name AS TrackName, AL.title AS AlbumTitle, AR.name AS ArtistName
FROM track AS T
JOIN album AS AL
ON T.albumid = AL.albumid
JOIN artist AS AR
ON AL.artistid = AR.artistid;
/*
Question 2.16:
Write an SQL query to find the number of tracks for each combination of
genre and media type. You should use the names of the genre and media type in your
output.
No table information given -- you will need to figure this out.
*/
SELECT G.name AS GenreName, M.name AS MediaTypeName, COUNT(T.trackid) AS
TrackCount
FROM track AS T
JOIN genre AS G
ON T.genreid = G.genreid
JOIN mediatype AS M
ON T.mediatypeid = M.mediatypeid
GROUP BY G.name, M.name;
/*
Part 3 (10 points): Explanation of SQL code
Explain what each of the following SQL statements does. Your description should
state what the output is, and how it was generated. You can run the code to see its output.
Answer: The above code uses a CROSS JOIN to show all possible combinations of
genre name and media type name, regardless of whether there are any examples of
those combinations.
*/
/*
Question 3.1 (3 points): Explain what the following SQL code does
*/
SELECT Name
FROM Track
WHERE Milliseconds > (
SELECT AVG(Milliseconds)
FROM Track
);
/*
This query selects the names of tracks whose duration (in milliseconds) is greater than the
average duration of all tracks. It first calculates the average duration using a subquery and
then compares each track's duration to this average.
*/
/*
Question 3.2 (3 points): Explain what the following SQL code does
*/
SELECT e1.FirstName, e1.LastName, e2.FirstName AS ManagerFirstName, e2.LastName
AS ManagerLastName
FROM Employee e1
JOIN Employee e