Introduction to Data Science and Analytics Stephan Sorger www.stephansorger.com Unit 6A. SQL: Overview and Coding SQL Commands 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. Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 1
Outline/ Learning Objectives Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 2 Topic 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 3 Database Query: Introduction Category SQL RDBMS Two Parts Structured Query Language (for this class: ANSI SQL) Programming language for accessing RDBMS Relational Database Management System Data Definition Language: Syntax for defining data structures Data Manipulation Language: Syntax for selecting, inserting deleting, and updating data in databases Structured Query Language Data Definition Language Data Manipulation Language
Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 4 Database Query: Introduction Category Top 3 Oracle Microsoft MySQL Others Top 3 Database engines Target market: Fortune 500 Corporations Oracle Database, Enterprise Edition, $10,450 Target market: Small Mid Sized Businesses Microsoft SQL Server, Enterprise Edition, $14,256 Target market: Small Mid-Sized Businesses; Education Open Source, Free Available for Mac and PC Sybase, MS Access, Ingres, etc. 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 5 RDBMS Database Example Primary Key: Column with 100% unique entries similar to Social Security Number ( Engine column will not work; duplicate entries) Row: Record
Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 6 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 Sample Table: Jazzstars
Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 7 SELECT Topic 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 Topic 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 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 Database element in lowercase Database name in Mixed/ lower SELECT age FROM Jazzstars; Command in CAPS
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 Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 11 Topic 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
CREATE TABLE Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 12 Topic 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 CREATE TABLE Jazzstars (last varchar(15), first varchar(15), id number (10), age number (3), city varchar (20), state varchar (20));
INSERT INTO (table) Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 13 Topic 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 INSERT INTO Jazzstars (last, first, id, age, city, state) VALUES ( Davis, Miles ; 11120, 65, Alton, Illinois );
Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 14 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 New record
UPDATE Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 15 Topic UPDATE Syntax Change records matching specified criteria UPDATE tablename SET columnname = newvalue [, nextcolumn = newvalue2 ] WHERE columnname AND OR value [AND OR column AND OR value ]; 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 Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 16 Topic DELETE FROM Eliminate records (rows) from tables Syntax DELETE FROM tablename AND OR value [AND OR column AND OR value ]; [ ] = optional DELETE FROM Jazzstars;
Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 17 Table: Jazzstars Last First ID Age City State Oops; Just deleted everything; I hope I made a backup Never mind, I hope I keep my job
DELETE FROM Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 18 Topic DELETE FROM Eliminate records (rows) from tables Syntax DELETE FROM tablename AND OR value [AND OR column AND OR value ]; [ ] = optional 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 Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 19 Topic DROP TABLE Delete table and all rows in the table Syntax DROP TABLE tablename DROP TABLE Jazzstars Table now gone
Table Joins Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 20 Topic Table Joins Syntax Structure Select rows from another table Linking data from tables SELECT list-of-columns FROM Table1, Table2 WHERE search_conditions ; 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) 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
Table Joins Topic Table Joins Syntax Structure Select rows from another table Linking data from tables SELECT list-of-columns FROM Table1, Table2 WHERE search_conditions ; RDBMS Database structure can now include 2 tables 1. Customer table for keeping track of customers 2. Purchase table for keeping track of purchases 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 Both tables have common ID column 1112 2/1/2016 X-2000 $55.00 Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 21
Table Joins Topic Table Joins Syntax Select rows from another table Linking data from tables SELECT list-of-columns FROM Table1, Table2 WHERE search_conditions ; Example: Select customer s name and items they purchased: SELECT Customers.last, Customers.first, Purchases.item FROM Customers, Purchases WHERE Customers.id = Purchases.id; 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 Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 22
SQL Resources: Learning More About SQL: Print Books Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 23 Learning SQL By Alan Beaulieu Published by O Reilly Media SQL in 10 Minutes, Sams Teach Yourself By Ben Forta Published by Sams Publishing
SQL Resources: Learning More About SQL: Online Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 24 sqlcourse.com sqlzoo.net
SQL Resources: Learning More About SQL: Video Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 25 lynda.com/sql SQL Essential Training YouTube.com SQL basics Microsoft SQL Server Oracle RDBMS Other
Outline/ Learning Objectives Stephan Sorger 2016; www.stephansorger.com; SQL: Commands 26 Topic 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