-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchinook.sql
More file actions
110 lines (93 loc) · 3.96 KB
/
chinook.sql
File metadata and controls
110 lines (93 loc) · 3.96 KB
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
104
105
106
107
108
/*Chinook Data*/
/*Show Customers (their full names, customer ID, and country) who are not in the US.*/
SELECT FirstName ||" "|| LastName AS FullName, CustomerID, country
FROM chinook.customers
WHERE country <> 'USA'
ORDER BY country;
/*Show only the Customers from Brazil.*/
SELECT FirstName ||" "|| LastName AS FullName, CustomerID, country
FROM chinook.customers
WHERE country = 'Brazil'
ORDER BY country;
/*Find the Invoices of customers who are from Brazil. The resulting table should show the customer's full name, Invoice ID, Date of the invoice, and billing country.*/
SELECT custo.FirstName ||" "|| custo.LastName AS FullName,
invo.InvoiceID, DATE(invo.InvoiceDate) AS InvoiceDate, invo.BillingCountry
FROM chinook.customers custo
JOIN chinook.invoices invo
ON custo.CustomerID = invo.CustomerID
WHERE country = 'Brazil'
ORDER BY country;
/*Show the Employees who are Sales Agents.*/
SELECT LastName, FirstName, Title
FROM chinook.employees
WHERE Title like '%Sales%'
/*Find a unique/distinct list of billing countries from the Invoice table.*/
SELECT DISTINCT(BillingCountry) FROM chinook.invoices
ORDER BY BillingCountry;
/*Provide a query that shows the invoices associated with each sales agent. The resulting table should include the Sales Agent's full name.*/
SELECT invo.InvoiceID, emp.LastName emp.FirstName
FROM chinook.invoices invo
JOIN chinook.customers custo
ON invo.CustomerID = custo.CustomerID
JOIN chinook.employees emp
ON custo.SupportRepID = emp.EmployeeID;
/*Show the Invoice Total, Customer name, Country, and Sales Agent name for all invoices and customers.*/
SELECT invo.Total AS InvoiceTotal,
custo.LastName ||" "|| custo.FirstName AS CustomerFullName,
custo.Country,
emp.LastName ||" "|| emp.FirstName AS employeeFullName
FROM chinook.invoices invo
JOIN chinook.customers custo
ON invo.CustomerID = custo.CustomerID
JOIN chinook.employees emp
ON custo.SupportRepID = emp.EmployeeID;
/*How many Invoices were there in 2009?*/
SELECT COUNT(*)
FROM chinook.Invoices
WHERE InvoiceDate BETWEEN '2009-01-01' AND '2009-12-31';
/*What are the total sales for 2009?*/
SELECT SUM(Total)
FROM chinook.Invoices
WHERE InvoiceDate BETWEEN '2009-01-01' AND '2009-12-31';
/*Write a query that includes the purchased track name with each invoice line ID.*/
SELECT tr.Name, invo_item.InvoiceLineId FROM chinook.tracks tr
JOIN chinook.invoice_items invo_item
ON tr.TrackId = invo_item.TrackId;
/*Write a query that includes the purchased track name AND artist name with each invoice line ID.*/
SELECT tr.Name AS Track, art.Name AS Artist, invo_item.InvoiceLineId FROM chinook.tracks tr
JOIN chinook.invoice_items invo_item
ON tr.TrackId = invo_item.TrackId
JOIN chinook.albums alb
ON tr.TrackID = alb.AlbumId
JOIN chinook.artists art
ON alb.AlbumID = art.ArtistID ;
/*Provide a query that shows all the Tracks, and include the Album name, Media type, and Genre.*/
SELECT tr.Name AS 'Track Name' , alb.Title AS 'Album Title', med.Name AS 'Media Type', gen.Name AS 'Genre'
FROM chinook.tracks tr
JOIN chinook.albums alb
ON tr.AlbumId = alb.AlbumId
JOIN chinook.media_types med
ON tr.MediaTypeId = med.MediaTypeId
JOIN chinook.genres gen
ON tr.GenreId = gen.GenreId;
/*Show the total sales made by each sales agent.*/
SELECT emp.LastName ||" "|| emp.FirstName AS employeeFullName,
ROUND(SUM(invo.Total),2) AS 'Total Sales'
FROM chinook.invoices invo
JOIN chinook.customers custo
ON invo.CustomerID = custo.CustomerID
JOIN chinook.employees emp
ON custo.SupportRepID = emp.EmployeeID
WHERE emp.Title = 'Sales Support Agent'
GROUP BY employeeFullName;
/*Which sales agent made the most dollars in sales in 2009?*/
SELECT emp.LastName ||" "|| emp.FirstName AS employeeFullName,
ROUND(SUM(invo.Total),2) AS 'Total Sales'
FROM chinook.invoices invo
JOIN chinook.customers custo
ON invo.CustomerID = custo.CustomerID
JOIN chinook.employees emp
ON custo.SupportRepID = emp.EmployeeID
WHERE emp.Title = 'Sales Support Agent'
AND invo.InvoiceDate BETWEEN '2009-01-01' AND '2009-12-31'
GROUP BY employeeFullName LIMIT 1;