forked from trentparkinson/SQL-for-Data-Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_data_science_quiz_2.sql
76 lines (51 loc) · 2.21 KB
/
SQL_data_science_quiz_2.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
--Q1) Find all the tracks that have a length of 5,000,000 milliseconds or more.
SELECT COUNT(TrackId)
FROM TRACKS
WHERE Milliseconds >= 5000000
------------------------------------------------------------------------------------
--Q2) Find all the invoices whose total is between $5 and $15 dollars.
SELECT InvoiceID,Total
FROM Invoices
WHERE Total > 5 AND Total < 15
------------------------------------------------------------------------------------
--Q3) Find all the customers from the following States: RJ, DF, AB, BC, CA, WA, NY.
SELECT FirstName, LastName, Company, State
FROM Customers
WHERE State IN ('RJ','DF','AB','BC','CA','WA','NY')
------------------------------------------------------------------------------------
--Q4) Find all the invoices for customer 56 and 58 where the total was between
-- $1.00 and $5.00.
SELECT CustomerId, InvoiceId, Total, InvoiceDate
FROM Invoices
WHERE CustomerID IN (56,58) AND
Total BETWEEN 1 AND 5
------------------------------------------------------------------------------------
--Q5) Find all the tracks whose name starts with 'All'.
SELECT TrackId, Name
FROM Tracks
WHERE Name LIKE 'All%'
------------------------------------------------------------------------------------
--Q6) Find all the customer emails that start with "J" and are from gmail.com.
SELECT CustomerId, Email
FROM Customers
WHERE Email LIKE "J%@gmail.com"
------------------------------------------------------------------------------------
--Q7) Find all the invoices from Brasilia, Edmonton, and Vancouver and sort in
-- descending order by invoice ID.
SELECT InvoiceId, BillingCity, Total
FROM Invoices
WHERE BillingCity IN ('Brasilia','Edmonton','Vancouver')
ORDER BY InvoiceId DESC
------------------------------------------------------------------------------------
--Q8) Show the number of orders placed by each customer and sort the result by
-- the number of orders in descending order.
SELECT CustomerId, COUNT(*) AS Orders
FROM Invoices
GROUP BY CustomerId
ORDER BY Orders DESC
------------------------------------------------------------------------------------
--Q9) Find the albums with 12 or more tracks.
SELECT AlbumId, Count(*) AS Ntracks
FROM Tracks
GROUP BY AlbumId
HAVING COUNT (*) >= 12