-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcensus project.txt
96 lines (53 loc) · 2.52 KB
/
census project.txt
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
select * from project.dbo.data1;
select * from project.dbo.data2;
-- number of rows into our dataset
select count(*) from project..data1
select count(*) from project..data2
-- dataset for jharkhand and bihar
select * from project..data1 where state in ('Jharkhand' ,'Bihar')
-- population of India
select sum(population) as Population from project..data2
-- avg growth
select state,avg(growth)*100 avg_growth from project..data1 group by state;
-- avg sex ratio
select state,round(avg(sex_ratio),0) avg_sex_ratio from project..data1 group by state order by avg_sex_ratio desc;
-- avg literacy rate
select state,round(avg(literacy),0) avg_literacy_ratio from project..data1
group by state having round(avg(literacy),0)>90 order by avg_literacy_ratio desc ;
-- top 3 state showing highest growth ratio
select state,avg(growth)*100 avg_growth from project..data1 group by state order by avg_growth desc limit 3;
--bottom 3 state showing lowest sex ratio
select top 3 state,round(avg(sex_ratio),0) avg_sex_ratio from project..data1 group by state order by avg_sex_ratio asc;
-- top and bottom 3 states in literacy state
drop table if exists #topstates;
create table #topstates
( state nvarchar(255),
topstate float
)
insert into #topstates
select state,round(avg(literacy),0) avg_literacy_ratio from project..data1
group by state order by avg_literacy_ratio desc;
select top 3 * from #topstates order by #topstates.topstate desc;
drop table if exists #bottomstates;
create table #bottomstates
( state nvarchar(255),
bottomstate float
)
insert into #bottomstates
select state,round(avg(literacy),0) avg_literacy_ratio from project..data1
group by state order by avg_literacy_ratio desc;
select top 3 * from #bottomstates order by #bottomstates.bottomstate asc;
--union opertor
select * from (
select top 3 * from #topstates order by #topstates.topstate desc) a
union
select * from (
select top 3 * from #bottomstates order by #bottomstates.bottomstate asc) b;
-- states starting with letter a
select distinct state from project..data1 where lower(state) like 'a%' or lower(state) like 'b%'
select distinct state from project..data1 where lower(state) like 'a%' and lower(state) like '%m'
--window
output top 3 districts from each state with highest literacy rate
select a.* from
(select district,state,literacy,rank() over(partition by state order by literacy desc) rnk from project..data1) a
where a.rnk in (1,2,3) order by state