Set Operations Part VI.

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

© Abdou Illia MIS Spring 2014
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Relational Database Operators
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
MULTIPLE-TABLE QUERIES
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
Chapter 9 Joining Data from Multiple Tables
Banner and the SQL Select Statement: Part Three (Joins) Mark Holliday Department of Mathematics and Computer Science Western Carolina University 4 November.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Chapter 4 Multiple-Table Queries
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in.
Student Admissions Introduction to Reporting Instructors: Linda Atkinson and Linda Herron The BARS Support Team.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
POPULATION SELECTION CONCEPT People in Database Selection Rules or Criteria Selected Population of Individuals who Satisfy Rules.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Advanced Adhoc Reporting 2010 Visions Conference July 28, 2010.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Using SET Operators Fresher Learning Program January, 2012.
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.
Writing Basic SQL SELECT Statements Lecture
 CONACT UC:  Magnific training   
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL Query Getting to the data ……..
Practice writing SQL statements Discuss SQL UNION statement.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
SQL Creating and Managing Tables
USING Sql to make banner better
Using the Set Operators
Dustin McDaniel – Athens Technical College
Theory behind the relational engine
Theory behind the relational engine
SQL – Subqueries.
Writing Basic SQL SELECT Statements
04 | Using Set Operators Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
David M. Kroenke and David J
Using the Set Operators
CS 405G: Introduction to Database Systems
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Types of Joins Farrokh Alemi, Ph.D.
INNER JOIN INNER SPRIDEN SARADAP
SQL – Entire Select.
Writing Basic SQL SELECT Statements
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Joins and other advanced Queries
SQL set operators and modifiers.
Using the Set Operators
Manipulating Data Lesson 3.
Microsoft Access Date.
Table Joins Part II.
Presentation transcript:

Set Operations Part VI

Set Operations Allow you to combine the results of two or more queries. UNION ALL UNION MINUS INTERSECT

Requirements Will This Work? Result sets of all queries being combined must have the same number of columns The data type of each column in the 2nd and subsequent result sets must match the data type of its corresponding column in the 1st result set SELECT spriden_id ,spriden_last_name … UNION SELECT sgbstdn_pidm ,sgbstdn_term_code_eff VARCHAR2(9) VARCHAR2(60) … NUMBER(8) VARCHAR2(6) Will This Work?

UNION ALL Combines the results of multiple SELECT statements into one result set SELECT 'SHREVNT’ AS Table_Code, shrevnt_pidm AS PIDM, spriden_id AS CSU_ID, spriden_last_name || ', ' || spriden_first_name AS Name, shrevnt_levl_code AS Levl, shrevnt_desc AS Description, shrevnt_activity_date AS Activity_Date, shrevnt_orig_code AS Orig_Code, FROM shrevnt, spriden WHERE spriden_pidm = shrevnt_pidm AND spriden_change_ind IS NULL AND shrevnt_effective_date IS NULL UNION ALL SELECT 'SHRTMCM’ AS Table_Code, shrtmcm_pidm AS PIDM, spriden_id AS CSU_ID, shrtmcm_levl_code AS Levl, shrtmcm_comment AS Description, shrtmcm_activity_date AS Activity_Date, shrtmcm_orig_code AS Orig_Code, FROM shrtmcm, spriden WHERE spriden_pidm = shrtmcm_pidm AND spriden_change_ind IS NULL AND shrtmcm_effective_date IS NULL ORDER BY Table_Code, Name, CSU_ID Selects records that have null values for the effective date in tables related to academic history.

UNION Combines the result set of multiple SELECT statements into one result set, then eliminates duplicate rows from the result set

MINUS Takes the result set of the first SELECT statement and removes any rows also returned by subsequent SELECT statements Duplicate rows are eliminated -- get all person records SELECT i.spriden_pidm FROM spriden i WHERE i.spriden_entity_ind = 'P’ MINUS -- remove if application exists SELECT saradap_pidm FROM saradap -- remove if transcript comment exists SELECT shrtmcm_pidm FROM shrtmcm

INTERSECT Returns only those rows returned by each of the SELECT statements Duplicate rows are eliminated SELECT spriden_id csu_id ,spriden_last_name || ', ' || spriden_first_name name FROM sgrvetn JOIN spriden ON spriden_pidm = sgrvetn_pidm AND spriden_change_ind is null INTERSECT FROM sgrsprt ON spriden_pidm = sgrsprt_pidm ORDER BY name

Intersect Why not this instead? SELECT distinct spriden_id csu_id ,spriden_last_name || ', ' || spriden_first_name name FROM sgrvetn JOIN spriden ON spriden_pidm = sgrvetn_pidm AND spriden_change_ind is null JOIN sgrsprt ON sgrsprt_pidm = sgrvetn_pidm ORDER BY name

Rules / Restrictions Column names used in the result set are derived from the first SELECT statement ORDER BY can appear only once and it must be after the final SELECT statement Column names used in the ORDER BY must be those defined in the first SELECT statement GROUP BY and HAVING may appear in any of the SELECT statements

Questions?