SQL subquery Exercise

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.