US Crime

From SQLZoo

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.

uscrime
StateYearCrime_Count_homicidepopulation
Alabama20053744548330
Alabama20063824599030
Alabama20074124627850
Alabama20083514661900
Alabama20093224708710
Alabama20102754785400
Alabama20112994803690
Alabama20123424817530
Alabama20133464834000
Alabama20142764846410
Alabama20153484853880
Alaska200532663253
Alaska200636670053
Alaska200743683478
...
Alaska201559737709
Arizona20054455953010
Arizona20065336166320
Arizona20075486338760
Arizona20075486338760
...
+------------------------------------+-------------+------+-----+---------+
| 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='Iowa'

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.

SELECT Year, Crime_Count_homicide
 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.

Homicides across the whole US

Show the total number of homicides for each year, also show the total population.

Notice the small decrease in homicides between 2005 and 2015 despite the small increase in the population over that period.

SELECT Year, Crime_Count_homicide FROM uscrime WHERE State='Iowa'

SELECT Year,

      SUM(Crime_Count_Homicide) hom,
      SUM(population) pop

FROM uscrime GROUP BY Year

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.

SELECT State,
 SUM(CASE WHEN Year=2005 THEN Crime_Count_Homicide END) h2005,
 SUM(CASE WHEN Year=2015 THEN Crime_Count_Homicide END) h2015,
 SUM(CASE WHEN Year=2005 THEN population END) p2005
FROM uscrime

WHERE Year IN (2005,2015) GROUP BY State

SELECT State,

 SUM(CASE WHEN Year=2005 THEN Crime_Count_Homicide END) h2005,
 SUM(CASE WHEN Year=2015 THEN Crime_Count_Homicide END) h2015,
 SUM(CASE WHEN Year=2005 THEN population END) p2005,
 SUM(CASE WHEN Year=2015 THEN population END) p2015
FROM uscrime

WHERE Year IN (2005,2015) GROUP BY State

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)

DataWars, Data Science Practice Projects - LogoDataWars: Practice Data Science/Analysis with +100 Real Life Projects
  • Served by: po at 2026-06-09T07:58