Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]

Slides:



Advertisements
Similar presentations
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated 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.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Oracle Database Administration Lecture 2 SQL language.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
CSCI N311: Oracle Database Programming 4-1 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. Subqueries in the where clause. Anti-Joins.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
1 Ch. 11: Grouping Things Together  ANSI standard SQL Group functions: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE  Others: 8i: GROUPING (used with CUBE.
DATABASES
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
Multiple-Column Subqueries
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Chapter 3 Introduction to SQL(3)
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Advanced Teradata SQL GLOBAL Temporary Vs VOLATILE Temporary Vs Derived tables WITH and WITH BY Special Index function Trigger Online Analytical Function.
Aggregating Data Using Group Functions
Interacting with the Oracle Server
Generalization.
Writing Correlated Subqueries
(SQL) Aggregating Data Using Group Functions
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Data warehouse Design Using Oracle
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
SQL Subquery.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]

select col_1, col_2, col_3,... col_n from table_1 set operator select col_1, col_2, col_3,... col_n from table_2 set operator select col_1, col_2, col_3,... col_n from table_n; The four set operators union, union all, intersect and minus allow to serially combine more than one select statements. Although more than one select statement will then be present, only one result set is then returned.

For the demonstration of set operators, the following test tables are created: create table table_1 ( col_1 number, col_2 varchar2(10), col_3 date ); create table create table table_2 ( col_1 number, col_2 varchar2(10), col_3 date ); create table

Then, a few values are inserted: insert into table_1 values ( 3, 'hello', to_date(' ')); to_date insert into table_1 values ( 42, 'galaxy', to_date(' ')); to_date insert into table_1 values (100, 'bye', to_date(' ')); to_date insert into table_2 values ( 3, 'bye', to_date(' ')); to_date insert into table_2 values ( 42, 'galaxy', to_date(' ')); to_date insert into table_2 values ( 60, 'bye', to_date(' ')); to_date insert into table_2 values ( 3, 'hello', to_date(' ')); to_date

SUBQUERIES A query nested within a query is known as subquery. For example, you want to see all the employees whose salary is above average salary. For this you have to first compute the average salary using AVG function and then compare employees salaries with this computed salary. This is possible using subquery. Here the sub query will first compute the average salary and then main query will execute. Select * from emp where sal > (select avg(sal) from emp); Similarly we want to see the name and empno of that employee whose salary is maximum. Select * from emp where sal = (select max(sal) from emp); To see second maximum salary Select max(sal) from emp where sal <(select max(sal) from emp);

Similarly to see the Third highest salary. Select max(sal) from emp where sal < (select max(sal) from emp Where sal < (select max(sal) from emp)); We want to see how many employees are there whose salary is above average. Select count(*) from emp where sal > (select avg(sal) from emp);

We want to see those employees who are working in Hyderabad. Remember emp and dept are joined on deptno and city column is in the dept table. Assuming that wherever the department is located the employee is working in that city. Select * from emp where deptno in (select deptno from dept where city=’HYD’); You can also use subquery in FROM clause of SELECT statement. For example the following query returns the top 5 salaries from employees table. Select sal from (select sal from emp order sal desc) where rownum <= 5;

To see the sum salary deptwise you can give the following query. Select sum(sal) from emp group by deptno; Now to see the average total salary deptwise you can give a sub query in FROM clause. select avg(depttotal) from (select sum(sal) as depttotal from emp group by deptno);

ROLLUP The ROLLUP operation in the simple_grouping_clause groups the selected rows based on the values of the first n, n-1, n-2,... 0 expressions in the GROUP BY specification, and returns a single row of summary for each group. You can use the ROLLUP operation to produce subtotal values by using it with the SUM function. When used with SUM, ROLLUP generates subtotals from the most detailed level to the grand total. Aggregate functions such as COUNT can be used to produce other kinds of superaggregates.

For example, given three expressions (n=3) in the ROLLUP clause of the simple_grouping_clause, the operation results in n+1 = 3+1 = 4 groupings. Rows grouped on the values of the first 'n' expressions are called regular rows, and the others are called superaggregate rows. The following query uses rollup operation to show sales amount product wise and year wise. To see the structure of the sales table refer to appendices. Select prod,year,sum(amt) from sales group by rollup(prod,year);

CUBE The CUBE operation in the simple_grouping_clause groups the selected rows based on the values of all possible combinations of expressions in the specification, and returns a single row of summary information for each group. You can use the CUBE operation to produce cross-tabulation values. For example, given three expressions (n=3) in the CUBE clause of the simple_grouping_clause, the operation results in 2 n = 2 3 = 8 groupings. Rows grouped on the values of 'n' expressions are called regular rows, and the rest are called superaggregate rows. The following query uses CUBE operation to show sales amount product wise and year wise. To see the structure of the sales table refer to appendices. Select prod,year,sum(amt) from sales group by CUBE(prod,year);