400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

SQL基础之子查询(十一)

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

公司主营业务:成都网站建设、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出满洲免费做网站回馈大家。

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

SQL 基础之子查询(十一)

子查询语法:

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

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 子句

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);


分享标题:SQL基础之子查询(十一)
分享URL:http://mzwzsj.com/article/pocccp.html

其他资讯

让你的专属顾问为你服务