首页 > 数据库 >SQL - 子查询

SQL - 子查询

时间:2023-03-25 18:33:05浏览次数:36  
标签:salary employees 查询 employee SQL department id SELECT

 

 

 

 

 

 

 

 

 

#单行子查询
#1.查询工资大于149号员工工资的员工信息
SELECT e.last_name ,e.salary 
FROM employees e 
WHERE e.salary > (
    SELECT e2.salary 
    FROM employees e2 
    WHERE e2.employee_id = 149
);

#2.返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
SELECT e.job_id ,e.salary 
FROM employees e 
WHERE e.job_id = (
    SELECT e.job_id
    FROM employees e 
    WHERE e.employee_id = 141
) 
AND e.salary = (
    SELECT e.salary 
    FROM employees e 
    WHERE e.employee_id = 143
);

#3.返回公司工资最少的员工的last_name,job_id和salary
SELECT e.last_name ,e.job_id ,e.salary 
FROM employees e 
HAVING e.salary = (
    SELECT MIN(e.salary) 
    FROM employees e 
) ;

#4.查询与141号员工的manager_id和department_id相同的其他员工
#的employee_id,manager_id,department_id
SELECT e.employee_id ,e.manager_id ,e.department_id 
FROM employees e 
WHERE e.manager_id = (
    SELECT e.manager_id
    FROM employees e 
    WHERE e.employee_id = 141
)
AND e.department_id = (
    SELECT e.department_id 
    FROM employees e 
    WHERE e.employee_id = 141
)
AND e.employee_id != 141;

#5.查询最低工资大于50号部门最低工资的部门id和其最低工资
SELECT e.department_id ,MIN(e.salary)
FROM employees e
GROUP BY e.department_id
HAVING MIN(e.salary) > (
    SELECT MIN(e.salary) 
    FROM employees e
    WHERE e.department_id = 50
);

#6.显示员工的employee_id,last_name和location
#其中若员工department_id与location_id为1800的department_id相同,
#则location为'Canada',其余则为'USA'
SELECT e.employee_id ,e.last_name ,
CASE WHEN d.department_id = (
    SELECT d.department_id 
    FROM departments d JOIN locations l ON d.location_id = l.location_id 
    WHERE l.location_id = 1800
) THEN 'Canada'
ELSE 'USA'END 'Location'
FROM employees e JOIN departments d ON e.department_id = d.department_id 
JOIN locations l ON d.location_id = l.location_id ;

 

 

 

标签:salary,employees,查询,employee,SQL,department,id,SELECT
From: https://www.cnblogs.com/hhs1998/p/17255310.html

相关文章

  • SQL优化总结
    一、查询SQL尽量不要使用select*,而是具体字段二、避免在where子句中使用or来连接条件三、尽量使用数值替代字符串类型四、使用varchar代替char五、技术延伸,char与varchar......
  • mysql
    数据库简述StructureQueryLanguage(结构化查询语言)简称SQL登录数据库与用户操作cmd打开数据库 添加用户 删除用户 查看所有用户......
  • Centos8安装MySQL
    受苦于博客总是过于老旧,安装时总会出现版本包已经删除的情况而且步骤总是各式各样难以理解,无奈之下还是官网靠谱,于是这篇博客以官网方法为基准,方便以后安装的时候可以直接......
  • mysql数据库备份与恢复
    环境:CentOS7.9mysql-5.71.数据库准备,建表createdatabaseschool;CREATETABLEstudent(idINT(10)NOTNULLUNIQUEPRIMARYKEY,nameVARCHAR(20)NOTNULL,se......
  • mysql手动实现窗口函数的方法
    背景:窗口函数是一种强大的SQL函数,它允许在查询中对一组行进行计算,而不需要将它们分组或汇总。然而,如果使用的MySQL版本不支持窗口函数,我们仍然可以通过一些技巧手动实现它......
  • es中的滚动查询
    一:简介  Elasticsearch滚动查询也叫游标查询  适合那种需要一次性或分批拉出大量数据做离线处理、迁移等。可以提升点效率。 二:实践中我使用到滚动的场景......
  • golang 实现的零依赖、高性能、并发 mysqldump 工具。
    mysqldumpgolang中实现的零依赖、高性能、并发mysqldump工具。项目地址:https://github.com/dengjiawen8955/mysqldump/blob/master/README-zh.md文章地址:https:/......
  • Oracle入门6(plsql语言)
    游标,触发器,plsql存储过程,存储函数,包plsql编程语言--普通用户需要获取编程权限grantcreateprocedureto用户名;grantexecuteanyprocedureto用户名;grant......
  • 部署SQL Server 2019 群集错误验证失败
    ##:部署WindowsCluster请参考之前相关文章。在之前文章部署群集时,我们使用无仲裁模式且未配置群集存储快速创建WinSrv群集,同时未进行群集测试验证。所以出现报错:问题描......
  • 在windows系统中设置MySQL数据库
    MySQL搭建效果图step1:下载安装包https://downloads.mysql.com/archives/community/step2:解压后即完成安装step3:创建my.ini配置文件(注意路径)[mysqld]......