Unit 6A. SQL: Overview and Coding SQL Commands

Similar documents
tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

L e a r n S q l select where


Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Chapter-14 SQL COMMANDS

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

CSC Web Programming. Introduction to SQL

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

SQL Commands & Mongo DB New Syllabus

Exploring Popular SQL Implementations. Query the database to retrieve the data stored therein.

SQL. Structured Query Language

Unit 27 Web Server Scripting Extended Diploma in ICT

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Lecture 1. Monday, August 25, 2014

Physical Design of Relational Databases

Almost all new data created today is digital Problem: How to organize the data and store it? Capacity Speed Life of the data Number of users

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

SQL functions fit into two broad categories: Data definition language Data manipulation language

CST272 SQL Server, SQL and the SqlDataSource Page 1

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

Exact Numeric Data Types

CHAPTER 11. Data Normalization

MySQL. Prof.Sushila Aghav

Creating the Data Layer

Database Management Systems,

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

SQL. Draft Version. Head First. A Brain-Friendly Guide. Lynn Beighley. A learner s companion to database programming using SQL

chapter 2 G ETTING I NFORMATION FROM A TABLE

(ADVANCED) DATABASE SYSTEMS (DATABASE MANAGEMENTS) PROF. DR. HASAN HÜSEYİN BALIK (6 TH WEEK)

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

Downloaded from

MySQL for Beginners Ed 3

Database Management Systems by Hanh Pham GOALS

The Structured Query Language Get Started

Christopher Lishawa. Info Final Project 12/6/14

CS143: Relational Model

Intro to Structured Query Language Part I

Database Programming with SQL

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

DATABASE MANAGEMENT SYSTEMS

MySQL Workshop. Scott D. Anderson

Lecture 07. Spring 2018 Borough of Manhattan Community College

AN INTRODUCTION TO WEB PROGRAMMING. Dr. Hossein Hakimzadeh Department of Computer and Information Sciences Indiana University South Bend, IN

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

October 29, Copyright 2012 by World Class CAD, LLC. All Rights Reserved.

Unit 1 - Chapter 4,5

How to use SQL to create a database

30. Structured Query Language (SQL)

! Define terms. ! Interpret history and role of SQL. ! Write single table queries using SQL. ! Establish referential integrity using SQL

Outline. Definitions History Basic concepts of DBMS Data Models Relational database Normalization

New Perspectives on Access Module 5: Creating Advanced Queries and Enhancing Table Design

UNIT-3 RELATIONAL DATABASE MANAGEMENT SYSTEM

DUE: CD_NUMBER TITLE PRODUCER YEAR 97 Celebrate the Day R & B Inc Holiday Tunes for All Tunes are US 2004

QUETZALANDIA.COM. 5. Data Manipulation Language

Slicing and Dicing Data in CF and SQL: Part 2

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

SQL stands for Structured Query Language. SQL is the lingua franca

Introduction to Databases and SQL

Working with Databases and Java

Getting Information from a Table

SQL Tutorial. Gerson Almeida SQL. Tutorial / 15:09 1 (24)

Chapter 1 SQL and Data

T-SQL Training: T-SQL for SQL Server for Developers

Databases (MariaDB/MySQL) CS401, Fall 2015

Downloaded from

CISC 3140 (CIS 20.2) Design & Implementation of Software Application II

A <column constraint> is a constraint that applies to a single column.

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

c r e at i N g yo u r F i r S t d ata b a S e a N d ta b l e

PDA Database Programming in PL/SQL (Oracle PL/SQL Developer Certified Associate Certification Course)

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

How to use SQL to work with a MySQL database

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Outline. Introduction to SQL. What happens when you run an SQL query? There are 6 possible clauses in a select statement. Tara Murphy and James Curran

Suggested Experience Required Exams Recommended Teradata Courses. TE Teradata 12 Basics

Chapter 9: Working with MySQL

CGS 3066: Spring 2017 SQL Reference

CS 2340 Objects and Design

Structured Query Language. ALTERing and SELECTing

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

SQL for MySQL A Beginner s Tutorial

Operating systems fundamentals - B07

Databases - Relations in Databases. (N Spadaccini 2010) Relations in Databases 1 / 16

Where is Database Management System (DBMS) being Used?

Lesson 05: How to Insert, Update, and Delete Data. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Lecture 04: SQL. Monday, April 2, 2007

Today Learning outcomes LO2

Lecture 04: SQL. Wednesday, October 4, 2006

Oracle Login Max Length Table Name 11g Column Varchar2

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Structured Query Language

CS6312 DATABASE MANAGEMENT SYSTEMS LABORATORY L T P C

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

Database Programming with SQL

Transcription:

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