-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20) Classicmodels database joins.sql
More file actions
55 lines (48 loc) · 1.94 KB
/
20) Classicmodels database joins.sql
File metadata and controls
55 lines (48 loc) · 1.94 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
use classicmodels;
show tables;
select*from employees;
-- retail store manager wants to release some offers for products that were
-- not sold even once to the customer(products & orderdetails)
select*from products;
select*from orderdetails;
select p.productCode, productName, orderNumber
from products p left join orderdetails o
on o.productCode = p.productCode
where orderNumber is null;
-- companies is planning on releasing offers to convert inactive into active customers
-- genrate such customers list (customers, orders)
select c.customerNumber, c.customerName, o.orderNumber
from customers c left join orders o
on c.customerNumber = o.customerNumber
where o.orderNumber is null;
-- genrate a report with all customers id,names, lifetime sales
-- (customers and payments)
select c.customerNumber, c.customerName, sum(amount) lifetime_sale_ampunt
from customers c join payments p
on c.customerNumber = p.customerNumber
group by c.customerNumber, c.customerName
order by lifetime_sale_ampunt desc;
-- list the products that have been ordered and cancelled
-- also count how many times the product has been cancelled
select p.productcode, p.productname, count(status) count_of_cancel
from products p join orderdetails od
on p.productcode = od.productcode
join orders o
on od.ordernumber = o.ordernumber
where status like '%Cancelled%'
group by p.productcode, p.productname
order by count(status) desc;
-- here we cant use count(status) in having as it comes under string
-- so do it in where clause
select c.customernumber, c.customername, count(*) total_counts,
p.productName, sum(quantityOrdered) sum_qty, max(creditlimit) credit_limit
from customers c join orders o
using (customernumber)
join orderdetails od
using (ordernumber)
join products p
using (productcode)
where customernumber in (187, 141, 124,119,114)
group by c.customernumber, c.customername, c.creditlimit, p.productName
having total_counts>50
order by total_counts desc;