-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal Project.sql
More file actions
197 lines (179 loc) · 3.86 KB
/
Final Project.sql
File metadata and controls
197 lines (179 loc) · 3.86 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
1) How many transactions took place between the years 2011 and 2012?
2) How much money did WSDA make in that period?
*/
--1
SELECT
count(date(InvoiceDate)) AS [TOTAL TRANSACTIONS BETWEEN 2011 AND 2012]
FROM
Invoice
WHERE
InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31"
--2
SELECT
sum(total) AS [TOTAL SALES OF WSDA BETWEEN 2011 AND 2012]
FROM
Invoice
WHERE
InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31"
/*
1) Get a list of customers who made purchases between 2011 and 2012
2) Get a list of customers,sales reps,total transaction amounts for each customer between 2011 and 2012
3) How many transactions are above the average transaction amount during the same time
4) What was the average transaction amount for each year that WSDA has been in Business?
*/
--1
SELECT
c.FirstName,
c.LastName,
date(i.InvoiceDate),
i.total,
i.InvoiceId
FROM
Invoice AS i
INNER JOIN
Customer AS c
ON
i.CustomerId = c.CustomerId
WHERE
i.InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31"
ORDER BY
i.total DESC
--2
SELECT
c.FirstName,
c.LastName,
e.FirstName,
e.LastName,
e.EmployeeId,
date(i.InvoiceDate),
SUM(i.total),
i.InvoiceId
FROM
Invoice AS i
INNER JOIN
Customer AS c
ON
i.CustomerId = c.CustomerId
INNER JOIN
Employee AS e
ON
c.SupportRepId = e.EmployeeId
WHERE
i.InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31"
GROUP BY
i.CustomerId
--3
--To Get total average transaction of company within 2011-2012
SELECT
round(avg(total),2)
FROM
Invoice
WHERE
InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31"
--To get transactions that are above the average total transaction during 2011-2012
SELECT
count(total)
FROM
Invoice
WHERE
total >
(
SELECT
round(avg(total),2)
FROM
Invoice
WHERE InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31"
)
AND InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31"
--4
SELECT
avg(total),
strftime("%Y",InvoiceDate) AS YEAR
FROM
Invoice
GROUP BY
YEAR
/*
1) Get a list of Employees who exceeded the average transaction amount from sales they generated during 2011-2012
2) Create a Commission Payout Column that displays each employee's commission based on 15% of the sales transaction amount, irrespective of whether that sale is greater than or less than 11.66
3) Which Employee made the highest commission?
4) List the customers that were served by that employee
5) Which customer made the highest purchase?
6) Look at that customer's record,is there anything suspicious?
7) Who is the primary person of interest?
*/
--1
SELECT
e.FirstName,
e.LastName,
sum(i.total) AS TOT
FROM
Invoice AS i
INNER JOIN
Customer AS c
ON
i.CustomerId = c.CustomerId
INNER JOIN
Employee AS e
ON
c.SupportRepId = e.EmployeeId
WHERE
(i.InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31") AND i.total > 11.66
GROUP BY
e.EmployeeId
--2
SELECT
e.FirstName,
e.LastName,
((sum(i.total))*0.15) AS COMMISSION
FROM
Invoice AS i
INNER JOIN
Customer AS c
ON
i.CustomerId = c.CustomerId
INNER JOIN
Employee AS e
ON
c.SupportRepId = e.EmployeeId
WHERE
(i.InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31")
GROUP BY
e.EmployeeId
--3
--JANE PEACOCK = 199.77
--4
SELECT
c.FirstName,
c.LastName,
sum(i.total) AS TOT,
((sum(i.total))*0.15) AS COMMISSION
FROM
Invoice AS i
INNER JOIN
Customer AS c
ON
i.CustomerId = c.CustomerId
INNER JOIN
Employee AS e
ON
c.SupportRepId = e.EmployeeId
WHERE
(i.InvoiceDate BETWEEN "2011-01-01" AND "2012-12-31") AND e.FirstName = "Jane"
GROUP BY
c.CustomerId
ORDER BY
TOT desc
--5
--John Doeein = 1000.86
--6
SELECT
*
FROM
Customer
WHERE
FirstName = "John" AND LastName = "Doeein"
--Yes, this record looks suspicious as it has NULL components
--7
--The primary suspect is Jane Peacock because John Doeein is her customer.