Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Disclaimer:
• All images such as logos, photos, etc. used in this presentation are the property of their respective copyright owners and are used
here for educational purposes only
• Some material adapted from: Sorger, Stephan. “Marketing Analytics: Strategic Models and Metrics. Admiral Press. 2013.
Introduction to
Data Science and Analytics
Stephan Sorger www.StephanSorger.com
Unit 6A. SQL: Overview and Coding SQL Commands
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 1
Outline/ Learning Objectives
Topic Description
Introduction Languages to query (get data from) databases
Vendors Top 3 vendors in market
RDBMS Relational Database Management Systems
Commands Litany of various SQL Commands, with examples
Resources Learning more about SQL
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 2
Database Query: Introduction
Category Description
SQL Structured Query Language (for this class: ANSI SQL)
Programming language for accessing RDBMS
RDBMS Relational Database Management System
Two Parts Data Definition Language: Syntax for defining data structures
Data Manipulation Language: Syntax for selecting, inserting
deleting, and updating data in databases
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 3
Structured Query Language
Data Definition Language Data Manipulation Language
Category Description
Top 3 Top 3 Database engines
Oracle Target market: Fortune 500 Corporations
Oracle Database, Enterprise Edition, $10,450
Microsoft Target market: Small – Mid Sized Businesses
Microsoft SQL Server, Enterprise Edition, $14,256
MySQL Target market: Small – Mid-Sized Businesses; Education
Open Source, Free
Available for Mac and PC
Others… Sybase, MS Access, Ingres, etc.
Database Query: Introduction
Oracle price list, Oct. 2015: http://www.oracle.com/us/corporate/pricing/technology-price-list-070617.pdf
Microsoft price list, Dec. 2015: http://www.microsoft.com/en-us/server-cloud/products/sql-server/purchasing.aspx
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 4
RDBMS Database Example
Row: Record
Primary Key: Column with 100% unique entries
similar to Social Security Number (‘Engine’ column will not work; duplicate entries)
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 5
Table: Jazzstars
last first id age city state
Armstrong Louis 11111 70 New Orleans Louisiana
Baker Chet 11112 58 Yale Oklahoma
Basie Count 11113 80 Red Bank New Jersey
Blakey Art 11114 32 Pittsburgh Pennsylvania
Brown Clifford 11115 25 Wilmington Delaware
Brubeck Dave 11116 91 Concord California
Calloway Cab 11117 86 Rochester New York
Carter Benny 11118 96 New York New York
Cole NatKing 11119 45 Montgomery Alabama
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 6
Sample Table: Jazzstars
SELECT
Topic Description
SELECT Used to query database and retrieve chosen data
Syntax SELECT “column1” [, “column2”, etc ] [ optional ]
FROM “tablename” [ WHERE “condition” ] ;
Conditionals = Equal
> Greater than; < Less than
>= Greater than or equal; <= Less than or equal
<> Not equal to
LIKE pattern matching operator…startswith%, %endswith
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 7
SELECT
Topic Description
SELECT Used to query database and retrieve chosen data
Syntax SELECT “column1” [, “column2”, etc ] [ optional ]
FROM “tablename” [ WHERE “condition” ] ;
Conditionals = Equal
> Greater than; < Less than
>= Greater than or equal; <= Less than or equal
<> Not equal to
LIKE pattern matching operator…startswith%, %endswith
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 8
SELECT age FROM Jazzstars; Returns age column
SELECT * FROM Jazzstars WHERE first=‘Count’; Returns Count Basie ( * = wildcard )
SELECT first, last FROM Jazzstars WHERE first LIKE ‘Ba%’; Returns Chet Baker, Count Basie
SELECT first, last FROM Jazzstars WHERE last LIKE ‘%k’; Returns Dave Brubeck
Format at Most Companies
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 9
SELECT age FROM Jazzstars;
Command in CAPS
Database element in lowercase Database name in Mixed/ lower
Format at Most Companies
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 10
WARNING:
Not following format can be
instantly-fireable offense
at some companies
CODING HAZARD
Troubleshooting nightmare
Similar to leaving hammer
on top of ladder in construction
SAFETY HAZARD
Enforcement nightmare
CREATE TABLE
Topic Description
CREATE TABLE Used to create a new table
Syntax CREATE TABLE “tablename”
(“column1” “datatype” [constraint], “column2”…); [ ] = optional
Datatype char(size) Fixed-length character string
varchar(size) Variable-length character string
number(size) Numerical value, integer
number(size,d) Numerical value, real, with d digits in decimal
date Date value
Constraint Rule associated with column: “Not null”; “Primary key”
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 11
CREATE TABLE
Topic Description
CREATE TABLE Used to create a new table
Syntax CREATE TABLE “tablename”
(“column1” “datatype” [constraint], “column2”…); [ ] = optional
Datatype char(size) Fixed-length character string
varchar(size) Variable-length character string
number(size) Numerical value, integer
number(size,d) Numerical value, real, with d digits in decimal
date Date value
Constraint Rule associated with column: “Not null”; “Primary key”
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 12
CREATE TABLE Jazzstars (last varchar(15), first varchar(15), id number (10),
age number (3), city varchar (20), state varchar (20));
INSERT INTO (table)
Topic Description
INSERT INTO Insert a row of data into existing table
Syntax INSERT INTO “tablename” (columnlist)
VALUES (‘valuestoinsert’);
Purpose Adding a new record into table, using same structure
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 13
INSERT INTO Jazzstars (last, first, id, age, city, state)
VALUES (‘Davis’, ‘Miles’; ‘11120’, ’65’, ‘Alton’, ‘Illinois’);
Table: Jazzstars
last first id age city state
Armstrong Louis 11111 70 New Orleans Louisiana
Baker Chet 11112 58 Yale Oklahoma
Basie Count 11113 80 Red Bank New Jersey
Blakey Art 11114 32 Pittsburgh Pennsylvania
Brown Clifford 11115 25 Wilmington Delaware
Brubeck Dave 11116 91 Concord California
Calloway Cab 11117 86 Rochester New York
Carter Benny 11118 96 New York New York
Cole NatKing 11119 45 Montgomery Alabama
Davis Miles 11120 65 Alton Illinois
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 14
New record
UPDATE
Topic Description
UPDATE Change records matching specified criteria
Syntax UPDATE “tablename”
SET “columnname” = “newvalue” [, “nextcolumn” = “newvalue2”]
WHERE “columnname”
AND|OR “value” [AND|OR “column” AND|OR “value”];
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 15
UPDATE SFPeninsula_Phonebook SET zipcode = 94028 WHERE prefix = 851;
UPDATE SFPeninsula_Phonebook SET lastname = ‘Marriedname’, prefix=555, suffix=1212
WHERE lastname = ‘Maidenname’;
UPDATE employeedatabase SET age=age+1
WHERE lastname= ‘Boy’ AND firstname = ‘Birthday’;
DELETE FROM
Topic Description
DELETE FROM Eliminate records (rows) from tables
Syntax DELETE FROM “tablename”
AND|OR “value” [AND|OR “column” AND|OR “value”];
[ ] = optional
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 16
DELETE FROM Jazzstars;
Table: Jazzstars
Last First ID Age City State
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 17
Oops; Just deleted everything; I hope I made a backup
Never mind, I hope I keep my job
DELETE FROM
Topic Description
DELETE FROM Eliminate records (rows) from tables
Syntax DELETE FROM “tablename”
AND|OR “value” [AND|OR “column” AND|OR “value”];
[ ] = optional
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 18
DELETE FROM Jazzstars WHERE last = ‘Davis’; Just got rid of Miles Davis
DELETE FROM Jazzstars WHERE first = ‘Chet’ AND last = ‘Baker’; Baker is common name
DROP TABLE
Topic Description
DROP TABLE Delete table and all rows in the table
Syntax DROP TABLE “tablename”
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 19
DROP TABLE Jazzstars Table now gone
Table Joins
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 20
One-Table Database
id last first address city state item purchased price paid
1111 Able Adam 123 Main Maple MA X-1000 $45.00
1111 Able Adam 123 Main Mapli MA X-2000 $55.00
1112 Baker James 456 Main Maple MA X-master 1 $55.00
Topic Description
Table Joins Select rows from another table Linking data from tables
Syntax SELECT “list-of-columns” FROM Table1, Table2
WHERE “search_conditions”;
Structure One-Table Database; aka Flat Table, before RDBMS invented
Problem 1: Huge table with lots of redundant data
Problem 2: Redundant data prone to errors (see table below)
Table Joins
Topic Description
Table Joins Select rows from another table Linking data from tables
Syntax SELECT “list-of-columns” FROM Table1, Table2
WHERE “search_conditions”;
Structure RDBMS Database structure can now include 2 tables
1. Customer table for keeping track of customers
2. Purchase table for keeping track of purchases
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 21
Customers Table: “Customers”
id last first address city state zip
1111 Able Adam 123 Main Maple MA 00001
1112 Baker James 456 Main Maple MA 00001
Purchase Table: “Purchases”
id date item price
1111 1/1/2016 X-1000 $45.00
1112 2/1/2016 X-2000 $55.00
Both tables have common ID column
Table Joins
Topic Description
Table Joins Select rows from another table Linking data from tables
Syntax SELECT “list-of-columns” FROM Table1, Table2
WHERE “search_conditions”;
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 22
Example: Select customer’s name and items they purchased:
SELECT Customers.last, Customers.first, Purchases.item FROM Customers, Purchases
WHERE Customers.id = Purchases.id;
Purchase Table: “Purchases”
id date item price
1111 1/1/2016 X-1000 $45.00
1112 2/1/2016 X-2000 $55.00
Customers Table: “Customers”
id last first address city state zip
1111 Able Adam 123 Main Maple MA 00001
1112 Baker James 456 Main Maple MA 00001
SQL Resources: Learning More About SQL: Print Books
Learning SQL
By Alan Beaulieu
Published by O’Reilly Media
SQL in 10 Minutes, Sams Teach Yourself
By Ben Forta
Published by Sams Publishing
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 23
SQL Resources: Learning More About SQL: Online
sqlcourse.com
sqlzoo.net
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 24
SQL Resources: Learning More About SQL: Video
lynda.com/SQL
SQL Essential Training
YouTube.com
SQL basics
Microsoft SQL Server
Oracle RDBMS
Other
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 25
Outline/ Learning Objectives
Topic Description
Introduction Languages to query (get data from) databases
Vendors Top 3 vendors in market
RDBMS Relational Database Management Systems
Commands Litany of various SQL Commands, with examples
Resources Learning more about SQL
© Stephan Sorger 2016; www.StephanSorger.com; SQL: Commands 26