Using SET Operators Fresher Learning Program January, 2012.

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
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.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Hierarchical Retrieval Fresher Learning Program December, 2011.
Lab 1 Writing Interactive Queries CISB514 Advanced Database Systems.
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.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Using Subqueries to Solve Queries
Restricting and Sorting Data
Basic select statement
Using the Set Operators
Using Subqueries to Solve Queries
Restricting and Sorting Data
Using the Set Operators
Restricting and Sorting Data
Manipulating Data.
Using Subqueries to Solve Queries
Restricting and Sorting Data
Using CASE Value expression
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
IST 318 Database Administration
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
Presentation transcript:

Using SET Operators Fresher Learning Program January, 2012

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Objectives of Using SET Operators  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

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Topics  Following topics will be covered  The SET Operators  Tables Used in This Lesson  The UNION Operator  Using the UNION Operator  The UNION ALL Operator  Using the UNION ALL Operator  The INTERSECT Operator  Using the INTERSECT Operator  The MINUS Operator  SET Operator Guidelines  The Oracle Server and SET Operators  Matching the SELECT Statements  Controlling the Order of Rows  Summary

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate The SET Operators INTERSECT MINUS UNION/UNION ALL A B

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Continued……  The SET Operators: The SET operators combine the results of two or more component queries into one result. Queries containing SET operators are called compound queries. OperatorReturns UNIONAll distinct rows selected by either query UNION ALLAll rows selected by either query, including all duplicates INTERSECTAll distinct rows selected by both queries MINUSAll distinct rows that are selected by the first SELECT statement and not selected in the second SELECT statement

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate 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

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Continued……  Tables Used in This Lesson Two tables are used in this lesson. They are the EMPLOYEES table and the JOB_HISTORY table. The EMPLOYEES table stores the employee details. For the human resource records, this table stores a unique identification number and address for each employee. The details of the employee’s job identification number, salary, and manager are also stored. Some of the employees earn a commission in addition to their salary; this information is tracked too. The company organizes the roles of employees into jobs. Some of the employees have been with the company for a long time and have switched to different jobs. This is monitored using the JOB_HISTORY table. When an employee switches jobs, the details of the start date and end date of the former job, the job identification number and department are recorded in the JOB_HISTORY table. The structure and the data from the EMPLOYEES and the JOB_HISTORY tables are shown on the next page.

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Continued…… There have been instances in the company of people who have held the same position more than once during their tenure with the company. For example, consider the employee Taylor, who joined the company on 24-MAR Taylor held the job title SA_REP for the period 24-MAR-98 to 31-DEC- 98 and the job title SA_MAN for the period 01-JAN-99 to 31-DEC-99. Taylor moved back into the job title of SA_REP, which is his current job title. Similarly consider the employee Whalen, who joined the company on 17-SEP Whalen held the job title AD_ASST for the period 17-SEP-87 to 17- JUN-93 and the job title AC_ACCOUNT for the period 01-JUL-94 to 31-DEC-98. Whalen moved back into the job title of AD_ASST, which is his current job title.

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Continued…  Tables Used in This Lesson (continued):DESC employees NameNull?Type EMPLOYEE_IDNOT NULLNUMBER(6) FIRST_NAMEVARCHAR2(20) LAST_NAMENOT NULLVARCHAR2(25) NOT NULLVARCHAR2(25) PHONE_NUMBERVARCHAR2(20) HIRE_DATEDATE JOB_IDNOT NULLVARCHAR2(10) SALARYNOT NULLNUMBER(8,2) COMMISSION_PCTNUMBER(2,2) MANAGER_IDNUMBER(6) DEPARTMENT_IDNUMBER() DEPARTMENT_NAMEVARCHAR2(14)

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate SELECT employee_id, last_name, job_id, hire_date, department_id FROM employees; Continued… EMPLOYEE_IDLAST_NAMEJOB_IDHIREDEPARTMENT_ID 100KingAD_PRES17-JUN KochharAD_VP21-SEP De HaanAD_VP13-JAN HunoldIT_PROG13-JAN EmstIT_PROG21-MAY LorrentzIT_PROG07-FEB MourgosST_MAN16-NOV RajsST_CLERK17-OCT DaviesST_CLERK29-JAN MaosST_CLERK15-MAR VargasST_CLERK09-JAN ZlotkeySA_MAN29-JUL AbelSA_REP11-MAY TaylorSA_REP24-MAR GrantSA_REP24-MAY WhalenAD_ASST17-SEP HarteinMK_MAN17-FEB-9620

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate The UNION Operator © 2011 Capgemini - All rights reserved10 The UNION operator returns results from both queries after eliminating duplications.  The UNION Operator: The UNION operator returns all rows selected by either query. Use the UNION operator to return all rows from multiple tables and eliminate any duplicate rows. A B

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate  Display the current and previous job details of all employees. Display each employee only once. Using the UNION Operator SELECT employee_id, job_id FROM employees SELECT employee_id, job_id FROM job_history; UNION EMPLOYEE_IDJOB_ID 100AD_PRES 101AC_ACCOUNT ………… 200AC_ACCOUNT 200AD_ASST ………… 205AC_MGR 206AC_ACCOUNT

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate The UNION ALL Operator The UNION ALL operator returns results from both queries, including all duplications. A B

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate 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;

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate The INTERSECT Operator The INTERSECT Operator: A B

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Using the INTERSECT Operator Display the employee IDs and job IDs of employees who currently have a job title that they held before beginning their tenure with the company. SELECT employee_id, job_id FROM employees INTERSECT SELECT employee_id, job_id FROM job_history; EMPLOYEE_IDJOB_ID 176SA_REP 200AD_ASST

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate The MINUS Operator The MINUS Operator: A B

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Continued… 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; EMPLOYEE_IDJOB_ID 100AD_PRES 101AD_VP 102AD_VP 103IT_PROG …………………. 201MK_MAN 202MK_REP 205AC_MGR 206AC_ACCOUNT

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate 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

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate 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.  The Oracle Server and SET Operators When a query uses SET operators, the Oracle Server eliminates duplicate rows automatically except in the case of the UNION ALL operator. The column names in the output are decided by the column list in the first SELECT statement. By default, the output is sorted in ascending order of the first column of the SELECT clause. The corresponding expressions in the select lists of the component queries of a compound query must match in number and datatype. If component queries select character data, the datatype of the return values are determined as follows: If both queries select values of datatype CHAR, the returned values have data type CHAR.

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate 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; DEPARTMENT_IDLOCATIONHIRE_DATE SEP FEB-96 ……………… JUN MAY-99

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate 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 FROM dual UNION SELECT ’the world to’, 2 FROM dual ORDER BY 2; My dream I’d like to teach The world sing

Learning & Culture All work described was performed by Capgemini or a Capgemini affiliate Summary In this lesson, you should have learned how to: Use UNION to return all distinct rows Use UNION ALL to returns all rows, including duplicates Use INTERSECT to return all rows shared by both queries Use MINUS to return all distinct rows selected by the first query but not by the second Use ORDER BY only at the very end of the statement

Thank You For Your Time