0% found this document useful (0 votes)
27 views7 pages

Wk2 DY2

Uploaded by

eleojo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views7 pages

Wk2 DY2

Uploaded by

eleojo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Sure, I'll help you complete the lab test.

Here's the revised version with the remaining


answers filled in:

```sql
/*

BSMM-8730 Summer 2024 Lab Test 1


Version 2024.06.26.2
NAME: ONOJA FAVOUR
STUDENT ID: 110134608
IMPORTANT: Save your work often in case something goes wrong.

This test consists of loading a new database followed by three parts:

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:

Question 1.1 (1 point):


How many tables are there in the Chinook database?

11

Question 1.2 (3 points):


Identify the primary key and foreign key(s)
in the Invoice table from the Chinook ER diagram.

Primary key: Invoice ID


Foreign key: CustomerID

Question 1.3 (3 points):


Are there any composite keys in the Chinook database? If yes, provide an example.
No, there are no composite keys in the Chinook database.

Question 1.4 (4 points):


Describe the relationship between the Playlist and Track tables. Indicate the
cardinality and explain what that means, referring specifically to the tables in the
Chinook database.

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.

Question 1.5 (4 points):


List all the attributes of the Customer table and identify which ones are mandatory (not
nullable).

CustomerId, FirstName, LastName, Company, Address, City, State, Country, PostalCode,


Phone, Fax, Email, SupportRepId
Mandatory (not nullable) attributes: CustomerId, FirstName, LastName, Address, City, State,
Country, PostalCode, Email

*/

/* IMPORTANT: Save your work often in case something goes wrong. */

/*
Part 2 (80 points): Write SQL queries AND provide answers for each of the following
questions.

Questions in this part are worth up to 5 (five) points each.


*/

/*
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;

/* IMPORTANT: Save your work often in case something goes wrong. */

/*
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;

/* IMPORTANT: Save your work often in case something goes wrong. */


/*
Question 2.11:
Write an SQL query to find all tracks where the name contains the word 'Love'.
(TABLE: track)
*/
SELECT *
FROM track
WHERE name LIKE '%Love%';

/*
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.

As an example: explain what the following code does:

SELECT Genre.Name AS GenreName, MediaType.Name AS MediaTypeName


FROM Genre
CROSS JOIN MediaType;

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

You might also like