美文网首页数据库
黑猴子的家:mysql 子查询练习题

黑猴子的家:mysql 子查询练习题

作者: 黑猴子的家 | 来源:发表于2019-02-27 08:44 被阅读9次
    1、查询和Zlotkey相同部门的员工姓名和工资
    SELECT last_name,salary 
    FROM employees
    WHERE department_id=(
        SELECT department_id
        FROM employees
        WHERE last_name='Zlotkey'
    )
    
    2、查询工资比公司平均工资高的员工的员工号,姓名和工资。

    (1)查询公司平均工资

    SELECT AVG(salary) FROM employees
    

    (2)查询工资>①的员工的员工号,姓名和工资

    SELECT employee_id,last_name,salary
    FROM employees 
    WHERE salary>(
        SELECT AVG(salary) FROM employees
    )
    
    3、查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资

    (1)查询各部门的平均工资

    SELECT department_id,AVG(salary)
    FROM employees
    GROUP BY department_id
    

    (2)查询员工的员工号, 姓名和工资,满足本部门并且工资>①

    SELECT salary,last_name,employee_id,e.department_id
    FROM employees e,
    (
    
        SELECT department_id,AVG(salary) ag
        FROM employees
        GROUP BY department_id
    ) av_dep
    WHERE e.department_id = av_dep.department_id
    AND e.salary>av_dep.ag;
    
    4、查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名

    (1)查询姓名中包含字母u的员工的部门

    SELECT department_id
    FROM employees
    WHERE last_name LIKE '%u%'
    

    (2)部门=①的员工的员工号和姓名

    SELECT employee_id,last_name
    FROM employees
    WHERE department_id IN(
        SELECT department_id
        FROM employees
        WHERE last_name LIKE '%u%'
    );
    
    5、查询在部门的location_id为1700的部门工作的员工的员工号

    (1)查询loaction_id =1700的部门编号

    SELECT  department_id
    FROM departments
    WHERE location_id =1700
    

    (2)查询员工号,满足部门号=①

    SELECT employee_id
    FROM employees 
    WHERE department_id IN(
        SELECT  department_id
        FROM departments
        WHERE location_id =1700
    );
    
    6、查询管理者是King的员工姓名和工资

    (1)查询员工名是king的编号

    SELECT employee_id
    FROM employees
    WHERE last_name = 'King'
    

    (2)查询员工姓名和工资,领导的编号=①

    SELECT last_name,salary
    FROM employees
    WHERE manager_id IN(
        SELECT employee_id
        FROM employees
        WHERE last_name = 'King'
    );
    
    7、查询工资最高的员工的姓名,要求first_name和last_name显示为一列,列名为 姓.名

    (1)查询最高工资

    SELECT MAX(salary) FROM employees
    

    (2)查询姓名,工资=①

    SELECT CONCAT(first_name,'.',last_name) "姓.名"
    FROM employees
    WHERE salary=(
        SELECT MAX(salary) FROM employees
    );
    

    相关文章

      网友评论

        本文标题:黑猴子的家:mysql 子查询练习题

        本文链接:https://www.haomeiwen.com/subject/hfmayqtx.html