-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab6InterestingQueries.sql
More file actions
77 lines (65 loc) · 1.81 KB
/
Copy pathLab6InterestingQueries.sql
File metadata and controls
77 lines (65 loc) · 1.81 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
--1
SELECT name, city
FROM customers
WHERE city in (
SELECT customers.city
from products
GROUP BY city
ORDER BY count(pid) DESC
LIMIT 1);
--2
SELECt name
from products
where priceUSD > (
select AVG(priceUSD)
FROM products
)
order by name desc
--3
Select customers.name, orders.pid, sum(orders.dollars) as totalOrderSum
from orders inner join customers on orders.cid = customers.cid
group by customers.name, orders.pid
Order BY totalOrderSum desc;
--4
Select customers.name, COALESCE(sum(orders.dollars), 0) as totalOfAllOrders
from orders full outer join customers on orders.cid = customers.cid
group by customers.name
order by customers.name asc
--5
Select customers.name, products.name, agents.name
from orders inner join customers on orders.cid = customers.cid
inner join agents on orders.aid = agents.aid
inner join products on orders.pid = products.pid
where orders.cid in
(
Select orders.cid
from orders inner join customers on orders.cid = customers.cid
inner join agents on orders.aid = agents.aid
where orders.aid =
(
Select aid
from agents
where city = 'Tokyo'
)
);
--6
Select *
From (Select o.*, o.qty*p.priceusd*(1-(discount/100)) as truedollars
from orders o
inner join products p on o.pid = p.pid
inner join customers c on o.cid = c.cid) as tmptable
Where dollars != truedollars
-- 7
-- When using outer joins, a left outer join specifies that you want all the rows
-- from the left table and any matching rows from the right table. a right outer
-- join specifies the opposite. For instance, the following query:
select *
from orders
right outer join customers
on orders.cid=customers.cid
--contains c005 while
select *
from orders
LEFT outer join customers
on orders.cid=customers.cid
--does not