Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Set Operators:

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

Database Fundamentals
© Abdou Illia MIS Spring 2014
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Self-joins,
Microsoft Enterprise Consortium Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Database Fundamentals Defining.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Create Student-Team.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals The Data.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Tables Lesson 6. Skills Matrix Tables Tables store data. Tables are relational –They store data organized as row and columns. –Data can be retrieved.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Evaluating.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Greenhouse Database.
Microsoft ® Office Outlook ® 2007 Training Manage your mailbox III: Move or copy messages to Personal Folders Doña Ana Community College presents:
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Type II (Correlated)
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Using two.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SQL Server.
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
Microsoft ® Office Access ® 2007 Training Build a database I: Design tables for a new Access database ICT Staff Development presents:
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Today Collection of unrelated stuff Questions? HW –4 -5 & drop lowest –emphasize project Projects Functional Dependencies? –Someone asked why: Keys, Normal.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Test.
MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.
Relational Algebra References: Databases Illuminated by Catherine Ricardo, published by Jones and Bartlett in 2004 Fundamentals of Relational Databases.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Nested aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregating.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output.
Introduction to Excel The Basics of Microsoft Word 2007 Excel.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Logical operators.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL In-Line Subquery.
1 PivotTables and Pivot Charts Cookie Setton for lesson downloads.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Equi-Join One-sided.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Presentation by: Tim Sablik.  There are two basic types of operations that will be addressed:  Unary operations are applied to only one relation. 
Backing Up and Restoring Databases by Using the SQL Server 2000.
Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals WHERE clause.
1 Connecting to a Database Server. 2 We all have accounts, with a single database each, on a Microsoft SQL Server on the USF network: allman.forest.usf.edu.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL The relational.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Understanding Core Database Concepts Lesson 1. Objectives.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Using the Set Operators
20761A 11: Using Set Operators Module 11   Using Set Operators.
Dumps4Download Oracle 1z0-051 Exam Dumps - Oracle 1z0-051 Exam Dumps Questions
Automating SQL Server Management
04 | Using Set Operators Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
20761B 12: Using Set Operators Module 12   Using Set Operators.
Using the Set Operators
Microsoft Enterprise Consortium Type I Subquery
Database Fundamentals
SQL Fundamentals in Three Hours
Data Manipulation using Relational Algebra
CMPT 354: Database System I
Topic 12 Lesson 2 – Retrieving Data with Queries
SQL set operators and modifiers.
Using the Set Operators
Presentation transcript:

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Set Operators: UNION, INTERSECT, EXCEPT 1 Microsoft Enterprise Consortium Microsoft Enterprise Consortium: Microsoft Faculty Connection/Faculty Resource Center

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What you’ll need … Log in to MEC for this lesson and into MSSMS (Microsoft SQL Server Management Studio). ◦Be sure to select your account ID under Database in the Object Explorer pane, similar to the example shown here. If there is something besides the topics for this preentation that you’re not familiar with, please review earlier lessons in this SQL Fundamental series. 2

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SET OPERATORS There are 3 set operator commands: ◦UNION ◦INTERSECT ◦EXCEPT UNION combines two or more data sets. 3 AB + A and B =

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Set Operators: UNION UNION: If the data sets overlap (have common data), the duplicates are removed. UNION compatible rule: This rule applies to all 3 set operators. ◦The # of columns must be the same in both data sets. ◦The column data types must be in the same sequence and compatible. Example: Column 2 in both data sets must be have the same data type. 4 A B + A and B =

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Set Operators: INTERSECT INTERSECT: If the data sets overlap, only the overlap is in the result. 5 A B + =

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Set Operators: EXCEPT EXCEPT: If the data sets overlap, only the portion not in common with the second data set is in the result. 6 A B + =

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium UNION example – p1 List students who got 90 or higher on their CONTRIBUTE evaluation item (they were evaluatees). select stdid, stdfname, stdlname, eval_item_ID, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'CONTRIBUTE' and score >= 90; The output has 14 rows. 7

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium UNION example – p2 List students who got 90 or higher on their RELIABLE evaluation item (they were evaluatees). select stdid, stdfname, stdlname, eval_item_ID, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'RELIABLE' and score >= 90; The output has 11 rows. 8

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium UNION example – p3 UNION the lists to combine the output. select stdid, stdfname, stdlname, eval_item_ID, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'CONTRIBUTE' and score >= 90 UNION select stdid, stdfname, stdlname, eval_item_ID, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'RELIABLE' and score >= 90; 9

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium UNION example – p4 Remove the evaluation item ID column. This will reduce the list because some students were listed twice—once for CONTRIBUTE and once for RELIABLE. The output drops to 20 rows. select stdid, stdfname, stdlname, eval_item_ID, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'CONTRIBUTE' and score >= 90 UNION select stdid, stdfname, stdlname, eval_item_ID, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'RELIABLE' and score >= 90; 10

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium INTERSECT example Change UNION to INTERSECT. Now, we see which students got 90 or higher in both CONTRIBUTE and RELIABLE> select stdid, stdfname, stdlname, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'CONTRIBUTE' and score >= 90 INTERSECT select stdid, stdfname, stdlname, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'RELIABLE' and score >= 90; 11

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium EXCEPT example Now, use EXCEPT. We see which students got 90 or higher for CONTRIBUTE but didn’t for RELIABLE. select stdid, stdfname, stdlname, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'CONTRIBUTE' and score >= 90 EXCEPT select stdid, stdfname, stdlname, score from students, evaluations, eval_items_scores where stdid = evaluateeID and evaluations.eval_ID = eval_items_scores.eval_ID and eval_item_ID = 'RELIABLE' and score >= 90; 12

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What was covered … Set Operators ◦UNION ◦INTERSECT ◦EXCEPT UNION compatible rule 13

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Resources Microsoft Faculty Connection—Faculty Resource Center Microsoft Transact-SQL Reference AdventureWorks Sample Database 14