Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

/*

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: InvoicecustomerID

Question 1.3 (3 points):


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

(Replace this with your answer.)

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 plalylisttrack is 1:N
cardinality of Playlisttrack to track is N:1

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
Primary keys are not nullable, The primary key is: CustomerId
*/

/* 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 *
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)
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.albumid,AL.artistid, AL.title, 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)
*/
/* 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)
*/

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

/*
Question 2.9 a:
Write an SQL query to find all customers whose first name starts with 'A'.
(TABLE: customer)
*/

/*
Question 2.9 b: What is the total query cost for the query you just wrote?

*/

/*
Question 2.10:
Write an SQL query to find genres that have more than 100 tracks.
(TABLES: track, genre)
*/

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

/*
Question 2.12:
Write an SQL query to retrieve the 10 most recent invoices.
(TABLE: invoice)
*/

/*
Question 2.13:
Write an SQL query to find all customers who do not have a company listed.
(TABLE: customer)
*/

/*
Question 2.14 a:
a) Write an SQL query to find the total number of tracks in each genre.
(TABLES: track, genre)
*/

/*
Question 2.14 b: What is the total query cost for the query you just wrote?

*/

/*
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)
(TABLES: track, album, artist)
*/

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

/*
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
);
/*
Replace this with your explanation
*/

/*
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 e2 ON e1.ReportsTo = e2.EmployeeId;
/*
Replace this with your explanation
*/

/*
Question 3.3 (4 points): Explain what the following SQL code does
*/
SELECT FirstName, LastName,
CASE
WHEN EmployeeId IN (SELECT DISTINCT ReportsTo FROM Employee) THEN
'Manager'
ELSE 'Staff'
END AS Role
FROM Employee;
/*
Replace this with your explanation
*/

/* END OF TEST */

You might also like