MySQL difficulty grammar - subqueries

Data in this chapter to explain knowledge through practice subquery, before entered, be familiar with the data table, the table data can not control, mainly familiar form field names

 

 Here subqueries divided into three parts:

  1, where the conditions Subqueries

      This sub-queries and general inquiry no difference, mainly to understand the meaning of the title, then the result set is assembled

 

        Requirements: 1. Staff name and the name of the same view as chang employees of all employees of the id and name

the SELECT the above mentioned id, last_name, title 
 from s_emp 
 the WHERE title = (# chang and staff positions as
     the SELECT title 
     from s_emp 
     the WHERE last_name = "chang" 
);

 

        Requirements: 2 View wages less than the average wage of all employees id and name

the SELECT the above mentioned id, last_name, salary
 from s_emp
 the WHERE salary < (# of employees average wage
     the SELECT  AVG (salary)
     from s_emp 
);

 

        需求:3.查看部门与员工名字为Chang的员工所在部门相同,或者与区域为2的部门相同的部门所有员工的id和 名字

select id, last_name, title, dept_id
from s_emp
where dept_id = (     # chang员工所在的部门
    select dept_id
    from s_emp
    where last_name = "chang"
) or dept_id in (       # 区域为2的部门
    select dept_id
    from s_dept
    where region_id=2
);

 

        需求:4.查看部门平均工资大于32号部门平均工资的部门id

select dept_id,salary
from s_emp
group by dept_id
    having avg(salary)>(   # 32部门的平均工资
        select avg(salary)
        from s_emp
        where dept_id = 32
    );

 

        需求:5.查看工资大于Smith所在部门平均工资的员工id和姓名

select id, last_name
from s_emp
where salary>(         # smith所在的部门的平均工资
    select avg(salary)
    from s_emp
    where dept_id = (   # smith所在的部门
        select dept_id
        from s_emp
        where last_name = "Smith"
    )
);

 

        需求:6.查看薪资高于Chang员工经理薪资的员工信息

select id, last_name, salary
from s_emp
where salary>(     #chang员工的经理的工资
    select salary
    from s_emp
    where id=(         # chang员工的经理
        select manager_id
        from s_emp
        where last_name = "chang"
    )
);

 

        需求:7.查看薪资高于(Chang员工经理的经理所在区域的)最低工资的员工的信息

select id, last_name, salary
from s_emp
where salary>(
    select salary
    from s_emp
    where dept_id in (     # chang员工的经理的经理所在的部门的所在区域的所有部门
        select id
        from s_dept
        where region_id = (     # chang员工的经理的经理所在的部门的所在区域
            select region_id
            from s_dept
            where id=(    # chang员工的经理的经理所在的部门      
                select dept_id
                from s_emp
                where id=(     # chang员工的经理的经理
                    select manager_id
                    from s_emp
                    where id=(   # chang员工的经理
                        select manager_id
                        from s_emp
                        where last_name="chang"
                    )
                )
            )
        ) 
    ) order by salary limit 0,1
);

 

        需求:8.查看所有客户负责员工的总工资

select sum(salary) 
from s_emp 
where id in (       # 员工负责的id
    select sales_rep_id 
    from s_customer   
);

 

        需求:9.查看工资大于客户负责员工最高工资的员工信息

select id, last_name 
from s_emp 
where salary>(     # 员工负责的id中工资最高的薪资
    select max(salary) 
    from s_emp 
    where id in (    # 员工负责的id
        select sales_rep_id 
        from s_customer
    )
);

 

        需求:10.查看客户负责员工中工资大于Chang员工的工资的员工信息

select id, last_name, salary
from s_emp
where id in (       # 员工负责的id
    select sales_rep_id
    from s_customer
) and salary > (        # 工资大于chang员工的工资
    select salary
    from s_emp
    where last_name="chang"
);

 

        需求:11.查看部门平均工资大于Chang所在部门平均工资的部门id

select dept_id, avg(salary)
from s_emp
group by dept_id
    having avg(salary) > (    # chang员工所在部门的平均工资
        select avg(salary)
        from s_emp
        where dept_id=(    # chang员工所在部门
            select dept_id
            from s_emp
            where last_name="chang"
        )
    );

 

        需求:12.查看Chang员工所在部门其他员工薪资总和

select sum(salary)
from s_emp
where dept_id=(         # chang员工所在的部门
    select dept_id
    from s_emp
    where last_name="chang"
) and last_name != "chang";   # 除了chang员工

 

        需求:13.查询工资大于41号部门平均工资的员工,并且该员工所在部门的平均工资也要大于41号部门的平均工资

select id,last_name,salary, dept_id
from s_emp
where salary > (             # 高于41号部门的平均工资
    select avg(salary)
    from s_emp
    where dept_id = 41
) and dept_id in (           # 高于41号部门的平均工资的部门
    select dept_id
    from s_emp
    group by dept_id
        having avg(salary)>(       # 41号部门的平均工资
            select avg(salary)
            from s_emp
            where dept_id = 41
        )
);

     2、数据集条件子查询

        需求:1.求平均薪水最高的部门的id

select dept_id 
from s_emp 
group by dept_id 
    having avg(salary) = (       # 部门平均工资表中的最大工资
        select max(max_avg)
        from (       # 部门平均工资表
            select avg(salary) as max_avg
            from s_emp
            group by dept_id
        ) as newtable
);
# 排序搜寻的部门
select dept_id
from s_emp
group by dept_id
order by avg(salary) desc
limit 0, 1;

 

        需求:2.求平均薪水最高的部门的部门名称

select id, name
from s_dept
where id=(              # 部门平均薪资表中最高的平均工资的部门
    select dept_id
    from s_emp
    group by dept_id
        having avg(salary) = (   # 部门平均薪资表中最高的平均工资
            select max(avgs)
            from (        # 部门平均薪资表
                select avg(salary) avgs
                from s_emp
                group by dept_id
            ) as newtable
        )
);
select id, name
from s_dept
where id=(
    select dept_id
    from s_emp
    group by dept_id
    order by avg(salary) desc
    limit 0, 1
);

 

Guess you like

Origin www.cnblogs.com/aitiknowledge/p/11461409.html