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 ...
随机推荐
- Ubuntu 16.04 LTS安装Docker
一.安装Docker的先决条件 1.运行64位CPU构架的计算机(目前只能是x86_64和amd64),请注意,Docker目前不支持32位CPU.2.运行Linux 3.8或更高版本内核.一些老版本 ...
- Codeforces Round #376 (Div. 2) F. Video Cards 数学 & 暴力
http://codeforces.com/contest/731/problem/F 注意到一个事实,如果你要找一段区间中(从小到大的),有多少个数是能整除左端点L的,就是[L, R]这样.那么,很 ...
- mysql非常全的和完整的总结
(1)数据类型 类型 备注 tinyint/smallint/mediumint/int/bigint 1B/2B/3B/4B/8B float/double 单精度/双精度浮点型 decimal 不 ...
- Smarty中的请求变量和保留变量的使用范例
PHP中提供的超全局数组 Smarty中对应的请求变量 $_GET <{$smarty.get}> $_POST ...
- 洛谷P1057 传球游戏
f[i][j]表示第i轮j拿到球的方案数 转移:f[i][j]=f[i-1][j+1] +f[i-1][j+-1].注意: 边界f[0][1]=1; 还有当j=1或N时 #include<ios ...
- webpack.config.js====插件purifycss-webpack,提炼css文件
1. 安装:打包编译时,可以删除一些html中没有使用的选择器,如果html页面中没有class=a class="b"的元素,.a{}.b{}样式不会加载 cnpm instal ...
- Flat UI theme--扁平化的UI
项目地址:点击打开 支持版本: jQuery Mobile 1.3.2 使用很简单,前提是你的前端是在jquery-mobile的基础上开发的,然后导入相应的css文件.img文件和js文件即可. 案 ...
- IDEA在debug模式项目启动一半卡主,无法启动,也不报错
罪魁祸首就是手误 点了一下代码中方法的左侧打了个方法断点 Java Method Breakpoints 有时候debug启动很慢也有可能是这个原因,记录一下
- Java的API及Object类、String类、字符串缓冲区
Java 的API 1.1定义 API: Application(应用) Programming(程序) Interface(接口) Java API就是JDK中提供给开发者使用的类,这些类将底层的代 ...
- GoDaddy网站程序根目录 网站文件上传到虚拟主机哪个目录
用的linux虚拟主机,网站根目录为public_html,(window主机的目录为httpdocs)我们需要把本地做好的网站上传到此目录下 cPanel控制面板 - 文件管理器 - public_ ...