-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL Joining Tables.sql
75 lines (69 loc) · 2.01 KB
/
SQL Joining Tables.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
--Sample SQL to demonstrate my ability to join tables and use common table expressions to answer questions about datasets
Use m_windler
go
-- List the name and truckid for every driver that has made a shipment, along each driver’s average shipment weight.
SELECT
DriverName,
Truck.TruckID,
AVG(ShipWeight) AS "Average ShipWeight"
FROM
Truck
JOIN
Shipment ON Truck.TruckID = Shipment.TruckID
GROUP BY
DriverName, Truck.TruckID;
-- What are the names of customers who have sent packages (shipments) to Honolulu?
SELECT
CustName
FROM
Customer
JOIN
Shipment ON Customer.CustID = Shipment.CustID
JOIN
City ON Shipment.CityID = City.CityID
WHERE
CityName = 'Honolulu';
-- Who are the customers (id, name, and cityname) either: 1) have over $2 million in annual revenue
-- and have sent shipments weighing over 900 pounds or 2) have sent a shipment to Lubbock.
-- No duplicates please, and alphabetize the city name backwards (Z to A).
SELECT DISTINCT
Customer.CustID,
CustName,
CityName
FROM
Customer
JOIN
Shipment ON Customer.CustID = Shipment.CustID
JOIN
City ON Shipment.CityID = City.CityID
WHERE
AnnualRevenue > 2000000 AND ShipWeight > 900
OR CityName = 'Lubbock'
ORDER BY
CityName DESC;
-- Who are the drivers (by name) who have delivered shipments for customers with annual
-- revenue over $2 million to cities with populations over 4 million?
SELECT
DriverName
FROM
Truck
JOIN
Shipment ON Truck.TruckID = Shipment.TruckID
JOIN
Customer ON Shipment.CustID = Customer.CustID
JOIN
City ON Shipment.CityID = City.CityID
WHERE
AnnualRevenue > 2000000
OR CityPop > 4000000;
-- To what cities have customers with revenue less than $300,000 sent packages?
SELECT DISTINCT
CityName
FROM
City
JOIN
Shipment ON City.CityID = Shipment.CityID
JOIN
Customer ON Shipment.CustID = Customer.CustID
WHERE
AnnualRevenue < 300000;