w3resource_MySQL练习:Subquery
w3resource_MySQL练习题:Subquery
-- 要点:where里select select first_name, last_name, salary from employees where salary>( select salary from employees where last_name='Bull' )
-- 要点:直接where筛选 select first_name, last_name from employees where department_id='it'
-- 要点:多层嵌套查询
select first_name, last_name
from employees
where manager_id in ( -- 通过employees表获得manager_id
select employee_id FROM employees
where department_id in ( -- 通过departments表获得在USA的department_id
select department_id from departments
where location_id in ( -- 通过locations表获得在USA的location_id
select location_id from locations
where country_id='US'
)
)
)
-- 要点:在where中传入manager_id select first_name, last_name from employees where employee_id in ( select distinct manager_id from employees )
-- 要点:where
select first_name, last_name, salary
from employees
where salary>(
select avg(salary) from employees
)
-- 方法1:使用where嵌套查询
select e.first_name, e.last_name, e.salary
from employees as e
where e.salary=(
select j.min_salary from jobs as j where j.job_id=e.job_id
)
-- 方法2:使用多表连接(from连接多张表)
select e.first_name, e.last_name, e.salary
from employees as e, jobs as j
where j.job_id=e.job_id
and e.salary=j.min_salary
-- 要点:where select first_name, last_name, salary from employees where salary>(avg salary from employees) and department_id in (select distinct department_id from departments where department_name like 'IT%')
-- 要点:where select first_name, last_name, salary from employees where salary>(select salary from employees where last_name='Bell')
-- 要点:where + min() select first_name, last_name, salary from employees where salary=(select min(salary) from employees)
-- 要点:where + min() select first_name, last_name, salary from employees where salary>(select avg(salary) from employees)
-- 要点:where
select first_name, last_name, salary
from employees
where salary>(
select max(salary) from employees where job_id='SH_CLERK'
)
order by salary asc
-- 要点:where + not in
select first_name, last_name
from employees
where employee_id not in (
select manager_id from employees
)
-- 1. 多表连接 select employee_id, first_name, last_name, department_name from employees, departments where employees.department_id=departments.department_id -- 2. select内筛选 SELECT employee_id, first_name, last_name, (SELECT department_name FROM departments d WHERE e.department_id = d.department_id) department FROM employees e ORDER BY department;
-- 要点:where
select employee_id, first_name, last_name, salary
from employees e
where salary>(
select avg(salary) from employees e2 where e1.department_id = e2.department_id
)
-- 要点:判断奇偶数,使用%2进行取余 select * from employees where (employee_id%2<>0)
-- 1. 首先按salary进行降序排列取前五行,然后顺序排列取第一行
select *
from (select * from employees order by salary desc limit 5)
order by salary
limit 1
-- 2. where内做筛选
SELECT DISTINCT salary
FROM employees e1
WHERE 5 = (
SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary >= e1.salary
);
-- 1. 首先按salary进行排列取前四行,然后顺序排列取第一行
select *
from (select * from employees order by salary asc limit 4)
order by salary
limit 1
-- 2. where内做筛选
SELECT DISTINCT salary
FROM employees e1
WHERE 4 = (
SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary >= e1.salary
);
-- 要点:根据employee_id进行排序 select * from employees order by employee_id desc limit 10
-- 要点:通过department_id进行判断 select department_id, department_name from departments where department_id not in ( select distinct department_id from employees )
-- 要点:limit select * from employees order by salary desc limit 3
-- 要点:limit select * from employees order by salary asc limit 3
-- 要点:limit a, b 取数范围为:第[a+1, a+b]条记录 select * from employees order by salary limit (n-1), 1
w3resource_MySQL练习:Subquery的更多相关文章
- MySQL----This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- Oracle索引失效问题:WHERE C1='' OR C2 IN(SubQuery),并发请求时出现大量latch: cache buffers chains等待
问题描述: 项目反馈某功能响应时间很长,高峰期时系统整体响应很慢... 获取相应的AWR,问题确实比较严重,latch: cache buffers chains等待,因为这些会话SQL执行时间太长, ...
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...
- This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 解决方法
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- DEPENDENT SUBQUERY” 和 “SUBQUERY”
http://blog.163.com/li_hx/blog/static/183991413201642410122327/ mysql> CREATE TABLE t1 (a INT, b ...
- linux之SQL语句简明教程---Subquery
我们可以在一个 SQL 语句中放入另一个 SQL 语句.当我们在 WHERE 子句或 HAVING 子句中插入另一个 SQL 语句时,我们就有一个 subquery 的架构. Subquery 的作用 ...
- cakephp , the subquery (2)
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...
- cakephp , the subquery
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...
- Subquery returns more than 1 row
Subquery returns more than 1 row表示子查询返回了多行数据 例如: select * from table1 where table1.colums=(select co ...
随机推荐
- 【aspnetcore】抓取远程图片
找到要抓取的图片地址:http://i.imgur.com/8S7OaEB.jpg 抓取的步骤: 请求图片路径 获取返回的数据 将数据转换为stream 将stream转换为Image 保存Image ...
- Python中输出字体的颜色设置
1.实现过程 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关.控制字符颜色的转义序列是以ESC开头,即用\033来完成 2.书写过程 开头部分: \033[显示方式;前 ...
- Net Core 分布式微服务框架
Jimu : .Net Core 分布式微服务框架介绍 https://www.cnblogs.com/grissom007/p/9291345.html 一.前言 近些年一直浸淫在 .Net 平台做 ...
- Linux下无法挂载U盘
大概的错误信息是这样的: Error mounting /dev/sdb4 at /media/xxx/xx: Command-line`mount -t "ntfs" -o&qu ...
- nuxt实践
利用手脚架搭起来的服务端渲染实例目录结构.nuxtassets 未编译的静态资源如 LESS.SASS 或 JavaScriptcomponents 用于组织应用的 Vue.js 组件middlewa ...
- MVC View与Controller分离
新建了一个 Separate 解决方案, 如下图 Separate.UI UI层. 引用 Separate.Home Separate.Home 把Home控制器分享到 一个类库中 并引用(Sy ...
- enable assembly bind failure logging (Fusion) in .NET
今天遇到新建wcf项目编译成64位版本在64位windows上无法运气的,问题 先百度了一下如何查看程序集加载日志: Add the following values to HKEY_LOCAL_MA ...
- Kendo MVVM 数据绑定(二) Checked
Kendo MVVM 数据绑定(二) Checked Checked 绑定用在 checkbox ()或 radio button ()上.注意: checked 绑定只适用于支持 checked 的 ...
- 初学Vue.js(2.x版本)
首先肯定是打开官网查看文档了,没想到我太高估了自己,看的我头晕也不知道到底说了个啥.没办法,只能另寻他法,好在有菜鸟教程.然而我还是想多了,不稀饭一点点看下去,只想快点明白它到底说了个啥.嗯,找来找去 ...
- git remote add 用法
前一阵子,对于git remote add 的内容一直调错,现在明确一下: 这里是gitStack的用法:git remote add gitServerName http://ip/name(这里没 ...