美文网首页
mysql子查询案例

mysql子查询案例

作者: 弦好想断 | 来源:发表于2020-04-19 19:47 被阅读0次

查询和Zlotkey相同部门的员工姓名和工资

#①查询Zlotkey所在的部门
show databases;
use myemployees;
select department_id from employees 
where last_name = 'Zlotkey';
#②查询部门号=①的员工姓名和工资
select last_name,salary from employees 
where department_id = (
    select department_id from employees where 
    last_name = 'Zlotkey'
    );

查询工资比平均工资高的员工的员工号、姓名和工资

select avg(salary) from employees;
select employee_id,last_name,salary from employees where salary>(
    select avg(salary) from employees
);

查询各部门中工资比本部门平均工资高的员工号,姓名和工资

select avg(salary),department_id from employees group by department_id;
#连接①结果集和employees表,进行筛选
select employee_id,last_name,salary from (
    select avg(salary) ag,department_id from employees group by department_id 
    ) avg_sal
inner join employees e on e.department_id = avg_sal.department_id 
where salary > avg_sal.ag;

查询和姓名中包含字母u的员工在相同部门的员工的员工号和员工名

select distinct department_id from employees where last_name like '%u%';
select employee_id,last_name from employees where department_id in (
    select distinct department_id from employees where last_name like '%u%'
);

查询在部门的location_id=1700的部门工作的员工的员工号

select department_id from departments where location_id = 1700;
select employee_id from employees where department_id in (
    select department_id from departments where location_id =1700
);#这里的in关键字可以换成是=any()

查询管理者是King的员工姓名和工资

select employee_id from employees where last_name = 'K_ing';
select last_name,salary from employees where manager_id in (
    select employee_id from employees where last_name = 'K_ing'
);

查询工资最高的员工姓名,要求first_name和last_name 显示为一列,列名为姓.名

select max(salary) from employees;
select concat(first_name,last_name) "姓.名" from employees where salary = (
    select max(salary) from employees
);  

相关文章

  • mysql子查询案例

    查询和Zlotkey相同部门的员工姓名和工资 查询工资比平均工资高的员工的员工号、姓名和工资 查询各部门中工资比本...

  • mysql子查询经典案例

    1. 查询工资最低的员工信息: last_name, salary ①查询最低的工资 SELECT MIN(sal...

  • MySQL 子查询、内联结、外联结

    子查询MySQL 子查询版本要求:MySQL4.1引入了对子查询的支持。子查询:嵌套在其他查询语句中的查询。 示例...

  • mysql 查询

    mysql的查询、子查询及连接查询 一、mysql查询的五种子句 where(条件查询)、having(筛选)、g...

  • 【MySQL】MySQL查询——子查询

    查出本网站,最新的(goods_id最大)的商品select goods_id,goods_name,cat_id...

  • 第六章 查询性能优化(下)

    MySQL查询优化器的局限性 关联子查询 MySQL的关联子查询实现的很差,最好改成左外连接(LEFT OUTER...

  • 查询性能优化

    MySQL查询优化器的局限性 关联子查询 MySQL的子查询实现的非常糟糕,最糟糕的一类查询是where条件中包含...

  • MySql查询-子查询

    子查询 在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为...

  • MySQL--基础二

    本节总结MySQL的筛选条件,聚合与分组,子查询,连接查询。 MySQL的筛选条件 MySQL中的比较运算符: 比...

  • mysql子查询

    聚合函数 聚合函数对一组值执行计算,并返回单个值。 除了 COUNT 以外,聚合函数都会忽略空值。 聚合函数经常与...

网友评论

      本文标题:mysql子查询案例

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