INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

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

Querying Data with Transact SQL

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

Relational Database Management Systems for Epidemiologists: SQL Part II

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

SQL Data Query Language

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

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

Database Management Systems,

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

Microsoft Power Tools for Data Analysis #10 Power BI M Code: Helper Table to Calculate MAT By Month & Product. Notes from Video:

Querying Data with Transact-SQL

CS1100: Data, Databases, and Queries QUERY CONSTRUCTION. CS1100 Microsoft Access 1

Teradata SQL Features Overview Version

MySQL Workshop. Scott D. Anderson

SQL CHEAT SHEET. created by Tomi Mester

This course is aimed at those who need to extract information from a relational database system.

Oracle Database 11g: SQL and PL/SQL Fundamentals

Subquery: There are basically three types of subqueries are:

MTA Database Administrator Fundamentals Course

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Unit Assessment Guide

Full file at

CSC Web Programming. Introduction to SQL

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

Principles of Data Management

DB2 SQL Class Outline

Assignment 6: SQL III Solution

SQL (Structured Query Language)

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

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

Chapter 3: Introduction to SQL

CMPT 354: Database System I. Lecture 4. SQL Advanced

Oracle Database: SQL and PL/SQL Fundamentals NEW

Advanced SQL. Chapter 8 finally!

Slicing and Dicing Data in CF and SQL: Part 1

Querying Data with Transact-SQL

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Relational Database Management Systems for Epidemiologists: SQL Part I

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

RDBMS- Day 4. Grouped results Relational algebra Joins Sub queries. In today s session we will discuss about the concept of sub queries.

Chapter 4: SQL. Basic Structure

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Silberschatz, Korth and Sudarshan See for conditions on re-use

1) Introduction to SQL

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Introduction to Computer Science and Business

Advanced SQL Tribal Data Workshop Joe Nowinski

CSE 530A SQL. Washington University Fall 2013

Sql Server Syllabus. Overview

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Greenplum SQL Class Outline

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

SQL Server 2012 Development Course

CSC 343 Winter SQL: Aggregation, Joins, and Triggers MICHAEL LIUT

Chapter 3: SQL. Chapter 3: SQL

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

5. Single-row function

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

QQ Group

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Querying Microsoft SQL Server

FUN WITH ANALYTIC FUNCTIONS UTOUG TRAINING DAYS 2017

Querying Microsoft SQL Server 2014

Querying Data with Transact-SQL (20761)

CSCD43: Database Systems Technology. Lecture 4

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

The SELECT-FROM-WHERE Structure

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

20761 Querying Data with Transact SQL

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

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

Database Technology. Topic 3: SQL. Olaf Hartig.

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

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Querying Data with Transact-SQL

SQL Data Querying and Views

Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios:

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

The SELECT-FROM-WHERE Structure

HPCC JDBC Driver. Boca Raton Documentation Team

The SQL Guide to Pervasive PSQL. Rick F. van der Lans

Chapter 3: Introduction to SQL

Database design process

Stat Wk 3. Stat 342 Notes. Week 3, Page 1 / 71

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Database Management Systems. Chapter 5

The SQL data-definition language (DDL) allows defining :

SQL Interview Questions

Aster Data Basics Class Outline

Transcription:

INTERMEDIATE SQL GOING BEYOND THE SELECT Created by Brian Duffey

WHO I AM Brian Duffey 3 years consultant at michaels, ross, and cole 9+ years SQL user What have I used SQL for?

ROADMAP Introduction 1. Who I am 2. Roadmap 3. Basic SQL Review Working with Data 1. Removing Data 2. Bringing in Data 3. Filtering Data 4. Transforming Data Working with Objects 1. Creating Functions 2. Creating Programs 3. Creating Datasets 4. Improving Performance

ROADMAP Introduction 1. Who I am 2. Roadmap 3. Basic SQL Review Working with Data 1. Removing Data 2. Bringing in Data 3. Filtering Data 4. Transforming Data Working with Objects 1. Creating Functions 2. Creating Programs 3. Creating Datasets 4. Improving Performance

BASIC SQL REVIEW Data Insert - Create Select - Read Update - Update Delete - Delete Object Create Drop

BASIC SQL REVIEW - DATA Insert Used to add new rows to the database insert into NAMES (FIRST_NAME, LAST_NAME) values ('John', 'Smith'); into NAMES - object where data is being added (FIRST_NAME, LAST_NAME) - fields for adding data values ('John', 'Smith') - values being added

BASIC SQL REVIEW - DATA Select Used to query the database for data Read-only select * from NAMES where LAST_NAME = 'Smith' order by FIRST_NAME; * - all fields, can also be a field list from NAMES - object data is coming from where LAST_NAME = 'Smith' - filtering out data order by FIRST_NAME - sorting data by a field(s)

BASIC SQL REVIEW - DATA Update Used to modify data in one or more columns update NAMES set FIRST_NAME = 'Jane' where LAST_NAME = 'Smith'; NAMES - object being updated set FIRST_NAME = 'Jane' - updating a field(s) to a new value where LAST_NAME = 'Smith' - setting which rows to update

BASIC SQL REVIEW - DATA Delete Used to remove rows from the database delete from NAMES where LAST_NAME = 'Smith'; from NAMES - object being affected where LAST_NAME = 'Smith' - rows to delete

BASIC SQL REVIEW - OBJECTS create table MONTHS (..); Create Used to add a new object to the database table - type of object to create MONTHS - name of object (..) - options for object

BASIC SQL REVIEW - OBJECTS drop table MONTHS; Drop Used to remove an object from the database table - type of object to remove MONTHS - name of object

BASIC SQL REVIEW Labeling To simplify queries, you can rename parts of it For instance, to rename a table, I can just put some identifier after it, like below Fields can also be renamed, by using the AS command select * from NAMES A; select LAST_NAME as SURNAME from NAMES;

ROADMAP Introduction 1. Who I am 2. Roadmap 3. Basic SQL Review Working with Data 1. Removing Data 2. Bringing in Data 3. Filtering Data 4. Transforming Data Working with Objects 1. Creating Functions 2. Creating Programs 3. Creating Datasets 4. Improving Performance

WORKING WITH DATA REMOVING REPEATED DATA Sometimes a data set has data that is repeated. For instance, when trying to get a list of all customers who ordered in a time period. select CUSTOMER from SALES where YEAR = 2013 order by CUSTOMER; The above will return every line of sales in 2013, meaning a customer could be in there zero, one, or many times!

WORKING WITH DATA REMOVING REPEATED DATA Instead, we can use a DISTINCT command select distinct CUSTOMER from SALES where YEAR = 2013 order by CUSTOMER; This returns results where no row is duplicated All returned values are considered select distinct CUSTOMER, ORDER_DATE, PRICE*AMOUNT from SALES order by CUSTOMER;

WORKING WITH DATA REMOVING REPEATED DATA For specific values, as well as aggregation, we can use a GROUP BY command select CUSTOMER from SALES group by CUSTOMER order by CUSTOMER; The above will return one line per customer, just like the distinct statement select CUSTOMER, max(order_date), sum(price*amount) from SALES group by CUSTOMER order by CUSTOMER; The above will still return one line per customer. Additionally it will show the last order date, the last ORDER_DATE, as well as the total sales of all orders. Aggregation (MIN, MAX, SUM, AVG, COUNT) can be done with or without GROUP BY

WORKING WITH DATA BRINGING IN ADDITIONAL DATA Sometimes a data set is missing information. For instance, needing to get a customer's state select * from SALES where YEAR = 2013; The above will return every field in SALES, however there is no state field in this table.

WORKING WITH DATA BRINGING IN ADDITIONAL DATA In order to grab data from a different table, we can do a JOIN select * from SALES A join CUSTOMERS B on A.CUSTOMER = B.CUSTOMER where YEAR = 2013; The above will return every field in SALES as well as CUSTOMERS

WORKING WITH DATA BRINGING IN ADDITIONAL DATA There are several types of joins: INNER JOIN LEFT/RIGHT OUTER JOIN FULL OUTER JOIN CROSS JOIN Exception joining UNION (ALL)

WORKING WITH DATA BRINGING IN ADDITIONAL DATA There are several types of joins: INNER JOIN

WORKING WITH DATA BRINGING IN ADDITIONAL DATA There are several types of joins: LEFT/RIGHT OUTER JOIN

WORKING WITH DATA BRINGING IN ADDITIONAL DATA There are several types of joins: FULL OUTER JOIN

WORKING WITH DATA BRINGING IN ADDITIONAL DATA There are several types of joins: CROSS JOIN

WORKING WITH DATA BRINGING IN ADDITIONAL DATA There are several types of joins: Exception joining

WORKING WITH DATA BRINGING IN ADDITIONAL DATA There are several types of joins: UNION (ALL)

WORKING WITH DATA BRINGING IN ADDITIONAL DATA What if we need data outside of the current data set? For instance, we need a breakdown of number of orders for a customer last year, plus their last order date. select CUSTOMER, sum(1), max(order_date) from SALES where YEAR = 2013 group by CUSTOMER; The above will not work because the records are limited to 2013, meaning any orders placed in 2014 are excluded.

WORKING WITH DATA BRINGING IN ADDITIONAL DATA To fix, we can use a sub-query select CUSTOMER, sum(1), (select max(b.order_date) from SALES B where B.CUSTOMER = A.CUSTOMER) from SALES A where YEAR = 2013 group by CUSTOMER; The above will still load all orders from 2013, however the subquery will go out and find the last order date for a customer.

WORKING WITH DATA BRINGING IN ADDITIONAL DATA Sub-queries are great for combining unrelated data They can be used anywhere within the query, such as in the WHERE clause

WORKING WITH DATA FILTERING UNWANTED DATA The WHERE clause is very useful for selecting on the desired data select * from SALES where AMOUNT < 20; Filter on any field in the data set, or in a different related set (subquery), using boolean operators: =!=, <> >, >= <, <= IS NULL, IS NOT NULL

WORKING WITH DATA FILTERING UNWANTED DATA There are also many other useful filters: IN select * from CUSTOMERS where STATE in ('FL', 'IL'); BETWEEN select * from SALES where AMOUNT between 10 and 20; LIKE select * from CUSTOMERS where CUSTOMER like 'A%';

WORKING WITH DATA FILTERING UNWANTED DATA There may be times we need to filter aggregated data select CUSTOMER, sum(1) from SALES where sum(1) > 500 group by CUSTOMER; The above will fail with an error as the WHERE clause can only filter raw data, not the aggregate Instead, use a HAVING clause, which is performed after the GROUP BY: select CUSTOMER, sum(1) from SALES group by CUSTOMER having sum(1) > 500;

TRANSFORMING DATA MAKING CONDITIONAL CHANGES The CASE statement is very useful for changing values select AMOUNT, case when AMOUNT > 20 then 'Good' else 'Bad' end from SALES where YEAR = 2013; You can have as many cases as you need, and everything is put into one column for easy reference The above breaks the AMOUNT field down into Good or Bad values

TRANSFORMING DATA USING DATABASE FUNCTIONS There are many built-in functions each database supports Some of the most common ones include things like: Substring - getting a section of a string Concatenation - joining two strings together Casting - changing a value from one data type to another Date-related functions - Getting the year of a date, number of days between two dates, etc.

ROADMAP Introduction 1. Who I am 2. Roadmap 3. Basic SQL Review Working with Data 1. Removing Data 2. Bringing in Data 3. Filtering Data 4. Transforming Data Working with Objects 1. Creating Functions 2. Creating Programs 3. Creating Datasets 4. Improving Performance

WORKING WITH OBJECTS USER DEFINED FUNCTIONS While each database has their own set of functions, there may be times when you have other needs The database will let you create a function to handle your own values You can pass in any number of values, do something with them, and then return one value

WORKING WITH OBJECTS create function ADD1 (NUMBER int) returns int begin return NUMBER + 1; end USER DEFINED FUNCTIONS The above creates a UDF called ADD1, which simply adds one to some number This function takes in one parameter, of type int(eger) It returns an int value All code happens in the begin..end section

WORKING WITH OBJECTS USER DEFINED FUNCTIONS create function GETSTATE (CUST char(25)) returns char(2) begin declare ST char(2); select STATE into ST from CUSTOMERS where CUSTOMER = CUST; return ST; end The above creates a UDF called GETSTATE This function takes in one parameter, the customer name It returns the state This function uses a query to grab the state for a customer

WORKING WITH OBJECTS USER DEFINED FUNCTIONS Use these functions like a field value The returned value is displayed select AMOUNT, ADD1(AMOUNT) from SALES; select CUSTOMER, STATE, GETSTATE(CUSTOMER) from CUSTOMERS;

WORKING WITH OBJECTS STORED PROCEDURES There will be times when you need a program Stored procedures differ from functions in a couple ways: They do not return any value They cannot be called from a query Their parameters can be modified

WORKING WITH OBJECTS STORED PROCEDURES create procedure CHANGEAMOUNT (in VAL int) begin update SALES set AMOUNT = AMOUNT + VAL; end The above program simply adds some amount to the AMOUNT field Parameters can be IN, OUT, or INOUT

WORKING WITH OBJECTS STORED PROCEDURES Procedures are called Usually this is done from some program, i.e. Java Can also be done from the database/command line: call CHANGEAMOUNT(1);

WORKING WITH OBJECTS CUSTOM DATA SETS When selecting data, the FROM clause is generally a table However, you can use a sub-query to SELECT from: select CUSTOMER, CUSTOMER_NUMBER, STATE from CUSTOMERS where CUSTOMER like 'A%'; The above returns a data set of all customers that have a name starting with 'A' We can now further select within this data set: select * from ( select CUSTOMER, CUSTOMER_NUMBER, STATE from CUSTOMERS where CUSTOMER like 'A%' ) A where STATE = 'OK';

WORKING WITH OBJECTS CUSTOM DATA SETS Alternatively, we can make this data set more "permanent" Views are dynamic data sets based upon some query create view A_CUSTOMERS as select CUSTOMER, CUSTOMER_NUMBER, STATE from CUSTOMERS where CUSTOMER like 'A%'; The above creates an object that stores all rows in CUSTOMERS that have a name starting with 'A' This can then be used like a table: select * from A_CUSTOMERS where STATE = 'OK';

WORKING WITH OBJECTS IMPROVING PERFORMANCE The less rows/columns selected, the quicker the query will run Use WHERE and HAVING clauses to limit irrelevant data Use INNER JOINs to only select matching data Don't use * when you only need a few fields

WORKING WITH OBJECTS IMPROVING PERFORMANCE Second, after first optimizing your query, try indexes Indexes are like a table of contents for your database Types of indexes: UNIQUE Covering Clustered Sample index: create index MY_INDEX on NAMES(FIRST_NAME, LAST_NAME); Covering: select FIRST_NAME, LAST_NAME from NAMES; Clustered: select FIRST_NAME, LAST_NAME, AGE from NAMES;

WORKING WITH OBJECTS IMPROVING PERFORMANCE So, why not create a bunch of indexes? Most tables won't have every column selected on All non-read statements become much slower, i.e. insert/update/delete Indexes take up disk space and memory Instead, use database tools like EXPLAIN to help you optimize your query and build the proper indexes

LINKS My information: www.mrc-productivity.com/services/brian_duffey.html Slides: www.mrcproductivity.com/duffey/slides/intermediatesql.html Other resources: www.mrc-productivity.com/duffey/common14.html MySQL DBVisualizer

CREDITS http://www.dbvis.com/ http://blog.codinghorror.com/a-visual-explanation-of-sqljoins/ http://www.sitepoint.com/using-explain-to-write-bettermysql-queries/