-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrenden_Bishop_SQL_3.sql
164 lines (126 loc) · 3.15 KB
/
Brenden_Bishop_SQL_3.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---Brenden Bishop---
---SQL HW 3---
---1---
select distinct city
from agents
where aid in (select aid from orders
where cid = 'c002');
---2---
select distinct city
from agents
inner join orders
on agents.aid = orders.aid
and orders.cid = 'c002';
---3---
select distinct pid
from orders where aid in (select aid from orders
where cid in (select cid from customers
where city = 'Kyoto'));
---4---
select distinct o2.pid
from orders o1
join orders o2
on o2.aid = o1.aid
inner join customers
on customers.cid = o1.cid
and customers.city = 'Kyoto'
order by o2.pid asc;
---5---
select distinct name
from customers
where cid not in (select cid from orders);
---6---
select distinct name
from customers
left outer join orders
on customers.cid = orders.cid
where orders.cid is null;
---7---
select distinct customers.name, agents.name
from customers
join orders
on customers.cid = orders.cid
join agents
on agents.aid = orders.aid
and customers.city = agents.city
order by customers.name asc;
---8---
select distinct customers.name, agents.name, customers.city
from customers
join agents
on customers.city = agents.city
order by customers.city asc;
---9---
select distinct customers.name, customers.city
from customers
where customers.city in(select city from (select p2.city, count(*) as city_count
from products p2
group by p2.city) as city_count
order by city_count asc
limit 1);
---10---
select distinct customers.name, customers.city
from customers
where customers.city in(select city from (select p2.city, count(*) as city_count
from products p2
group by p2.city) as city_count
order by city_count desc
limit 1);
---11---
drop view if exists cityCount;
create view cityCount as (select p2.city, count(*) as city_count
from products p2
group by p2.city
order by city_count desc
);
select distinct customers.name, customers.city
from customers
where customers.city in(select city from cityCount
where city_count = (select max(city_count)
from cityCount));
---12---
select name from products
where priceUSD > (select avg(priceUSD) from products)
order by name asc;
---13---
select customers.name, orders.pid, orders.dollars
from customers
inner join
orders
on orders.cid = customers.cid
order by dollars desc;
---14---
select customers.name, coalesce(orders.qty,'0') as "Order Quantity"
from customers
left outer join
orders
on orders.cid = customers.cid
order by customers.name asc;
---15---
select distinct customers.name, products.name, agents.name
from orders
inner join customers
on customers.cid = orders.cid
join products
on products.pid = orders.pid
join orders o2
on o2.aid = orders.aid
inner join agents
on agents.aid = orders.aid
and agents.city = 'New York'
order by customers.name asc;
---16---
select orders.ordno, orders.dollars as "Inaccurate Total Costs"
from orders
where orders.dollars not in (
select((products.priceUSD*orders.qty) - ((customers.discount*.01)*(products.priceUSD*orders.qty)))
as totalCost
from customers
inner join
orders
on orders.cid = customers.cid
inner join products
on products.pid = orders.pid
order by orders.dollars);
---17---
update orders set dollars = '550' where ordno = 1011;