-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQLQuery.sql
More file actions
206 lines (154 loc) · 5.59 KB
/
SQLQuery.sql
File metadata and controls
206 lines (154 loc) · 5.59 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
198
199
200
201
202
203
204
205
206
select * from BankLoan
-- Total Loan Applications
select count(id) as total_loan_applications
from BankLoan
-- Month to date total loan applications, where month is the latest (december)
-- The query have a fucntion day (issue date) although it should be month. But because of the data type error we have to consider date represnts month in this query
select count(id) as MTD_total_loan_applications
from BankLoan
where day(issue_date) = 12 and year(issue_date) = 2021
-- Previous MTD total applications, previous month is november, day(issue_date) represnts month(issue_date) in all of this query
select count(id) as PMTD_total_loan_applications
from BankLoan
where day(issue_date) = 11 and year(issue_date) = 2021
-- Total funded amount (total loan amount)
select sum(loan_amount) AS Total_funded_amount
from BankLoan
select sum(loan_amount) AS MTD_Total_funded_amount
from BankLoan
where day(issue_date) = 12 and year(issue_date) = 2021
select sum(loan_amount) AS PMTD_Total_funded_amount
from BankLoan
where day(issue_date) = 11 and year(issue_date) = 2021
-- Total Recived Amount (total payment amount)
select sum(total_payment) AS Total_recived_amount
from BankLoan
select sum(total_payment) AS MTD_Total_recived_amount
from BankLoan
where day(issue_date) = 12 and year(issue_date) = 2021
select sum(total_payment) AS PMTD_Total_recived_amount
from BankLoan
where day(issue_date) = 11 and year(issue_date) = 2021
-- Average internest rate
select round(avg(int_rate)*100, 4) AS average_interest_rate
from BankLoan
select round(avg(int_rate)*100, 4) AS MTD_average_interest_rate
from BankLoan
where day(issue_date) = 12 and year(issue_date) = 2021
select round(avg(int_rate)*100, 4) AS PMTD_average_interest_rate
from BankLoan
where day(issue_date) = 11 and year(issue_date) = 2021
-- Average Debt to income
select round(avg(dti)*100, 4) AS average_dti_rate
from BankLoan
select round(avg(dti)*100, 4) AS MTD_average_dti_rate
from BankLoan
where day(issue_date) = 12 and year(issue_date) = 2021
select round(avg(dti)*100, 4) AS PMTD_average_dti_rate
from BankLoan
where day(issue_date) = 11 and year(issue_date) = 2021
-- Good vs Bad Loan
select distinct loan_status
from BankLoan
-- Good Loan
select (count( case when loan_status = 'Fully Paid' or loan_status = 'Current' then id end)*100.0) / count (id) as good_loan_percentage
from BankLoan
select count(id) as total_good_loan_application
from BankLoan
where loan_status = 'Fully Paid' or loan_status = 'Current'
select sum(loan_amount) as total_goodloan_funded
from BankLoan
where loan_status = 'Fully Paid' or loan_status = 'Current'
select sum(total_payment) as total_goodloan_recived
from BankLoan
where loan_status = 'Fully Paid' or loan_status = 'Current'
-- Bad loan
select (count( case when loan_status = 'Charged Off' then id end)*100.0) / count (id) as bad_loan_percentage
from BankLoan
select count(id) as total_bad_loan_application
from BankLoan
where loan_status = 'Charged Off'
select sum(loan_amount) as total_badloan_funded
from BankLoan
where loan_status = 'Charged Off'
select sum(total_payment) as total_badloan_recived
from BankLoan
where loan_status = 'Charged Off'
-- Loan Status Grid Lines
select
loan_status,
count(id) as Loan_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded,
round(avg(int_rate)*100 , 2) as Average_Interest_Rate,
round(avg(dti)*100 , 2) as Average_DTI
from BankLoan
Group by loan_status
select
loan_status,
count(id) as Loan_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded,
round(avg(int_rate)*100 , 2) as Average_Interest_Rate,
round(avg(dti)*100 , 2) as Average_DTI
from BankLoan
where day(issue_date)=12
Group by loan_status
-- Monthly Trend by Issue date (There is a whole issue in issue_date column, so this query might not be correct. Have to contact with the data engineer related to this dataset or source of information)
select
datename(day, issue_date) as Month_Serial,
count(id) as Total_Application_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded
from BankLoan
group by datename(day, issue_date)
order by datename(day, issue_date)
-- Regional Analysis by State
select
address_state as State,
count(id) as Total_Application_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded
from BankLoan
group by address_state
order by count(id) desc
-- Loan term analysis
select
term as Loan_Term,
count(id) as Total_Application_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded
from BankLoan
group by term
order by term
-- Employee Length Analysis
select
emp_length as Employee_Lenght,
count(id) as Total_Application_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded
from BankLoan
group by emp_length
order by count(id) desc
-- Loan Purpose Breakdown
select
purpose as Loan_Purpose,
count(id) as Total_Application_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded
from BankLoan
group by purpose
order by count(id) desc
-- Home Ownership Analysis
select
home_ownership as Home_Ownership_Status,
count(id) as Total_Application_Count,
sum(total_payment) as Totol_Amount_Recived,
sum(loan_amount) as Total_Amount_Funded
from BankLoan
--where home_ownership <> 'NONE'
group by home_ownership
order by count(id)
-- Grid View, its nothing but the entire dataset
select *
from BankLoan