US Crime
This data has been gathered from https://www.kaggle.com - it shows crimes rates alongside some socio/economic indicators for each state for the year 2005 to 2015.
| State | Year | Crime_Count_homicide | population |
|---|---|---|---|
| Alabama | 2005 | 374 | 4548330 |
| Alabama | 2006 | 382 | 4599030 |
| Alabama | 2007 | 412 | 4627850 |
| Alabama | 2008 | 351 | 4661900 |
| Alabama | 2009 | 322 | 4708710 |
| Alabama | 2010 | 275 | 4785400 |
| Alabama | 2011 | 299 | 4803690 |
| Alabama | 2012 | 342 | 4817530 |
| Alabama | 2013 | 346 | 4834000 |
| Alabama | 2014 | 276 | 4846410 |
| Alabama | 2015 | 348 | 4853880 |
| Alaska | 2005 | 32 | 663253 |
| Alaska | 2006 | 36 | 670053 |
| Alaska | 2007 | 43 | 683478 |
| ... | |||
| Alaska | 2015 | 59 | 737709 |
| Arizona | 2005 | 445 | 5953010 |
| Arizona | 2006 | 533 | 6166320 |
| Arizona | 2007 | 548 | 6338760 |
| Arizona | 2007 | 548 | 6338760 |
| ... | |||
+------------------------------------+-------------+------+-----+---------+ | Field | Type | Null | Key | Default | +------------------------------------+-------------+------+-----+---------+ | State | varchar(20) | NO | PRI | NULL | | Year | int(11) | NO | PRI | NULL | | Crime_Count_aggravated_assault | float | YES | | NULL | | Crime_Count_burglary | float | YES | | NULL | | Crime_Count_homicide | float | YES | | NULL | | Crime_Count_larceny | float | YES | | NULL | | Crime_Count_motor_vehicle_theft | float | YES | | NULL | | Crime_Count_robbery | float | YES | | NULL | | Crime_Rate_aggravated_assault | float | YES | | NULL | | Crime_Rate_burglary | float | YES | | NULL | | Crime_Rate_homicide | float | YES | | NULL | | Crime_Rate_larceny | float | YES | | NULL | | Crime_Rate_motor_vehicle_theft | float | YES | | NULL | | Crime_Rate_robbery | float | YES | | NULL | | State_Abbr | varchar(2) | YES | | NULL | | Population | float | YES | | NULL | | Violent_Crime | float | YES | | NULL | | Property_Crime | float | YES | | NULL | | Rape_Legacy | float | YES | | NULL | | Rape_Revised | float | YES | | NULL | | Location | varchar(10) | YES | | NULL | | Age | float | YES | | NULL | | Average_household_size | float | YES | | NULL | | Black_white | float | YES | | NULL | | GDP_per_capita | float | YES | | NULL | | Gini_coefficient | float | YES | | NULL | | Hispanic_white | float | YES | | NULL | | Labor_force_participation | float | YES | | NULL | | Log_of_GDP_per_capita | float | YES | | NULL | | Corruption_convictions_per_1000000 | float | YES | | NULL | | People_with_less | float | YES | | NULL | | Poverty_rate | float | YES | | NULL | | Unemployment_rate | float | YES | | NULL | +------------------------------------+-------------+------+-----+---------+
Number of Homicides in Indiana
Use Crime_Count_homicide to show the number of homicides for each year in Indiana.
SELECT Year, Crime_Count_homicide FROM uscrime WHERE State='Indiana'
Notice that the figure for 2015 (370) is greater than that for 2005 (356), that shows an increase for the state of Indiana - but is that refected across the whole country?
Homicides across the whole US
Show the total number of homicides for each year, also show the total population and the homicide rate per million as a whole number.
FROM uscrime WHERE State='Iowa'
SELECT Year,
SUM(Crime_Count_homicide) h,SUM(population) p, ROUND(1000000*SUM(Crime_Count_homicide)/SUM(population)) hr
FROM uscrime GROUP BY Year
Notice the small decrease in homicides between 2005 and 2015 despite the small increase in the population over that period.
Rows to columns
The default query shows the number of homicides for 2005 and 2015 and the population for 2005 for each state.
Change it to add the 2015 population as the fifth column.
s05.Crime_Count_Homicide h05,s05.population p05,
s15.Crime_Count_Homicide h15
FROM uscrime s05 JOIN uscrime s15 ON
s05.State=s15.State AND s05.Year=2005 AND s15.Year=2015
s05.Crime_Count_Homicide h05,s05.population p05,
s15.Crime_Count_Homicide h15,s15.population p15
FROM uscrime s05 JOIN uscrime s15 ON
s05.State=s15.State AND s05.Year=2005 AND s15.Year=2015
Which States saw an increase in Homicide Rates
Identify the states that saw an increase in the homicide rate between 2005 and 2015.
SELECT Year, Crime_Count_homicide FROM uscrime WHERE State='Iowa'
SELECT State FROM ( SELECT State, Crime_Rate_Homicide h2005,0 h2015 FROM uscrime WHERE Year=2005 UNION SELECT State, 0, Crime_Rate_Homicide FROM uscrime WHERE Year=2015) as c GROUP BY State HAVING SUM(h2015)>SUM(h2005)