-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathFinal SQL Project
103 lines (83 loc) · 2.68 KB
/
Final SQL Project
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Here are potential answers to the example questions in the final SQL project.
# Note that there are often multiple ways to write SQL to come up with the same answer.
#1.
SELECT FirstName, LastName, CustomerId, Country
FROM chinook.customers
WHERE Country != "USA";
#2.
SELECT * FROM chinook.customers
WHERE Country = "Brazil" ;
#3.
SELECT cust.FirstName, cust.LastName, inv.InvoiceId, inv.BillingCountry, inv.InvoiceDate
FROM chinook.invoices as inv
LEFT JOIN chinook.customers as cust
on inv.CustomerId = cust.CustomerId
WHERE inv.BillingCountry = "Brazil";
#4.
SELECT * FROM chinook.Employees
WHERE Title = "Sales Support Agent" ;
#5.
SELECT DISTINCT BillingCountry FROM chinook.Invoices ;
#6.
SELECT emp.LastName, emp.Firstname, inv.InvoiceId
FROM chinook.Employees emp
JOIN chinook.Customers cust ON cust.SupportRepId = emp.EmployeeId
JOIN chinook.Invoices Inv ON Inv.CustomerId = cust.CustomerId;
#7.
SELECT emp.LastName, emp.Firstname, cust.FirstName, cust.LastName, cust.Country, inv.total
FROM chinook.employees emp
JOIN chinook.Customers cust ON cust.SupportRepId = emp.EmployeeId
JOIN chinook.Invoices Inv ON Inv.CustomerId = cust.CustomerId;
#8.
SELECT COUNT(*)
FROM chinook.Invoices
WHERE InvoiceDate BETWEEN '2009-01-01' AND '2009-12-31';
#9.
SELECT SUM(Total)
FROM chinook.Invoices
WHERE InvoiceDate BETWEEN '2009-01-01' AND '2009-12-31';
#10.
SELECT t.Name, i.InvoiceLineId
FROM chinook.Invoice_items i
JOIN chinook.Tracks t
ON i.TrackId = t.TrackId;
#11.
SELECT ar.name as Artist, t.Name as Track, i.InvoiceLineId
FROM chinook.Invoice_items i
LEFT JOIN chinook.tracks t
ON i.TrackID=t.TrackID
INNER JOIN chinook.albums a
ON a.AlbumID=t.AlbumID
LEFT JOIN chinook.artists ar
ON ar.ArtistID=a.ArtistID;
#12.
SELECT t.Name AS 'Track Name', a.Title AS 'Album Title', m.Name AS 'Media Type', g.Name AS 'Genre'
FROM chinook.tracks t
JOIN chinook.Albums a
on a.AlbumId = t.AlbumId
JOIN chinook.Media_Types m
on m.MediaTypeId = t.MediaTypeId
JOIN chinook.Genres g
on g.GenreId = t.GenreId;
#13.
SELECT emp.FirstName, emp.LastName,
ROUND(SUM(Inv.Total), 2) as 'Total Sales'
FROM chinook.Employees emp
JOIN chinook.Customers cust
ON cust.SupportRepId = emp.EmployeeId
JOIN chinook.Invoices Inv
ON Inv.CustomerId = cust.CustomerId
WHERE emp.Title = 'Sales Support Agent'
GROUP BY emp.FirstName;
#14.
SELECT emp.FirstName, emp.LastName,
ROUND(SUM(Inv.Total), 2) as 'Total Sales'
FROM chinook.Employees emp
JOIN chinook.Customers cust
ON cust.SupportRepId = emp.EmployeeId
JOIN chinook.Invoices Inv
ON Inv.CustomerId = cust.CustomerId
WHERE emp.Title = 'Sales Support Agent'
AND Inv.InvoiceDate LIKE '2009%'
GROUP BY emp.FirstName
ORDER BY (round(sum(Inv.Total), 2)) DESC LIMIT 1;