Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Similar documents
Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Intermediate SQL: Aggregated Data, Joins and Set Operators

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

Join, Sub queries and set operators

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Retrieving Data from Multiple Tables

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Oracle9i: Advanced SQL

RETRIEVING DATA USING THE SQL SELECT STATEMENT

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

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

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

EXISTS NOT EXISTS WITH

Database Programming with SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Lab # 6. Data Manipulation Language (DML)

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Creating Other Schema Objects

Oracle Database 10g: Introduction to SQL

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

Objectives. After completing this lesson, you should be able to do the following:

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Database Programming with PL/SQL

SELF TEST. List the Capabilities of SQL SELECT Statements

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Oracle 1Z Oracle Database 11g : SQL Fundamentals I. Download Full Version :

Additional Practice Solutions

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Data Manipulation Language

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

Database Programming with PL/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,

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

CS 275 Winter 2011 Problem Set 3

Oracle Database: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Volume II Student Guide. D64258GC10 Edition 1.0 January 2010 D65028

Oracle Syllabus Course code-r10605 SQL

Oracle Database 10g: SQL Fundamentals I

Database Programming - Section 18. Instructor Guide

Querying Data with Transact SQL

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

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

Introduction to Oracle9i: SQL

Reporting Aggregated Data Using the Group Functions. Copyright 2004, Oracle. All rights reserved.

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

Querying Data with Transact-SQL

HR Database. Sample Output from TechWriter 2007 for Databases

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

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

Database Programming with SQL

SQL Structured Query Language Introduction

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

Oracle Database 11g: SQL and PL/SQL Fundamentals

Oracle Database SQL Basics

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

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

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Database Programming with PL/SQL

Monitoring and Resolving Lock Conflicts. Copyright 2004, Oracle. All rights reserved.

Database Programming with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Querying Data with Transact-SQL (20761)

KORA. RDBMS Concepts II

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Oracle Database 10g: SQL Fundamentals I

ÇALIŞMA TEST SORULARI

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

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Oracle Database 11g: SQL Fundamentals I

Denys Xavier denys org br) has a non-transferable license to use this Student Guide

Concepts of Database Management Eighth Edition. Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra

Testing Masters Technologies

Database Management Systems,

Database Programming with SQL 5-1 Conversion Functions. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

School of Computing and Information Technology. Examination Paper Autumn Session 2017

RESTRICTING AND SORTING DATA

SYSTEM CODE COURSE NAME DESCRIPTION SEM

Oracle Database 11g: SQL Fundamentals I

Database Programming with PL/SQL

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

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

Oracle Internal & Oracle Academy

CSC Web Programming. Introduction to SQL


DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

Querying Data with Transact-SQL

GIFT Department of Computing Science. CS-217: Database Systems. Lab-4 Manual. Reporting Aggregated Data using Group Functions

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Transcription:

Using the Set Operators Copyright 2004, Oracle. All rights reserved.

Objectives After completing this lesson, you should be able to do the following: Describe set operators Use a set operator to combine multiple queries into a single query Control the order of rows returned 7-2 Copyright 2004, Oracle. All rights reserved.

7-3 Copyright 2004, Oracle. All rights reserved. Set Operators A B A B UNION/UNION ALL A B INTERSECT A B MINUS

7-4 Copyright 2004, Oracle. All rights reserved. Tables Used in This Lesson The tables used in this lesson are: EMPLOYEES: Provides details regarding all current employees JOB_HISTORY: Records the details of the start date and end date of the former job, and the job identification number and department when an employee switches jobs

7-5 Copyright 2004, Oracle. All rights reserved. UNION Operator A B The UNION operator returns results from both queries after eliminating duplications.

Using the UNION Operator Display the current and previous job details of all employees. Display each employee only once. SELECT employee_id, job_id FROM employees UNION SELECT employee_id, job_id FROM job_history; 7-6 Copyright 2004, Oracle. All rights reserved.

7-7 Copyright 2004, Oracle. All rights reserved. UNION ALL Operator A B The UNION ALL operator returns results from both queries, including all duplications.

7-8 Copyright 2004, Oracle. All rights reserved. Using the UNION ALL Operator Display the current and previous departments of all employees. SELECT employee_id, job_id, department_id FROM employees UNION ALL SELECT employee_id, job_id, department_id FROM job_history ORDER BY employee_id;

7-9 Copyright 2004, Oracle. All rights reserved. INTERSECT Operator A B The INTERSECT operator returns rows that are common to both queries.

7-10 Copyright 2004, Oracle. All rights reserved. Using the INTERSECT Operator Display the employee IDs and job IDs of those employees who currently have a job title that is the same as their job title when they were initially hired (that is, they changed jobs but have now gone back to doing their original job). SELECT employee_id, job_id FROM employees INTERSECT SELECT employee_id, job_id FROM job_history;

7-11 Copyright 2004, Oracle. All rights reserved. MINUS Operator A B The MINUS operator returns rows in the first query that are not present in the second query.

7-12 Copyright 2004, Oracle. All rights reserved. MINUS Operator Display the employee IDs of those employees who have not changed their jobs even once. SELECT employee_id,job_id FROM employees MINUS SELECT employee_id,job_id FROM job_history;

Set Operator Guidelines The expressions in the SELECT lists must match in number and data type. Parentheses can be used to alter the sequence of execution. The ORDER BY clause: Can appear only at the very end of the statement Will accept the column name, aliases from the first SELECT statement, or the positional notation 7-13 Copyright 2004, Oracle. All rights reserved.

The Oracle Server and Set Operators Duplicate rows are automatically eliminated except in UNION ALL. Column names from the first query appear in the result. The output is sorted in ascending order by default except in UNION ALL. 7-14 Copyright 2004, Oracle. All rights reserved.

7-15 Copyright 2004, Oracle. All rights reserved. Matching the SELECT Statements Using the UNION operator, display the department ID, location, and hire date for all employees. SELECT department_id, TO_NUMBER(null) location, hire_date FROM employees UNION SELECT department_id, location_id, TO_DATE(null) FROM departments;

7-16 Copyright 2004, Oracle. All rights reserved. Matching the SELECT Statement: Example Using the UNION operator, display the employee ID, job ID, and salary of all employees. SELECT employee_id, job_id,salary FROM employees UNION SELECT employee_id, job_id,0 FROM job_history;

7-17 Copyright 2004, Oracle. All rights reserved. Controlling the Order of Rows Produce an English sentence using two UNION operators. COLUMN a_dummy NOPRINT SELECT 'sing' AS "My dream", 3 a_dummy FROM dual UNION SELECT 'I''d like to teach', 1 a_dummy FROM dual UNION SELECT 'the world to', 2 a_dummy FROM dual ORDER BY a_dummy;

Summary In this lesson, you should have learned how to: Use UNION to return all distinct rows Use UNION ALL to return all rows, including duplicates Use INTERSECT to return all rows that are shared by both queries Use MINUS to return all distinct rows that are selected by the first query but not by the second Use ORDER BY only at the very end of the statement 7-18 Copyright 2004, Oracle. All rights reserved.