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);