In [2]:
# loading the special sql extension
%load_ext sql

In [1]:
# connecting to a database which lives on the Amazon Cloud
# need to substitute password with the one provided in the email!!!
%sql postgresql://dssg_student:password@seds-sql.csya4zsfb6y4.us-east-1.rds.amazonaws.com/dssg2016


ERROR: Line magic function `%sql` not found.

Two interesting tables: seattlecrimeincidents first half of 2015 census_data


In [ ]:
# running a simple SQL command
%sql select * from seattlecrimeincidents limit 10;

In [ ]:
# Show specific columns
%sql select "Offense Type",latitude,longitude from seattlecrimeincidents limit 10;

In [ ]:
%%sql
-- select rows
select "Offense Type", latitude, longitude, month from seattlecrimeincidents
    where "Offense Type" ='THEFT-BICYCLE' and month = 1

In [ ]:
%%sql
select count(*) from seattlecrimeincidents;

In [ ]:
%%sql
select count(*) from settlecrimeincidents

In [ ]:
%%sql
select count(*) from (select "Offense Type", latitude, longitude, month from seattlecrimeincidents
    where "Offense Type" ='THEFT-BICYCLE' and month = 1) as small_table

In [5]:
# use max, min functions

In [6]:
%%sql 
select min(latitude) as min_lat,max(latitude) as max_lat,
        min(longitude)as min_long,max(longitude) as max_long
        from seattlecrimeincidents;


1 rows affected.
Out[6]:
min_lat max_lat min_long max_long
47.46221396 47.75651395 -122.4193685 -122.2282241

In [7]:
%%sql
select year,count(*) from seattlecrimeincidents 
    group by year
    order by year ASC;


17 rows affected.
Out[7]:
year count
1990 1
1999 1
2000 1
2001 2
2002 1
2004 1
2005 3
2006 1
2007 5
2008 4
2009 2
2010 10
2011 8
2012 17
2013 35
2014 691
2015 30031

In [8]:
%%sql
select distinct year from seattlecrimeincidents;


17 rows affected.
Out[8]:
year
2010
2006
2000
1999
2005
2013
2009
2004
2007
1990
2012
2011
2002
2001
2014
2008
2015

In [ ]:


In [ ]:


In [ ]: