Single-row and multiline subqueries
Single-row and multiline subqueries
--Subquery (single-row subquery, multiline subquery)--Query the information of employees with higher salary than "CLARK"? What is the salary of--[1] ' CLARK '? SELECT SAL from EMP WHERE ename = ' CLARK '; --2450--[2] 2450 higher employee information SELECT * from EMP WHERE SAL >2450; ----subquery:--use the result of one query as a condition of another query using the SELECT * from emp where SAL > (SELECT SAL from emp where ename = ' CLARK '); Syntax: Select field column name from table name WHERE condition Comparer (SELECT * from table name); --Features:--subqueries are executed one time before the main query--the result of a subquery using the query--"1" subquery can be used as a condition for another query--query the name and salary of employees above average salary--[1] average salary SE Lect AVG (SAL) from EMP; Select Ename, sal from emp WHERE sal > (SELECT AVG (SAL) from EMP) ORDER by Sal; -The name and salary of the employee who is in the same department as Scott and who is lower than his salary-which department is ' SCOTT '? SELECT DEPTNO from EMP WHERE ename = ' SCOTT '; --20--' SCOTT ' 's salary? SELECT SAL from EMP WHERE ename = ' SCOTT '; --3000 Select Ename,sal from emp where DEPTNO = (SELECT DEPTNO from emp where ename = ' SC OTT ') and sal< (SELECT SAL from EMP WHERE ENAME = ' SCOTT '); --NOTE: The subquery cannot have more than one field, only one--the "2" subquery can be used as the value of the INSERT statement--A new employee today, insert into EMP with a department of ' SCOTT ' (EMPNO, ename,deptno,hiredate) VALUES (1001, ' Success on Code ', (SELECT DEPTNO from EMP WHERE ename = ' SCOTT '), sysdate); SELECT * from EMP; ---"3" subquery can be used as a condition of update or as a modified value--Modify the work of ' new employee ' to work the same as ' SMITH '--[1]smith SELECT job from EMP WHERE ename = ' SMIT H '; UPDATE EMP SET job = (SELECT job from EMP where ename = ' SMITH ') where ename = ' success '; --Add 1000 dollars to employees who are lower than Ford's salary--[1] ' Ford ' is the pay of SELECT SAL from EMP WHERE ename = ' Ford '; UPDATE EMP SET Sal =sal+1000 where sal< (SELECT SAL from EMP where ename = ' FORD '); SELECT * from EMP; ---"4" subquery can be used as a condition of delete-delete emp where SAL > (SELECT sal from emp where ename = ' Ford ') is removed from the high pay of Ford; --the result of the "5" subquery can be used as a table to use SELECT EMpno as manager number, ename as manager name from EMP; --Query employee's number, name, manager name SELECT E.empno employee number, E.ename employee name, M. Manager name from EMP E, (select EMPNO as manager number, ename A S manager name from EMP) M WHERE e.mgr = M. Manager number (+); --92 Standard SELECT E.empno employee number, E.ename employee name, M. Manager name from EMP E left JOIN (SELECT EMPNO as manager number, ENAM E as manager name from EMP) M on e.mgr = M. Manager number; --99--multi-row subquery--the result of a subquery returns a multiline data--all: Comparison of all values returned by the subquery--any: compare to any of the values returned by the subquery--in: equals any one of the list--query employee information for wages below any one "clerk" position 。 --How much is the salary of the clerk job? 1300 SELECT SAL from EMP WHERE JOB = ' clerk '; SELECT MAX (SAL) from EMP WHERE JOB = ' clerk '; SELECT * from emp where SAL < (SELECT MAX (SAL) from emp where JOB = ' clerk '); SELECT * from EMP; SELECT * from emp where SAL < No (SELECT sal from emp where JOB = ' clerk '); --How much does it cost to inquire about the number of employees whose wages are higher than all ' salesman ' positions, name and salary--[1] ' salesman ' salary? SELECT SAL from EMP WHERE JOB = ' salesman '; SELECT Empno,enaMe,sal from EMP where Sal >all (SELECT sal from emp where JOB = ' salesman '); --Query the employee information in department 20 for the same position as the employee in department 10. --[2] Department 10 is what job SELECT job from EMP WHERE DEPTNO = 10; SELECT * from EMP where JOB in (the SELECT job from emp where DEPTNO = ten) and DEPTNO = 20; --query among employees those who are managers--query managers number--query the average salary level of each department--summary:--[1] Multi-table connection query--92 standard--equivalent connection query--Two tables , there is a primary foreign key relationship--a non-equivalent connection--two tables with no direct relationship between >=, <= ...--the above two queries, the table participating in the query is a lateral relationship--left outer connection, right outer join--two of the participating query Tables have primary and secondary points--[2] multi-table connection query--99 standard--cross-connect Croos join---Cartesian product--natural connection natural join--equal to the equivalent connection (provided that the two The table has the same name, requires the same name type exactly)--using connect-more than one of the two tables participating in the query, using the use of a specific column to connect--inner join .... on...where--equivalent connection --left outer link---right outer join----[3 join--Fully connected full join, single row subquery--the result of a subquery is a single row--using >=, <=,=,<,>,<>--[4] Multiline subquery-the result of a subquery is a multi-line single-With any: either, all (full), in (equivalent)
SQL single-row and multiline subqueries