Subquery four (using subqueries in the SELECT clause)

Source: Internet
Author: User
Tags dname

example One, query out the number of each department, name, location, number of departments, average wage

 select   D.deptno,d.dname,d.loc, ( select  count  (empno) from  emp where  Emp.deptno =  d.deptno group  by   Deptno) con, ( select  avg  (SAL) from  emp where  Emp.deptno=  d.deptno by   Deptno) avgsal  from  Dept D 
With clause

example Two, using the WITH clause to define the data in the EMP table as a temporary table

 with  as (SELECT* from EMP) SELECT *  from E;

example three , query each department's number, name, location, department average salary, number of people

--query Each department's number, name, location, department average salary, number of people withE as(  SELECTDeptno DNO,ROUND(AVG(SAL),2) Avgsal,COUNT(empno) con fromEMPGROUP  bydeptno)SELECTD.deptno,d.dname,d.loc,e.avgsal,e.con fromDept D,eWHERED.deptno=E.dno (+);

example Four, query the highest salary of each department employee number, name, position, entry date, salary, department number, department name, display results sorted by department number

 withE as(   SELECTDeptno DNO,MAX(SAL) maxsal fromEMPGROUP  bydeptno)SELECTEm.ename,em.job,em.hiredate,em.sal,d.deptno,d.dname fromEMP em,dept d,eWHEREEm.deptno=E.dno andEm.sal=E.maxsal andD.deptno=E.dnoORDER  byEm.deptno;

Subquery four (using subqueries in the SELECT clause)

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.