--subquery Exercises/*1, sub-query (internal query) before the main query (outer query) one execution completes 2, the result of the subquery is used by the main query 3, in the query list using subqueries, can only be a single-row single-column. See Exercise 2 4, do not use the ORDER BY statement in a subquery unless you are doing top N analysis, the efficiency of the factor query is low, and the ordering consumes resources. See Exercise 3*/--Exercise 1: Query employee information for all employees with a salary greater than Clark--use the query result as a valueSELECT * fromEMPWHERESal>(SELECTSal fromEmpWHEREEname= 'CLARK')--Exercise 2: Query all employee information and total payroll--use the query result as a columnSELECTEmp.*,(SELECT SUM(SAL) fromemp) Sum of Wages fromEMP;--Exercise 3: Query the highest salary top 5--use the query result as a tableSELECT * from(SELECT * fromEmpORDER bySalDESC)WHEREROWNUM<= 5;--Exercise 4: Querying the 6th to 12th data in the employee table--using subqueries to solve problems where rownum cannot calculate greater than or equal to a valueSELECT * from(SELECTROWNUM rnum,emp.* fromEMP)WHERERnumbetween 6 and A; --recommended wording (pre-filter) SELECT * from(SELECTROWNUM rnum, EMP.* fromEmpWHEREROWNUM<= A) WHERERnum>=6; --multi-row subqueries--Exercise 1: Query all employees who are not department managers--in useSELECT * fromEMP EWHEREE.empno not inch(SELECTmanager_id fromDEPTWHEREmanager_id is not NULL)--Exercise 2: Find departmental information for all employees with no less than 3 people--clauses can be used in subqueriesSELECT * fromDeptWHEREDeptnoinch(SELECTDeptno fromEMPGROUP byDeptno having COUNT(*)>=3 ) --correlated subqueries (inside and outside interactive)--Practice: Query employee number, name, department number, salary, the sum of the department's wagesSELECTEmpno,ename,deptno,sal, (SELECT SUM(SAL) fromEMPWHEREE.deptno=deptno) Department Payroll sum fromEMP EORDER byDeptno
SQL subquery Exercise