-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsql-04-join-2.sql
More file actions
64 lines (48 loc) · 1.85 KB
/
sql-04-join-2.sql
File metadata and controls
64 lines (48 loc) · 1.85 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
-- Find the employees that have created no purchases (using a set operation)
select *
from employees
except
select employees.*
from employees join purchase_orders on created_by=employees.id;
-- Find the employees that have created no purchases (not using a set operation)
select employees.*
from employees left join purchase_orders on created_by=employees.id
where created_by is null;
-- Find the shippers that have handled an order shipped to the state of NY or to state of CO
-- (using a set operation)
select shippers.*
from shippers join orders on shippers.id=orders.shipper_id
where ship_state_province = 'NY'
union
select shippers.*
from shippers join orders on shippers.id=orders.shipper_id
where ship_state_province = 'CO';
-- Find the shippers that have handled an order shipped to the state of NY or to state of CO
-- (not using a set operation)
select distinct shippers.*
from shippers join orders on shippers.id=orders.shipper_id
where ship_state_province = 'NY' or ship_state_province = 'CO';
-- Compute the total number of items (*quantity*) to be shipped to the state of NY
select sum(quantity)
from order_details join orders on order_details.order_id=orders.id
where ship_state_province = 'NY';
-- Compute the cities that have a customer and are the destination of a shipment
select city
from customers
intersect
select ship_city as city
from orders;
-- alternative solution without intersect
select distinct customers.city
from customers, orders
where customers.city = orders.ship_city;
-- Compute the cities that have a customer or are the destination of a shipment
select city
from customers
union
select ship_city as city
from orders;
-- alternative solution without union
-- does not work on sqlite, since it does not support full outer joins
select distinct customers.city
from customers outer join orders on customers.city = orders.ship_city;