Repository navigation
Expand file tree
/
Copy pathcodes 4.sql
More file actions
101 lines (77 loc) · 2.49 KB
/
Copy pathcodes 4.sql
File metadata and controls
101 lines (77 loc) · 2.49 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
CREATE TABLE department(
department_id int not null PRIMARY KEY,
department varchar(50) not null)
CREATE TABLE aisles(
aisle_id int not null PRIMARY KEY,
aisle varchar(50) not null,
department_id int not null,
FOREIGN KEY(department_id) REFERENCES department(department_id))
CREATE TABLE product(
product_id int not null PRIMARY KEY,
product_name varchar(225) not null,
department_id int not null,
aisle_id int not null,
FOREIGN KEY(department_id) REFERENCES department(department_id),
FOREIGN KEY(aisle_id) REFERENCES aisles(aisle_id))
CREATE TABLE orders(
order_id int not null PRIMARY KEY,
product_id int not null,
user_id int not null,
order_dow varchar(25) not null,
order_hour_of_day int not null,
days_since_prior_order int not null,
FOREIGN KEY(product_id) REFERENCES product(product_id))
drop table product cascade
select * from orders
select * from department
select * from product
select * from aisles
----1. On which day/s of the week are condoms mostly sold
With WeekDays AS (
SELECT p.product_id, p.product_name, order_dow
FROM orders o
JOIN product p on o.product_id = p.product_id
WHERE p.product_name like '%Condoms%'
)
SELECT order_dow, count(order_dow) as Totalsales
FROM WeekDays
GROUP BY order_dow
ORDER BY Totalsales DESC
--2. At what time of the day is condoms mostly sold
With Time as (
SELECT p.product_id, p.product_name,o.order_hour_of_day
FROM orders o
JOIN product p on o.product_id = p.product_id
WHERE p.product_name LIKE '%Condoms%'
)
SELECT order_hour_of_day, COUNT(order_hour_of_day) as Hours
FROM Time
GROUP BY order_hour_of_day
ORDER BY Hours DESC
--3.What type of condom do the customers prefer?
With preferred AS(
SELECT o.order_id, product_name
FROM orders o
JOIN product p on o.product_id = p.product_id
WHERE p.product_name LIKE '%Condoms%'
)
SELECT product_name ,count(product_name) as Totalnum
FROM preferred
GROUP BY product_name
ORDER BY Totalnum desc
--4. Which aisle contains most of the organic products?
With aislecount AS(
SELECT a.aisle, product_name
FROM aisles a
JOIN product p on a.aisle_id = p.aisle_id
WHERE p.product_name LIKE '%Organic%'
)
SELECT aisle, COUNT(product_name) AS Totalnum
FROM aislecount
GROUP BY aisle
ORDER BY Totalnum DESC
---5. Which aisle/s can i find all the non-alcoholic drinks?
SELECT a.aisle, d.department
FROM aisles a
JOIN department d on a.department_id = d.department_id
WHERE d.department in ('beverages');