SQL 基础之子查询(十一)
admin
2023-05-27 04:41:21
0

子查询:类型、语法、和注意事项


使用子查询能解决哪些问题?

SQL 基础之子查询(十一)


子查询语法:

select select_list from table where expr operator (select select_list from table);

  • 子查询(内查询)在主查询(外查询)之前执行。

  • 主查询使用子查询结果。

  • 位置:select,where,from,having


1、查询谁的工资比Abel高

select last_name, salary from employees 

where salary >

(select salary

from employees

where last_name = 'Abel');

SQL 基础之子查询(十一)


使用子查询注意事项

  • 子查询要包含在括号内。

  • 将子查询放在比较条件的右侧增强可读性(子查询可以出现在比较运算符的两侧)

  • 单行操作符对应单行子查询,多行操作符对应多行子查询


单行子查询:

– 子查询中的组函数

– HAVING 子句中的子查询


  • 只返回一行

  • 使用单行比较操作符


操作符含义
=等于
>大于
>=大于等于
<小于
<=小于等于
<>不等于


select last_name, job_id, salary from employees

where job_id  in  (select job_id from employees

where last_name like  'Taylor')

and salary in

(select salary

from employees

where last_name like 'Taylor');

SQL 基础之子查询(十一)


在子查询中使用组函数

select last_name, job_id, salary from employees where 

salary = (select min(salary) from employees);

SQL 基础之子查询(十一)



子查询中的HAVING 子句

  • 首先执行子查询

  • 向主查询中的 HAVING 子句返回结果

select department_id, min(salary)

from employees

group by department_id

having min(salary) >

(select min(salary)

from employees

where department_id = 50);

SQL 基础之子查询(十一)


多行子查询使用单行比较符,以下为错误写法

select employee_id, last_name

from employees

where salary =

(select min(salary)

from employees

group by department_id);


子查询中的空值问题

SQL 基础之子查询(十一)

select last_name, job_id from employees

where job_id =

(select job_id from employees

where last_name = 'haas');



多行子查询

– 使用 ALL 或 ANY


  • 返回多行。

  • 使用多行比较操作符。

操作符含义
IN等于列表中的任何一个值
ANY必须在=, !=, >, <, <=, >= 操作符之前使用,与列表中每个值进行比较,如果没有返回任何行,说明计算结果为FALSE
ALL必须在=, !=, >, <, <=, >=操作符之前使用,与列表中每个值进行比较,如果没有任何行返回,说明计算结果为TRUE


使用范例:

在多行子查询中使用 ANY 

select employee_id, last_name, job_id, salary

from employees  where salary < any

(select salary

from employees

where job_id = 'IT_PROG')

and job_id < > 'IT_PROG';

SQL 基础之子查询(十一)


在多行子查询中使用 ALL  操作符

select employee_id, last_name, job_id, salary

from employees

where salary < all

(select salary

from employees

where job_id = 'IT_PROG')

and job_id <> 'IT_PROG';

SQL 基础之子查询(十一)


子查询中的空值

select emp.last_name

from employees emp

where emp.employee_id not in

(select mgr.manager_id

from employees mgr);



1、HR 部门的同事想要你帮忙写一个 SQL 语句,该 SQL 语句可以传入一个值(员工的 last_name),然后返回结果是该员工同一部门同事的 last_name 和 hire_date,且要求该员工不在返回结果中。

举个例子,如果用户输入”Zlotkey”,结果就会返回除了 Zlotkey 之外的同一部门的其他同事的

last_name 和 hire_date.


select last_name,hire_date

from employees

where department_id =(select department_id from employees

where last_name= '&&enter_name')

and last_name < > '&enter_name';


2、请查询出所有高于平均工资的员工的 employee_id,last_name,salary,并将最终结果根据salary 降序排列。

select employee_id,last_name,salary 

from employees 

where salary > (select avg(salary) 

            from employees) 

order by salary;


3、请写一条 SQL 语句,要求查询出那些同一部门员工 last_name 里面包含字母”u”的员工的employee_id,last_name。

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



4、请帮助HR部门的同事查出所有部门location_id是1700的员工的last_name,department_id,job_id。

select last_name,department_id,job_id

from employees

where department_id in(select department_id

from departments

where location_id=1700);


让用户可以选择输入一个 location_id,然后输出结果。

select last_name,department_id,job_id

from employees

where department_id in(select department_id

from departments

where location_id=&enter_location);


5、请查出所有需要向 King 汇报的员工的 last_name 以及 salary

select last_name,salary,manager_id

from employees

where manager_id = (select employee_id

from employees

where last_name like 'King' and manager_id is null);


6、请查出所有是执行部(Executive)的员工的 department_id,last_name,job_id

select department_id,last_name,job_id

from employees

where department_id in(select department_id

from departments

where department_name like 'Executive');


7、请查出比 department_id 是 60 的任何员工薪水高的所有的员工的 last_name。

select department_id,last_name,salary from employees 

where salary > any 

(select salary from employees

where department_id=60);


8、查询所有高于平均工资,并且同一部门员工 last_name 里面包含字母”u”的员工的 employee_id,last_name,salary。

select employee_id,last_name,salary

from employees

where department_id in(select department_id

from employees

where last_name like '%u%')

and salary > (select avg(salary) from employees);


相关内容

热门资讯

伊朗称与阿曼就霍尔木兹海峡航运... 据凤凰卫视报道,伊朗外交部发言人巴加埃5日表示,伊朗与阿曼已就霍尔木兹海峡拟定航道的地理坐标达成一致...
予非知识图谱大模型完成生成式人... 近日,山东省网信办正式公布生成式人工智能服务备案名录,潍坊华创信息技术有限公司自主研发的“予非知识图...
学习语丨人工智能应该是造福全人... 近日,习近平总书记向2025世界智能产业博览会致贺信,指出:“人工智能应该是造福全人类的国际公共产品...
2026年交易全链路可追溯的智... 2026-08-05 12:10:48 作者:狼叫兽 2026年被视作智能体支付规模化落地的关键之年...
百余名中外专家齐聚海西 共话人... 8月4日,人工智能赋能的天文学开放科学会议(第二届)在青海省海西州盛大启幕。来自中国、美国、英国、法...
“私自评大师的机构”,也被撤了 曾经披着“大师”外衣四处走穴的人,正在纷纷退场。2026年以来,多个行业协会宣布撤销此前授予的“大师...
2026南昌家用呼吸机坏了去哪... 家用无创呼吸机属于精密二类医疗器械,长期昼夜连续运行,随着使用时间推移,有可能出现报警、噪音变大、无...
演习首日发生意外事故:台空军一... 【环球网报道 记者 徐思琦】据中时新闻网、中天新闻网等台媒8月5日报道,台军所谓“汉光42号演习”首...
奥运冠军陆春龙挂职担任苏州大学... 澎湃新闻记者 岳怀让近日,苏州大学体育学院官网“现任领导”栏目更新信息显示,陆春龙已经挂职担任苏州大...
我国科研人员研发亚10毫秒级神... 来源:环球网 【环球网科技综合报道】8月5日消息,据《科学》(Science)报道称,近日,中国科学...