Lists the number of employees and department numbers in each department with wages higher than the department's average salary, sorted by department number (expressed in SQL statements).

Source: Internet
Author: User

Find out the number of employees and department numbers in each department with wages higher than the department's average salary, and sort by department number (expressed in SQL statement)

1. Create a table

2. Ideas:

(1) First, check the average salary for each department

1 Select deptid,avgas fromgroup by

-----> Search Results:

(2) query using the idea of federated query: SELECT * from Table1,table2 where Table1.name=table2.name

That is, the employee table and the above table query the results of the joint query, to find all wages more than the average wage record.

1 Select table1. *  from  as table1, 2 (select deptid,avgas fromgroupby as  table23where table1.deptid= and Table1.salary  >

----> Search results:

(3) The number of employees and department numbers that are higher than the average wage in each department are queried and sorted by department number

1 SelectTable1.deptid,Count(*) fromEmployee astable1,2(SelectDeptID,avg(Salary) asAvg_salary fromEmployeeGroup  byDeptID) astable23 whereTable1.deptid=Table2.deptid andTable1.salary>table2.avg_salary4 Group  byTable1.deptidOrder  byTable1.deptid;

-----> Search Results:

Lists the number of employees and department numbers in each department with wages higher than the department's average salary, sorted by department number (expressed in SQL statements).

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.