Oracle subqueries

//The database is executing sql from right to left


. What is a subquery?
Subquery refers to a select statement embedded in other SQL statements, also called nested query


1) Single-line subquery A
single-line subquery refers to a subquery statement that returns only one row of data

? How to display all employees in the same department as SMITH
SQL> select * from emp where deptno=(select deptno from emp where ename='SMITH');


2) Multi-row subquery
Multi-row subquery refers to a subquery that returns multiple rows of data

? How to query the name, position, salary, department number of the same employee as the job in department 10
SQL> select * from emp where job in (select distinct job from emp where deptno=10);


Note: if it is a multi-row subquery Able to use '=' to use in as range

**************************************** ****************************
            Deduplication of data
******************** ************************************************


3) *** How ​​to filter the data (filter out the duplicate data) ***


Just add a distinct
SQL> select distinct job from emp where deptno=30;


4) Use the all operator in a multi-row subquery

? How to display employee name, salary and department number whose salary is higher than that of all employees in department 30

Method 1: Use the all operator
SQL> select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30 );

Method 2: Use the max() function which is more efficient
SQL> select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);

5)Use it in a multi-row subquery any operator

? How to display the name, salary and department number of the employee whose salary is higher than that of any employee in department 30

Method 1: Use the any operator
SQL> select ename, sal, deptno from emp where sal> any(select sal from emp where deptno =30);

Method 2: Use min() function
SQL> select ename,sal,deptno from emp where sal>(select min(sal) from emp where deptno=30);


6) Multi-column subquery
A single-column subquery refers to a subquery that returns only a single column and a single row of data, a multi-row subquery refers to returning a single column and multiple rows of data, all for a single column, and a multi-column subquery refers to a subquery that returns multiple columns of data. Statement

************************************************* ****************
? How to query all employees with the same department and position as SMITH
SQL> select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');

Note: The order in parentheses must be the same


Emphasize again:
group by is used to group the results of the query and have the
clause used to limit the grouping to display the results

. Use subqueries in the from clause

? How to display employee information higher than the average salary of your own department

//1. Query the average salary and department number of each department
select deptno,avg(sal) mysal from emp group by deptno;

//2. Take the above query as work one Zhang sub-table
(select deptno,avg(sal) mysal from emp group by deptno)
The meaning of a1 above is to treat the result of the above query as a table and name it a1


SQL> select a1.ename,a1.sal,a2.deptno ,a2.mysal from emp a1,(select deptno,avg(sal) mysal from emp group by deptno) a2 where a1.deptno=a2.deptno and a1.sal>a2.mysal order by deptno;


use in the from clausesubquery

here needs to be explained that when a subquery is used in the from clause, the subquery will be treated as a view, so it is also called an embedded view. When a subquery is used in the from clause, it must be given to the subquery. Query the specified alias

 

This command to create a new table with the results of the query

is a quick way to create a table:
create table mytable (id,name,sal,job,deptno) as select empno,ename,sal,job,deptno,from emp;

this kind of command The created table has the same data
as the reference table, that is, the data of the reference table is transferred to the newly created table.


Merge query (not used in practice).
Sometimes in practical applications, in order to merge the results of multiple select statements , you can use the set operator symbol union, union all, intersect, minus

1) union

This operator is used to obtain the union of two result sets. When this operator is used, duplicate rows in the result set will be automatically removed
select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job='MANAGER';

2) union all

this operator Similar to union, but it does not cancel duplicate rows and does not sort, just like the logical operator 'or'.
select ename,sal,job from emp where sal>2500 union all select ename,sal,job from emp where job='MANAGER';

3) intersect (intersection)

This operator is used to obtain the intersection of two result sets.
select ename,sal,job from emp where sal>2500 intersect select ename,sal,job from emp where job='MANAGER';

4) minus (subtract)

This operator is used to obtain the difference of two result sets, it Only the data that exists in the first set, but not the second set will be displayed
select ename,sal,job from emp where sal>2500 minus select ename,sal,job from emp where job='MANAGER';

that is, the first The data remaining in the first set after subtracting one set from the second set

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327042587&siteId=291194637