In [1]:
%load_ext sql
%sql postgresql://dssg_student:password@seds-sql.csya4zsfb6y4.us-east-1.rds.amazonaws.com/dssg2016
Out[1]:
Data source: http://www.seattle.gov/seattle-police-department/crime-data/spd-data-sets
Lesson source: https://github.com/valentina-s/SQL_tutorial/
"A database system is basically a computerized recordkeeping system -- that is, a system whose overall purpose is to maintain information and to make that information available on demand." (Date 1986)
CREATE TABLE seattlecrimesincidents
("crimesID" int,
"Offense type" character,
"Offense code" int,
"Date" timestamp,
"Location" character);
crimesID | Offense type | Offense code | Date | Location |
---|---|---|---|---|
populating the database records:
INSERT INTO seattlecrimeincidents VALUES
(1,'trespass', 5700,'2015-01-28 09:30:00','12XX Block of E Pike St'),
(2,'larceny-theft',2300, '2015-02-21 08:24:21','15XX Block of Aurora St');
crimesID | Offense type | Offense code | Date | Location |
---|---|---|---|---|
1 | tresspass | 5700 | 2015-01-28 09:30:00 | 12XX Block of E Pike St |
2 | larceny-theft | 2300 | 2015-02-21 08:24:21 | 15XX Block of Aurora St |
Some common data types:
Name | Aliases | Description |
---|---|---|
boolean | bool | logical Boolean (true/false) |
character [(n)] | char [(n)] | fixed-length character string |
date | calendar date (year, month, day) | |
double precision | float8 | double precision floating-point number (8 bytes) |
integer | int, int4 | signed four-byte integer |
json | JSON data | |
money | currency amount | |
timestamp [(p)] [ without time zone ] | date and time (no time zone) | |
xml | XML data |
crimesID | Offense type | Offense code | Date | Location |
---|---|---|---|---|
1 | tresspass | X | 2015-01-28 09:30:00 | 12XX Block of E Pike St |
2 | burglary | 5710 | 2015-01-28 09:30:00 | 12XX Block of E Pike St |
3 | larceny-theft | 2300 | 2015-02-21 08:24:21 | 15XX Block of Aurora St |
crimesID | Offense type | Offense code | Date | Location |
---|---|---|---|---|
1 | tresspass | 5700 | 2015-01-28 09:30:00 | 12XX Block of E Pike St |
Offense type | Date |
---|---|
tresspass | 2015-01-28 09:30:00 |
larceny-theft | 2015-02-21 08:24:21 |
In [4]:
%%sql
SELECT "Date Reported", date_part('hour', "Date Reported")
FROM seattlecrimeincidents
LIMIT 5;
Out[4]:
crimesID | Offense code | Date | Location | Damage |
---|---|---|---|---|
1 | 5700 | 2015-01-28 09:30:00 | 12XX Block of E Pike St | \$1,220 |
1 | 5700 | 2015-02-12 03:25:00 | 1XX Block of Aloha St | \$11,420 |
2 | 5710 | 2015-01-28 09:30:00 | 12XX Block of E Pike St | \$5,389 |
2 | 5710 | 2015-1-02 12:31:20 | 12XX Block of E Pine St | \$15,231 |
3 | 2300 | 2015-02-21 08:24:21 | 15XX Block of Aurora St | \$2,405 |
crimesID | Offense code | Date | Location | Damage |
---|---|---|---|---|
1 | 5700 | 2015-01-28 09:30:00 | 12XX Block of E Pike St | \$1,220 |
2 | 5700 | 2015-02-12 03:25:00 | 1XX Block of Aloha St | \$11,420 |
3 | 5710 | 2015-01-28 09:30:00 | 12XX Block of E Pike St | \$5,389 |
4 | 5710 | 2015-1-02 12:31:20 | 12XX Block of E Pine St | \$15,231 |
5 | 2300 | 2015-02-21 08:24:21 | 15XX Block of Aurora St | \$2,405 |
SELECT SUM("Damage")
FROM seattlecrimeincidents
GROUP BY "Offense code";
Offense code | totalDamage |
---|---|
5700 | \$12,640 |
5710 | \$20,620 |
2300 | \$2,405 |
In [5]:
%%sql
SELECT "Date Reported", date_part('hour', "Date Reported") AS "reported hour"
FROM seattlecrimeincidents
LIMIT 5;
Out[5]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: