-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLData.sql
213 lines (179 loc) · 6.02 KB
/
SQLData.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
CREATE TABLE card_holder (
id INT NOT NULL,
name VARCHAR(50),
PRIMARY KEY (id)
);
CREATE TABLE credit_card (
card VARCHAR(20) NOT NULL,
id_card_holder INT,
PRIMARY KEY (card)
);
CREATE TABLE merchant_category (
id INT NOT NULL,
name VARCHAR(15),
PRIMARY KEY (id)
);
CREATE TABLE merchant (
id INT NOT NULL,
name VARCHAR(30),
id_merchant_category INT NOT NULL,
FOREIGN KEY (id_merchant_category) REFERENCES merchant_category (id),
PRIMARY KEY (id)
);
CREATE TABLE transaction (
id INT NOT NULL,
date timestamp NOT NULL,
amount float,
card VARCHAR(20),
id_merchant INT,
FOREIGN KEY (card) REFERENCES credit_card (card),
FOREIGN KEY (id_merchant) REFERENCES merchant (id),
PRIMARY KEY (id)
);
--How can you isolate (or group) the transactions of each cardholder?
select count(*), id_card_holder,name
from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
group by id_card_holder, name;
--Consider the time period 7:00 a.m. to 9:00 a.m.
select date
from transaction
where date_part('hour', date) >= 7
and date_part('hour', date) < 9;
--What are the 100 highest transactions during this time period?
select date, amount
from transaction
where date_part('hour', date) >= 7
and date_part('hour', date) < 9
order by 2 desc
limit 100;
--Do you see any fraudulent or anomalous transactions?
--- Yes
--If you answered yes to the previous question,
--explain why you think there might be fraudulent transactions during this time frame.
--- One has way too many decimals and some are very large (without decimals)
--- when compared against the rest.
-- Count the transactions less than $2.00 per cardholder subtracted min from max date
select count(*), card, max(date) - min(date)
from transaction
where amount < 2
group by card
order by 1 desc;
--joined 3 tables to find the card holder
select count(*), credit_card.id_card_holder
from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
where amount < 2
group by credit_card.id_card_holder;
--further investigated top transaction card
select *
from transaction
where card = '376027549341849'
and amount < 2;
--If we are assuming anything less than $2.00 is fraudulent, then yes, we found the
--top fraudulent cards with counts higher than 10.
--What are the top five merchants prone to being hacked using small transactions?
select count(*), card
from transaction
where amount < 2
group by card
order by 1 desc
limit 5;
select count(*), id_merchant, name
from transaction
inner join merchant on transaction.id_merchant = merchant.id
where amount < 2
group by id_merchant, name
order by 1 desc
limit 5;
-- Top 5 merchants hacked are Wood-Ramirez, Hood-Phillips, Baker Inc,
-- Henderson and Sons, and Atkinson Ltd.
--Once you have a query that can be reused, create a view for each of the previous queries.
create view vw_cardholder_transactions as
select count(*), id_card_holder,name from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
group by id_card_holder, name;
select * from vw_cardholder_transactions
create view vw_transaction_time as
select date
from transaction
where date_part('hour', date) >= 7
and date_part('hour', date) < 9;
select * from vw_transaction_time
create view vw_100_highest_transactions as
select date, amount
from transaction
where date_part('hour', date) >= 7
and date_part('hour', date) < 9
order by 2 desc
limit 100;
select * from vw_100_highest_transactions
create view vw_smallest_transactions as
select count(*), card, max(date) - min(date)
from transaction
where amount < 2
group by card
order by 1 desc;
select * from vw_smallest_transactions
create view vw_top_5_merchant_hacked as
select count(*), id_merchant, name
from transaction
inner join merchant on transaction.id_merchant = merchant.id
where amount < 2
group by id_merchant, name
order by 1 desc
limit 5;
select * from vw_top_5_merchant_hacked
--Verify if there are any fraudulent transactions in the
-- history of two of the most important customers of the firm.
-- For privacy reasons, you only know that their cardholders' IDs are 18 and 2.
create view vw_2_important_customers as
select id_card_holder, name, amount, date
from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
where id_card_holder = '2' or id_card_holder = '18'
group by id_card_holder, name, amount, date
order by 3 desc;
-- view for customer 2
create view vw_important_customer2 as
select amount, date
from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
where id_card_holder = '2'
group by amount, date
order by 2 desc;
--view for customer 18
create view vw_important_customer18 as
select amount, date
from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
where id_card_holder = '18'
group by amount, date
order by 2 desc;
--data of daily transactions from jan to jun 2018 for card holder 25
-- use table.column to parse thru and collect the data I want
select card_holder.name, transaction.date, transaction.amount, transaction.id_merchant
from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
where id_card_holder = '25'
and date_part('month', date) >= 1
and date_part('month', date) < 7
order by 2;
--view of data of daily transactions from jan to jun 2018 for card holder 25
create view vw_cardholder25 as
select amount, date
from transaction
inner join credit_card on transaction.card = credit_card.card
inner join card_holder on credit_card.id_card_holder=card_holder.id
where id_card_holder = '25'
and date_part('month', date) >= 1
and date_part('month', date) < 7
group by amount, date
order by 2;