Set Operators in SQL

Similar documents
Querying Data with Transact SQL

More with SQL Queries. Advanced SAS Programming

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,

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

Chapter 7. Advanced SQL. Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel

Querying Data with Transact-SQL

Chapter 3. Advanced Techniques. The advanced techniques presented in this chapter are meant to solve difficult grids.

Querying Data with Transact-SQL

Querying Data with Transact-SQL

1.2 Venn Diagrams and Partitions

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

20461: Querying Microsoft SQL Server 2014 Databases

Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Sets Fall / 31

Querying Data with Transact-SQL (20761)

Chapter 6. Planar Orientations. 6.1 Numberings of Digraphs

Topology I Test 1 Solutions October 13, 2008

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

BMI/CS 576 Fall 2015 Midterm Exam

CMSC424: Database Design. Instructor: Amol Deshpande

Microsoft Querying Data with Transact-SQL - Performance Course

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

Querying Data with Transact-SQL

BCS Level 4 Certificate in Data Analysis Tools QAN 603/0824/2

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Lab # 6. Data Manipulation Language (DML)

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

HW Graph Theory Name (andrewid) - X. 1: Draw K 7 on a torus with no edge crossings.

Querying Data with Transact-SQL

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Evaluation of Relational Operations

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

AVANTUS TRAINING PTE LTD

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Querying Microsoft SQL Server 2014

Database Principles: Fundamentals of Design, Implementation, and Management Ninth Edition Carlos Coronel, Steven Morris, and Peter Rob

20761B: QUERYING DATA WITH TRANSACT-SQL

CMPSCI 105: Lecture #12 Searching, Sorting, Joins, and Indexing PART #1: SEARCHING AND SORTING. Linear Search. Binary Search.

Figure 1.1: This is an illustration of a generic set and its elements.

Schema-less databases - Part 3

Querying Microsoft SQL Server 2008/2012

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 4

CMP-3440 Database Systems

Relational Database: The Relational Data Model; Operations on Database Relations

CompSci 516 Data Intensive Computing Systems

Outline. Query Processing Overview Algorithms for basic operations. Query optimization. Sorting Selection Join Projection

Querying Microsoft SQL Server

Chapter 3B Objectives. Relational Set Operators. Relational Set Operators. Relational Algebra Operations

Azure-persistence MARTIN MUDRA

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

COURSE OUTLINE: Querying Microsoft SQL Server

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

SQL Data Querying and Views

Querying Data with Transact-SQL

Sql Server Compare Two Tables To Find Differences

Querying Microsoft SQL Server

Database Management Systems,

Querying Data with Transact-SQL

MTA Database Administrator Fundamentals Course

5.2 E xtended Operators of R elational Algebra

Basic Data & Dynamic Query

20761 Querying Data with Transact SQL

Ian Kenny. November 28, 2017

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led

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

After completing this course, participants will be able to:

Querying Microsoft SQL Server 2012/2014

Lecture 8. Monday, September 29, 2014

Duration Level Technology Delivery Method Training Credits. Classroom ILT 5 Days Intermediate SQL Server

COUNTING AND PROBABILITY

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

The SAS interface is shown in the following screen shot:

1. Represent each of these relations on {1, 2, 3} with a matrix (with the elements of this set listed in increasing order).

9.5 Equivalence Relations

EECS 647: Introduction to Database Systems

Querying Data with Transact-SQL

Homework Set #2 Math 440 Topology Topology by J. Munkres

20461: Querying Microsoft SQL Server

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

2.2.2.Relational Database concept

Querying Microsoft SQL Server (MOC 20461C)

Unit Assessment Guide

20761C: Querying Data with Transact-SQL

Chapter 6 The Relational Algebra and Calculus

20461D: Querying Microsoft SQL Server

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

Relational Database Management Systems for Epidemiologists: SQL Part II

More on MS Access queries

Implementing Relational Operators: Selection, Projection, Join. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Relational Algebra. Procedural language Six basic operators

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Review -Chapter 4. Review -Chapter 5

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

Evaluation of relational operations

Querying Microsoft SQL Server

γ 2 γ 3 γ 1 R 2 (b) a bounded Yin set (a) an unbounded Yin set

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

This course is suitable for delegates working with all versions of SQL Server from SQL Server 2008 through to SQL Server 2016.

RELATIONAL OPERATORS #1

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

Transcription:

Set Operators in SQL

Set Operators in SQL Joins in SQL are generally concerned with creating new row/observations with columns from multiple tables, where some of the columns are expected to have matching values (and, of course, various outer joins include mismatches). Set operators, named as such since they include items like UNION and INTERSECTION, are concerned with assembling a group of rows/observations from a set of tables for a chosen column set. In most cases, the column set is presumed to be common to all tables, but that is not a requirement. The choice of operation determines the rows collected. To illustrate these operations, existing data sets will be sampled and reassembled under various conditions.

Starting Data From the FLIGHTDELAYS data, three data sets will be created: data notdelayed delayed delayedten set mysas.flightdelays if delay le 0 then output notdelayed if delay gt 0 then output delayed if delay ge 10 then output delayedten run

Starting Data From the FLIGHTDELAYS data, three data sets will be created: data notdelayed delayed delayedten set mysas.flightdelays if delay le 0 then output notdelayed if delay gt 0 then output delayed if delay ge 10 then output delayedten run

Starting Data From the FLIGHTDELAYS data, three data sets will be created: data notdelayed delayed delayedten set mysas.flightdelays if delay le 0 then output notdelayed if delay gt 0 then output delayed if delay ge 10 then output delayedten run Clearly NOTDELAYED and DELAYED are have disjoint sets of rows, as do NOTDELAYED and DELAYEDTEN

Starting Data From the FLIGHTDELAYS data, three data sets will be created: data notdelayed delayed delayedten set mysas.flightdelays if delay le 0 then output notdelayed if delay gt 0 then output delayed if delay ge 10 then output delayedten run DELAYED and DELAYEDTEN are not disjoint across their rows.

Union First, the SQL UNION operator: create table union1 as select * from notdelayed union select * from delayed quit

Union First, the SQL UNION operator: create table union1 as select * from notdelayed union select * from delayed quit The original 624 observations are reconstructed.

Union UNION does what you would expect, collects all rows from all tables, but ignores duplication: create table union2 as select * from delayedten union select * from delayed quit

Union UNION does what you would expect, collects all rows from all tables, but ignores duplication: create table union2 as select * from delayedten union select * from delayed quit This returns a table equivalent to the delayed table.

Intersection INTERSECT does with the rows what you would typically expect from its definition, collects all rows common to all tables: create table intersect1 as select * from delayedten intersect select * from delayed quit

Intersection INTERSECT does with the rows what you would typically expect from its definition, collects all rows common to all tables: create table intersect1 as select * from delayedten intersect select * from delayed quit As expected, this is a duplicate of the DELAYEDTEN data table

Intersection INTERSECT does with the rows what you would typically expect from its definition, collects all rows common to all tables: create table intersect2 as select * from notdelayed intersect select * from delayed quit

Intersection INTERSECT does with the rows what you would typically expect from its definition, collects all rows common to all tables: create table intersect2 as select * from notdelayed intersect select * from delayed quit No rows, as would be expected.

A Random Partition From the FLIGHTDELAYS data, two random samples will be created: data delay1 delay2 set mysas.flightdelays if ranuni(1) le.25 then output delay1 if ranuni(2) le.25 then output delay2 run

A Random Partition From the FLIGHTDELAYS data, two random samples will be created: data delay1 delay2 set mysas.flightdelays if ranuni(1) le.25 then output delay1 if ranuni(2) le.25 then output delay2 run What is the probability an observation is selected for both data sets?

Intersection We would expect an intersection here: create table intersect3 as select * from delay1 intersect select * from delay2 quit

Intersection We would expect an intersection here: create table intersect3 as select * from delay1 intersect select * from delay2 quit

Exception EXCEPT is another set operator that is available, it is equivalent to an intersection with a complement: create table except1 as select * from delay1 except select * from delay2 quit

Exception EXCEPT is another set operator that is available, it is equivalent to an intersection with a complement: create table except1 as select * from delay1 except select * from delay2 quit What are these 102 rows? They are the rows in DELAY1 that are not in DELAY2.

Exception EXCEPT is another set operator that is available, it is equivalent to an intersection with a complement: create table except1 as select * from delay1 except select * from delay2 quit There is an additional subtlety here, except removes duplicates from all tables before completing its operation. Except all will preserve duplicates.

In Practice, More Care Must Be Taken Suppose the data tables in question were a bit less clean: data delaya format delay 5. date mmddyy10. destination origin $3. set mysas.flightdelays if ranuni(1) le.25 keep delay date destination origin run data delayb format date mmddyy10. delay 5. origin destination $3. set mysas.flightdelays if ranuni(2) le.25 keep delay date destination origin run

Union Applying the UNION operator to these: create table try1 as select * from delaya union select * from delayb quit

Union Applying the UNION operator to these: create table try1 as select * from delaya union select * from delayb quit There appears to be a problem here.

Column Alignment For each of these operators, the alignment of the columns is positional, i.e.: column order matters. To overcome this: Be precise in the select clause/statement especially if column names do not match. Use the CORRESPONDING option. As would be expected, attempting to match a numeric and character column will produce an error.

In Practice A couple of possible solutions to the previous: create table try2 as select date, origin, destination, delay from delaya union select date, origin, destination, delay from delayb quit Or create table try3 as select * from delaya union corresponding select * from delayb quit

In Practice, More Care Must Be Taken Suppose the data tables in question were a bit less clean: data delayi format delay 5. date mmddyy10. destination origin $3. set mysas.flightdelays if ranuni(1) le.25 keep delay date destination origin run data delayii format date mmddyy10. delay 5. origin $3. set mysas.flightdelays if ranuni(2) le.25 keep delay date origin run

CORRESPONDING Corresponding will not preserve mismatched columns with union, but it will with outer union: create table try4 as select * from delayi union corresponding select * from delayii quit Outer union create table try5 as select * from delayi outer union corresponding select * from delayii quit

CORRESPONDING Corresponding will not preserve mismatched columns with union, but it will with outer union: create table try4 as select * from delayi union corresponding select * from delayii quit Outer union create table try5 as select * from delayi outer union corresponding select * from delayii quit