Data Cleaning & Data Exploration
use usprojects;select * from ushouseholdincome;select ID, count(ID)from ushouseholdincomegroup by IDhaving count(ID)>1;select * from ushouseholdincome_statistics;# Change column nameALTER TABLE ushouseholdincome_statistics rename column `id` to `ID`;select row_id, id,row_number() over(partition by id order by id) row_numfrom ushouseholdincome;select *from (select row_id, id,row_number() over(partition by id order by id) row_numfrom ushouseholdincome) duplicateswhere row_num >1;delete from ushouseholdincomewhere row_id in ( select row_id from ( select row_id, id, row_number() over(partition by id order by id) row_num from ushouseholdincome) duplicates where row_num >1);# Check whether there are duplicates in Table: ushouseholdincome_statisticsselect ID, count(ID)from ushouseholdincome_statisticsgroup by IDhaving count(ID)>1;# found duplicate state_name "Georgie" and lower case 'a' in 'Alabama'# Change state_nameupdate ushouseholdincomeset State_Name = 'Georgia'where State_Name = 'georia';update ushouseholdincomeset State_Name = 'Alabama'where State_Name = 'alabama';update ushouseholdincomeset Place = 'Autaugaville'where County='Autauga County' and City='Vinemont';select Type, Count(Type)from ushouseholdincomegroup by Typeorder by 1;update ushouseholdincomeset Type = 'Borough'where Type = 'Boroughs';select Aland, AWaterfrom ushouseholdincomewhere ALand=0 or ALand='' or ALand is null;select *from ushouseholdincome;

Use usprojects;
select *
from ushouseholdincome var
inner join ushouseholdincome_statistics inc
on var.id=inc.id
where Mean <> 0;
select var.State_name, round(avg(Mean),1), round(avg(Median),1), avg(ALand), avg(AWater)
from ushouseholdincome var
inner join ushouseholdincome_statistics inc
on var.id=inc.id
where Mean <> 0
group by var.State_Name
order by 3 desc
limit 10;
select var.Type, Count(var.Type), round(avg(Mean),1), round(avg(Median),1), avg(ALand), avg(AWater)
from ushouseholdincome var
inner join ushouseholdincome_statistics inc
on var.id=inc.id
where Mean <> 0
group by var.Type
order by 4 desc
limit 10;
select var.Type, Count(var.Type), round(avg(Mean),1), round(avg(Median),1), avg(ALand), avg(AWater)
from ushouseholdincome var
inner join ushouseholdincome_statistics inc
on var.id=inc.id
where Mean <> 0
group by var.Type
order by 4 desc
limit 10;
select *
from ushouseholdincome
where Type = 'County';
select *
from ushouseholdincome
where Type = 'Urban';
select var.Type, Count(var.Type), round(avg(Mean),1), round(avg(Median),1), avg(ALand), avg(AWater)
from ushouseholdincome var
inner join ushouseholdincome_statistics inc
on var.id=inc.id
where Mean <> 0
group by var.Type
having Count(var.Type)>100
order by 4 desc
limit 10;
select var.State_Name, City, round(avg(Mean),1), round(avg(Median),1)
from ushouseholdincome var
inner join ushouseholdincome_statistics inc
on var.id=inc.id
group by var.State_Name, City
order by round(avg(Mean),1) desc
limit 10;
