-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject sql
More file actions
87 lines (73 loc) · 3.11 KB
/
project sql
File metadata and controls
87 lines (73 loc) · 3.11 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
-- Final SQL Project: Example Queries on the Chinook Database
-- 1. Customers not located in the USA
SELECT FirstName, LastName, CustomerId, Country
FROM chinook.customers
WHERE Country != 'USA';
-- 2. All customers from Brazil
SELECT * FROM chinook.customers
WHERE Country = 'Brazil';
-- 3. Brazilian customers with invoice details
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. List of Sales Support Agents
SELECT * FROM chinook.Employees
WHERE Title = 'Sales Support Agent';
-- 5. All unique billing countries from invoices
SELECT DISTINCT BillingCountry
FROM chinook.Invoices;
-- 6. Employee names with their associated invoice IDs
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. Employee and customer details with invoice totals
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. Number of invoices from 2009
SELECT COUNT(*)
FROM chinook.Invoices
WHERE InvoiceDate BETWEEN '2009-01-01' AND '2009-12-31';
-- 9. Total revenue from invoices in 2009
SELECT SUM(Total)
FROM chinook.Invoices
WHERE InvoiceDate BETWEEN '2009-01-01' AND '2009-12-31';
-- 10. Track names with their invoice line IDs
SELECT t.Name, i.InvoiceLineId
FROM chinook.Invoice_items i
JOIN chinook.Tracks t ON i.TrackId = t.TrackId;
-- 11. Artist, track name, and invoice line ID details
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. Track details including album, media type, and genre
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 t.AlbumId = a.AlbumId
JOIN chinook.Media_Types m ON t.MediaTypeId = m.MediaTypeId
JOIN chinook.Genres g ON t.GenreId = g.GenreId;
-- 13. Total sales per Sales Support Agent
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. Top-selling Sales Support Agent in 2009
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;