-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL_data_science_quiz_3.sql
82 lines (69 loc) · 2.38 KB
/
SQL_data_science_quiz_3.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
--Q1) Using a subquery, find the names of all the tracks for the
-- album "Californication".
SELECT Name,
AlbumID
FROM Tracks
WHERE AlbumId IN (SELECT AlbumId
FROM Albums
WHERE Title = "Californication");
------------------------------------------------------------------------------------
-- Q2) Find the total number of invoices for each customer along
-- with the customer's full name, city and email.
SELECT FirstName,
LastName,
City,
Email,
COUNT(I.CustomerId) AS Invoices
FROM Customers C INNER JOIN Invoices I
ON C.CustomerId = I.CustomerId
GROUP BY C.CustomerId
------------------------------------------------------------------------------------
-- Q3) Retrieve the track name, album, artist, and trackID for
-- all the albums.
SELECT Tracks.Name,
A.Name AS Artist,
Albums.Title AS Album,
Tracks.TrackId
FROM ((Tracks INNER JOIN Albums
ON Tracks.AlbumId = Albums.AlbumId)
INNER JOIN Artists A
ON A.ArtistId = Albums.ArtistId);
------------------------------------------------------------------------------------
-- Q4) Retrieve a list with the managers last name, and the last
-- name of the employees who report to him or her.
SELECT M.LastName AS Manager,
E.LastName AS Employee
FROM Employees E INNER JOIN Employees M
ON E.ReportsTo = M.EmployeeID
------------------------------------------------------------------------------------
-- Q5) Find the name and ID of the artists who do not have albums.
SELECT Name AS Artist,
Artists.ArtistId,
Albums.Title AS Album
FROM Artists
LEFT JOIN Albums
ON Artists.ArtistId = Albums.ArtistId
WHERE Album IS NULL
------------------------------------------------------------------------------------
-- Q6) Use a UNION to create a list of all the employee's &
-- customer's first names and last names ordered by the last
-- name in descending order.
SELECT FirstName,
LastName
FROM Employees
UNION
SELECT FirstName,
LastName
FROM Customers
ORDER BY LastName DESC
------------------------------------------------------------------------------------
-- Q7) See if there are any customers who have a different city
-- listed in their billing city versus their customer city.
SELECT C.FirstName,
C.LastName,
C.City AS CustomerCity,
I.BillingCity
FROM Customers C
INNER JOIN Invoices I
ON C.CustomerId = I.CustomerId
WHERE CustomerCity != BillingCity