Skip to content

Commit 0f21bfe

Browse files
authored
Add files via upload
1 parent ccf9ec1 commit 0f21bfe

File tree

1 file changed

+295
-0
lines changed

1 file changed

+295
-0
lines changed

2011_Census_Data_Anlaysis_SQL.sql

+295
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
SELECT * FROM census_1
2+
SELECT * FROM census_2
3+
4+
----1. # Total number of states and union territories in the dataset---
5+
6+
SELECT state from census_1
7+
GROUP BY state;
8+
9+
---2. # SELECTING THE PARTTICULAR STATES TO VIEW THE DATA---
10+
11+
SELECT * FROM census_1
12+
WHERE state IN ('Assam','Punjab')
13+
ORDER BY state;
14+
15+
---3. # FINDING THE TOTAL POPULATION OF INDIA----
16+
17+
SELECT SUM(population) AS Total_Population FROM census_1
18+
19+
---4. # FINDING THE TOTAL POPULATION OF INDIA (STATEWISE)----
20+
21+
SELECT state,SUM(population) AS Total_Population
22+
FROM census_1
23+
GROUP BY state
24+
ORDER BY Total_Population DESC;
25+
26+
---5. # FINDING THE AVERAGE POPULATION GROWTH RATE OF INDIA IN LAST 10 YAERS---
27+
28+
SELECT ROUND(AVG(growth),2) AS avg_growth FROM census_2;
29+
30+
----6. # FINDING THE AVERAGE POPULATION GROWTH RATE OF INDIA(STATEWISE) IN LAST 10 YAERS---
31+
32+
SELECT state,ROUND(AVG(growth),2) AS avg_growth
33+
FROM census_2
34+
GROUP BY state
35+
ORDER BY avg_growth DESC
36+
37+
----7. # FINDING THE AVERAGE POPULATION GROWTH RATE OF INDIA(ASSAM) IN LAST 10 YAERS---
38+
39+
SELECT state,ROUND(AVG(growth),2) AS avg_growth
40+
FROM census_2
41+
WHERE state='Assam'
42+
GROUP BY state;
43+
44+
----8. # FINDING THE AVERAGE SEX RATIO OF INDIA ---
45+
46+
SELECT AVG(sex_ratio) AS avg_sex_ratio FROM census_2;
47+
48+
----9. # FINDING THE AVERAGE SEX RATIO OF INDIA(STATEWISE) ---
49+
50+
SELECT state,AVG(sex_ratio) AS avg_sex_ratio
51+
FROM census_2
52+
GROUP BY state
53+
ORDER BY avg_sex_ratio DESC;
54+
55+
----10. # FINDING THE AVERAGE SEX RATIO OF INDIA(ASSAM) ---
56+
57+
SELECT state,AVG(sex_ratio) AS avg_sex_ratio
58+
FROM census_2
59+
WHERE state='Assam'
60+
GROUP BY state;
61+
62+
----11. # FINDING THE AVERAGE literacy rate OF INDIA ---
63+
64+
SELECT AVG(literacy) AS avg_literacy FROM census_2;
65+
66+
----12. # FINDING THE AVERAGE literacy OF INDIA(STATEWISE) ---
67+
68+
SELECT state,AVG(literacy) AS avg_literacy
69+
FROM census_2
70+
GROUP BY state
71+
ORDER BY avg_literacy DESC;
72+
73+
----13. # FINDING THE AVERAGE literacy OF INDIA(ASSAM) ---
74+
75+
SELECT state,AVG(literacy) AS avg_literacy
76+
FROM census_2
77+
WHERE state='Assam'
78+
GROUP BY state
79+
80+
----14. # FINDING THE AVERAGE literacy OF INDIA(STATEWISE)>90 ---
81+
82+
SELECT state,ROUND(AVG(literacy)) AS avg_literacy
83+
FROM census_2
84+
GROUP BY state
85+
HAVING ROUND(AVG(literacy))>90
86+
ORDER BY avg_literacy DESC;
87+
88+
----15. # FINDING THE TOP 3 STATES HAVING HGHEST AVERAGE POPULATION GROWTH RATE IN INDIA -----
89+
90+
SELECT state,ROUND(AVG(growth)) AS High_avg_growth
91+
FROM census_2
92+
GROUP BY state
93+
ORDER BY High_avg_growth DESC
94+
LIMIT 3;
95+
96+
----16. # FINDING THE TOP 3 STATES HAVING lowest AVERAGE POPULATION GROWTH RATE IN INDIA -----
97+
98+
SELECT state,ROUND(AVG(growth)) AS low_avg_growth
99+
FROM census_2
100+
GROUP BY state
101+
ORDER BY low_avg_growth
102+
LIMIT 3;
103+
104+
----17. # FINDING THE TOP 3 STATES HAVING HGHEST AVERAGE SEX RATIO IN INDIA -----
105+
106+
SELECT state,ROUND(AVG(sex_ratio)) AS High_avg_sex_ratio
107+
FROM census_2
108+
GROUP BY state
109+
ORDER BY High_avg_sex_ratio DESC
110+
LIMIT 3;
111+
112+
----18. # FINDING THE TOP 3 STATES HAVING lowest AVERAGE SEX RATIO IN INDIA -----
113+
114+
SELECT state,ROUND(AVG(sex_ratio)) AS low_avg_sex_ratio
115+
FROM census_2
116+
GROUP BY state
117+
ORDER BY low_avg_sex_ratio ASC
118+
LIMIT 3;
119+
120+
--- 19. #Combine the top 3 states with highest average sex ratio and top 3 states with lowest average sex ratio
121+
--- using CTEs(Common Table Expressions) and UNION/UNION ALL operator----
122+
123+
WITH HighSexRatio AS (
124+
SELECT state, ROUND(AVG(sex_ratio)) AS avg_sex_ratio
125+
FROM census_2
126+
GROUP BY state
127+
ORDER BY avg_sex_ratio DESC
128+
LIMIT 3
129+
),
130+
LowSexRatio AS (
131+
SELECT state, ROUND(AVG(sex_ratio)) AS avg_sex_ratio
132+
FROM census_2
133+
GROUP BY state
134+
ORDER BY avg_sex_ratio ASC
135+
LIMIT 3
136+
)
137+
SELECT 'Top 3 High Sex Ratio' AS category, state, avg_sex_ratio
138+
FROM HighSexRatio
139+
140+
UNION ALL
141+
142+
SELECT 'Top 3 Low Sex Ratio' AS category, state, avg_sex_ratio
143+
FROM LowSexRatio;
144+
145+
--- 20. #Combine the top 3 states with highest average literacy rate and top 3 states with lowest average literacy
146+
--- rate using CTEs(Common Table Expressions) and UNION/UNION ALL operator----
147+
148+
WITH High_Literacy AS (
149+
150+
SELECT state,ROUND(AVG(literacy)) AS avg_literacy
151+
FROM census_2
152+
GROUP BY state
153+
ORDER BY avg_literacy DESC
154+
LIMIT 3
155+
),
156+
157+
Low_Literacy AS (
158+
159+
SELECT state,ROUND(AVG(literacy)) AS avg_literacy
160+
FROM census_2
161+
GROUP BY State
162+
ORDER BY avg_literacy ASC
163+
LIMIT 3
164+
)
165+
166+
SELECT 'High_literacy_rate' AS category, state, avg_literacy
167+
FROM High_Literacy
168+
169+
UNION ALL
170+
171+
SELECT 'Low_literacy_rate' AS category, state, avg_literacy
172+
FROM Low_Literacy;
173+
174+
175+
---- # 21. Finding the name of states which starts with 'a' & 'b'
176+
177+
SELECT DISTINCT state FROM census_2
178+
WHERE state LIKE 'A_sa_' OR state LIKE 'B%';
179+
180+
--- OR (if we want to view unique rows of data in a column, we can use either DINSTINCT or GROUP BY Clause)
181+
182+
SELECT state FROM census_2
183+
WHERE Lower(state) LIKE 'a%' OR Lower(state) LIKE 'b%'
184+
GROUP BY state;
185+
186+
---- # 22. Finding the name of states which ends with 'a'.
187+
188+
SELECT DISTINCT state FROM census_2
189+
WHERE state LIKE '%a';
190+
191+
---- # 23. Finding out the total number of males and females in the different states of India.(in terms of
192+
--- populualtion and sex_ratio/gender ratio)
193+
194+
-----# (We can join two or more tables even if without the foreign key, if we have commom expression or atleast
195+
------ one row of data common in both the table)
196+
197+
--- Since we have only sex_ratio and population columns in our database tables, so we need to do statiscal analysis
198+
--- to derive a formula to find the total no. of males and females in all the states of India.
199+
200+
--- sex_ratio = females/males -------> (1)
201+
--- population = males+female (disregarding other genders) ------>(2)
202+
--- females=population-males -------->(3)
203+
204+
--- substituting (3) into (1)
205+
--- sex_ratio*males=(population-males)
206+
--- sex_ratio*males+males = population
207+
--- population= males(sex_ratio+1)
208+
209+
--- males = population/(sex_ratio+1) -----(Total males) -----(4)
210+
--- substituting (4) into (3)
211+
--- females = population-population/(sex_ratio+1)
212+
--- females = population(1-1/(sex_ratio+1))
213+
--- feamles = (population*(sex_ratio))/(sex_ratio+1) --- (Total females)
214+
215+
SELECT d.state,SUM(d.Total_males)AS T_males,SUM(d.Total_females) AS T_females
216+
FROM
217+
(SELECT c.district,c.state,ROUND(c.population/(c.sex_ratio+1)) Total_males,
218+
ROUND((c.population*(c.sex_ratio))/(c.sex_ratio+1)) Total_females
219+
FROM (SELECT c1.district,c1.state,c1.sex_ratio/1000 AS sex_ratio,c2.population
220+
FROM census_2 AS c1 INNER JOIN census_1 AS c2 ON c1.district=c2.district) AS c)AS d
221+
GROUP BY state
222+
ORDER BY state;
223+
224+
--- # 24. Finding out the total number of literate and illiterate people in different states of India(in terms of
225+
--- populualtion and literacy rate)
226+
227+
---- Here also, we will use literacy rate and population column to find out the desired result
228+
229+
--- literacy_rate = total literate people/population
230+
--- total literate people = literacy_rate*population--------(1) -----(T_L_P)
231+
--- total illitearate people = population - total literate people-----(2)
232+
--- Substituting (1) into (2)
233+
--- total illiterate people = population -(literacy_rate*population)
234+
--- Factor out the common term 'population'
235+
--- total illiterate people = population(1-literacy_rate*1)
236+
--- total illiterate people = (1-literacy_rate)*population -----(T_I_P)
237+
238+
SELECT d.state,SUM(d.Total_literate) Total_literateP,SUM(d.Total_illiterate) Total_illiterateP FROM
239+
(SELECT c.district,c.state,ROUND(c.literacy_rate*c.population) AS Total_literate,
240+
ROUND((1-c.literacy_rate)*c.population) AS Total_illiterate
241+
FROM (SELECT c2.district,c2.state,c2.literacy/100 AS literacy_rate,c1.population
242+
FROM census_1 c1 INNER JOIN census_2 c2
243+
ON c1.district=c2.district ) AS c) AS d
244+
GROUP BY state
245+
ORDER BY 2 DESC, 3 DESC;
246+
247+
--- # 25. Finding the previous census poupulation of states in India(in terms of current populualtion and growth rate)
248+
249+
--- Here also we use statistical analysis to find out the desried result by deriving the formula
250+
251+
--- previous_census+growth*previous_census=population
252+
--- previous_census(1+growth)=population
253+
--- previous_census=population/(1+growth)
254+
255+
256+
SELECT q.state,ROUND(AVG(q.g_r)) AS g_r,SUM(q.prev_census_population) p_c_p,SUM(q.current_census_population) c_c_p FROM
257+
(SELECT p.district,p.state,p.growth_rate*100 AS g_r,
258+
ROUND(p.population/(1+p.growth_rate)) AS prev_census_population,p.population current_census_population FROM
259+
(SELECT c2.district,c2.state,c2.growth/100 growth_rate,c1.population
260+
FROM census_1 c1 INNER JOIN census_2 c2 ON c1.district=c2.district) AS p) AS q
261+
GROUP BY state
262+
ORDER BY 2 DESC;
263+
264+
--- #26. Finding the total previous census poupulation in India(in terms of current populualtion and growth rate)
265+
266+
267+
SELECT SUM(r.p_c_p) prev_census_population, SUM(r.c_c_p) current_census_population FROM
268+
(SELECT q.state,ROUND(AVG(q.g_r)) AS g_r,SUM(q.prev_census_population) p_c_p,SUM(q.current_census_population) c_c_p FROM
269+
(SELECT p.district,p.state,p.growth_rate*100 AS g_r,
270+
ROUND(p.population/(1+p.growth_rate)) AS prev_census_population,p.population current_census_population FROM
271+
(SELECT c2.district,c2.state,c2.growth/100 growth_rate,c1.population
272+
FROM census_1 c1 INNER JOIN census_2 c2 ON c1.district=c2.district) AS p) AS q
273+
GROUP BY state
274+
ORDER BY 2 DESC) AS r;
275+
276+
--- #27. Finding out the population density of different states of India
277+
278+
--- population_density = total population of an area/total land area
279+
280+
SELECT c.state,SUM(c.area_km2) area_km2,SUM(c.poulation_density_per_km2) poulation_density_per_km2 FROM
281+
(SELECT district,state,area_km2, population/area_km2 AS poulation_density_per_km2
282+
FROM census_1)AS c
283+
GROUP BY state
284+
ORDER BY 3 DESC;
285+
286+
--- # 28. Finding top 3 districts from each state of India with highest literacy rate
287+
--- (If we have to find/segregated certain value(s) from a group, we can use ranking functions best on your uses cases)
288+
289+
SELECT n.* FROM
290+
(SELECT district,state,literacy,
291+
RANK() OVER(PARTITION BY state ORDER BY literacy DESC) AS rnk
292+
FROM census_2) n
293+
WHERE rnk IN (1,2,3)
294+
ORDER BY state;
295+

0 commit comments

Comments
 (0)