-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sql insights.sql
126 lines (90 loc) · 2.73 KB
/
Sql insights.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
/* Creating new database */
create database credit;
/* Importing the table named credit_card_cleaned*/
select * from credit_cleaned;
/* Renaming the table */
RENAME TABLE credit_cleaned TO credit_card;
/*1.Group the customers based on their income type and find the average
of their annual income.*/
select Type_income,round(avg(Annual_income),2) as Avg_income
from credit_card
group by Type_income
order by Avg_income desc;
/*Answer:
# Type_income Avg_income
Commercial associate 220539.04
State servant 206825.43
Working 178835.41
Pensioner 148199.26 */
# 2. Find the female owners of cars and property.
select count(Id) as Total_females
from credit_card
where Gender = 'F' and Car_owner = 'Y' and Property_owner ='Y';
/*# Answer:
Total_females
179
179 Female customers own both Car and Property*/
#3.Find the male customers who are staying with their families.
select *
from credit_card
where Gender = 'M' and Family_members > 1;
/*Answer:
470 males are living with their families */
#4.Please list the top five people having the highest income.
/* considering the top five income and people of this category */
with cte as (
select Id,Annual_income,rank() over (order by Annual_income desc) as rnk
from credit_card
order by Annual_income desc)
select * from cte
where rnk <= 5;
/* Answer:
1st Top income -492750- 3 persons- rank 1, (rank 2 and 3 is skipped)
2nd Top income - 450000- 1 person -rank 4
3rd Top income - 438750 -29 persons - rank 5
Id Annual_income
5067653 492750
5088834 492750
5088836 492750
5009074 450000
5010864 438750....and 28 more */
#5.How many married people are having bad credit?
select count(Id) as Total
from credit_card
where Marital_status <> 'Single / not married'
and Credit_status = 1;
/*Answer:
Total
140
140 married people having bad credit */
#6.What is the highest education level and what is the total count?
select distinct Education from credit_card;
# Highest education level is "Academic degree"
/* # Education
Higher education
Secondary / secondary special
Lower secondary
Incomplete higher
Academic degree */
select count(Id) as Total
from credit_card
where Education = 'Academic degree';
/* Answer:
Total
2
only 2 are of Highest education level */
#7.Between married males and females, who is having more bad credit?
with cte as (
select Gender, count(Id) as num_of_applicants,
sum(case when Credit_status = 1 then 1 else 0 end) bad_credit
from credit_card
where Marital_status <> 'Single / not married'
group by Gender)
select Gender ,round((bad_credit/num_of_applicants)*100,2) as percentage_bad_credit
from cte
order by percentage_bad_credit desc;
/* Answer:
# Gender percentage_bad_credit
M 11.98
F 9.80
# Males have highest bad credit percentage (11.98%) */