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 ...
随机推荐
- UML类图学习总结
1.首先来认识下类图?以及类图的作用 类图(Class diagram)由许多(静态)说明性的模型元素(例如类.包和它们之间的关系,这些元素和它们的内容互相连接)组成.类图可以组织在(并且属于)包中, ...
- net core (下)
net core (下) 第一部分: https://www.cnblogs.com/cgzl/p/8450179.html 本文是基于Windows10的. Debugging javascript ...
- C# 序列化与反序列化json
与合作伙伴讨论问题,说到的c++与c#数据的转换调用,正好就说到了序列化与反序列化,同样也可用于不同语言间的调用,做了基础示例,作以下整理: using System.Data; using Syst ...
- 微信支付(java版本)_统一下单
最近工作接触到微信支付,刚开始解决微信支付很神秘,接触之后发现并没有那么神秘,就是有很多坑,在开发的时候需要注意,整理出来: 1.准备工作 首先需要登录微信支付公众平台阅读接口文档,地址:https: ...
- ios中 input 焦点光标不垂直居中
笔记:在ios,如果同时给input设置这种平时我们使字体垂直居中的css写法. 光标会出现,如下图的问题 . 改正方案: 采取不使用line-height的垂直居中方法即可.
- WebStorm快捷键(Mac版)
编辑 Command+alt+T 用 (if..else, try..catch, for, etc.)包住 Command+/ 注释/取消注释的行注释 Command+alt+/ 注释/取消注释与块 ...
- VirtualBox中出现UUID have already exists ,并且数字键盘numlock效果相反
原文地址:https://www.cnblogs.com/xqzt/p/5053338.html 原因:由于linux密码登录错误,修改也报错误,所以只能重新安装虚拟机并在其中安装镜像文件,但是安装镜 ...
- Emacs中自动刷新dired缓冲区
Emacs中自动刷新dired缓冲区 在dired模式中,如果在不同buffer间切换,buffer不会自动更新,有时还需要手工按“g”键,比较麻烦,如下设置和代码能够在buffer切换和执行shel ...
- 《超实用的Node.js代码段》连载一:获取Buffer对象字节长度
我们知道Node.js框架下的Buffer对象能够对二进制数据提供很好的支持,那么获取一个Buffer对象真实的字节长度则是必须要用到的功能了.Node.js框架为开发人员提供了一个Buffer.by ...
- python-day1作业(感谢视频老师留的作业)
__author__ = 'zht' #!/usr/bin/env python # -*- coding: utf-8 -*- ''' #努力学习每一天 ''' #尝试次数计数器 tries = 0 ...