w3resource_MySQL练习题:Subquery

1. Write a query to find the name (first_name, last_name) and the salary of the employees who have a higher salary than the employee whose last_name='Bull'
Sample table: employees
-- 要点:where里select
select first_name, last_name, salary
from employees
where salary>(
select salary from employees where last_name='Bull'
)

 

2. Write a query to find the name (first_name, last_name) of all employees who works in the IT department
Sample table: employees
-- 要点:直接where筛选
select first_name, last_name
from employees
where department_id='it'
3. Write a query to find the name (first_name, last_name) of the employees who have a manager and worked in a USA based department
Hint : Write single-row and multiple-row subqueries
Sample table: employees
Sample table: departments
Sample table: locations
-- 要点:多层嵌套查询
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'
        )
    )
)
4. Write a query to find the name (first_name, last_name) of the employees who are managers
Sample table: employees
-- 要点:在where中传入manager_id
select first_name, last_name
from employees
where employee_id in (
  select distinct manager_id from employees
)

  

5. Write a query to find the name (first_name, last_name), and salary of the employees whose salary is greater than the average salary
Sample table: employees
-- 要点:where
select first_name, last_name, salary
from employees
where salary>(
    select avg(salary) from employees
)
6. Write a query to find the name (first_name, last_name), and salary of the employees whose salary is equal to the minimum salary for their job grade
Sample table: employees
Sample table: jobs
-- 方法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
 
7. Write a query to find the name (first_name, last_name), and salary of the employees who earns more than the average salary and works in any of the IT departments
Sample table: employees
Sample table: departments
-- 要点: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%')
8. Write a query to find the name (first_name, last_name), and salary of the employees who earns more than the earning of Mr. Bell
Sample table: employees
Sample table: departments
-- 要点:where
select first_name, last_name, salary
from employees
where salary>(select salary from employees where last_name='Bell')
 
9. Write a query to find the name (first_name, last_name), and salary of the employees who earn the same salary as the minimum salary for all departments
Sample table: employees
Sample table: departments
-- 要点:where + min()
select first_name, last_name, salary
from employees
where salary=(select min(salary) from employees)
 
10. Write a query to find the name (first_name, last_name), and salary of the employees whose salary is greater than the average salary of all departments
Sample table: employees
-- 要点:where + min()
select first_name, last_name, salary
from employees
where salary>(select avg(salary) from employees)
11. Write a query to find the name (first_name, last_name) and salary of the employees who earn a salary that is higher than the salary of all the Shipping Clerk (JOB_ID = 'SH_CLERK'). Sort the results of the salary of the lowest to highest
Sample table: 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
12. Write a query to find the name (first_name, last_name) of the employees who are not supervisors
Sample table: employees
-- 要点:where + not in
select first_name, last_name
from employees
where employee_id not in (
    select manager_id from employees
)
13. Write a query to display the employee ID, first name, last name, and department names of all employees
Sample table: employees
Sample table: departments
-- 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;
14. Write a query to display the employee ID, first name, last name, salary of all employees whose salary is above average for their departments
Sample table: employees
Sample table: departments
-- 要点: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
)
15. Write a query to fetch even numbered records from employees table
Sample table: employees
-- 要点:判断奇偶数,使用%2进行取余
select *
from employees
where (employee_id%2<>0)
16. Write a query to find the 5th maximum salary in the employees table
Sample table: employees
-- 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
);
17. Write a query to find the 4th minimum salary in the employees table
Sample table: employees
-- 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
);
18. Write a query to select last 10 records from a table
Sample table: employees
-- 要点:根据employee_id进行排序
select *
from employees
order by employee_id desc
limit 10
19. Write a query to list the department ID and name of all the departments where no employee is working
Sample table: employees
Sample table: departments
-- 要点:通过department_id进行判断
select department_id, department_name
from departments
where department_id not in (
select distinct department_id from employees
)
20. Write a query to get 3 maximum salaries
Sample table: employees
-- 要点:limit
select *
from employees
order by salary desc
limit 3
21. Write a query to get 3 minimum salaries
Sample table: employees
-- 要点:limit
select *
from employees
order by salary asc
limit 3
22. Write a query to get nth max salaries of employees
Sample table: employees
-- 要点:limit a, b 取数范围为:第[a+1, a+b]条记录
select *
from employees
order by salary
limit (n-1), 1

w3resource_MySQL练习:Subquery的更多相关文章

  1. 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 不支持使 ...

  2. Oracle索引失效问题:WHERE C1='' OR C2 IN(SubQuery),并发请求时出现大量latch: cache buffers chains等待

    问题描述: 项目反馈某功能响应时间很长,高峰期时系统整体响应很慢... 获取相应的AWR,问题确实比较严重,latch: cache buffers chains等待,因为这些会话SQL执行时间太长, ...

  3. [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时

    案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...

  4. 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 不支持使 ...

  5. DEPENDENT SUBQUERY” 和 “SUBQUERY”

    http://blog.163.com/li_hx/blog/static/183991413201642410122327/ mysql> CREATE TABLE t1 (a INT, b ...

  6. linux之SQL语句简明教程---Subquery

    我们可以在一个 SQL 语句中放入另一个 SQL 语句.当我们在 WHERE 子句或 HAVING 子句中插入另一个 SQL 语句时,我们就有一个 subquery 的架构. Subquery 的作用 ...

  7. cakephp , the subquery (2)

    Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...

  8. cakephp , the subquery

    Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...

  9. Subquery returns more than 1 row

    Subquery returns more than 1 row表示子查询返回了多行数据 例如: select * from table1 where table1.colums=(select co ...

随机推荐

  1. Linux Maven install

    1 下载 maven : http://maven.apache.org/download.cgi2 解压 tar -xvf apache-maven-3.3.9-bin.tar.gz3 移到所需目录 ...

  2. <pre></pre>标签自动换行

    原文地址:https://www.cnblogs.com/qq78292959/p/4193142.html   pre { white-space: pre-wrap; word-wrap: bre ...

  3. 作用域提升(Scope Hositing )是 Webpack 3 的标志性特征

    http://blog.csdn.net/playboyanta123/article/details/73533079

  4. Funsioncharts 线图 破解

    在线示例:http://jsfiddle.net/henley/xnozyLa8/2/ 下载:http://files.cnblogs.com/files/ycdx2001/chart.zip

  5. MySQL 如何在一个语句中更新一个数值后返回该值 -- 自增长种子竞态问题处理

    什么是竞态问题? 假设有一个计数器,首先当前值自增长,然后获取到自增长之后的当前值.自增长后的值有可能被有些操作用来当做唯一性标识,因此并发的操作不能允许取得相同的值. 为什么不能使用使用UPDATE ...

  6. java内存分配(堆、栈、常量池)

    Java内存分配: ◆寄存器:我们在程序中无法控制 ◆栈:存放基本类型的数据和对象的引用,以及成员方法中的局部变量 ◆堆:存放对象本身(成员变量+成员方法的引用) ◆静态域:存放在对象中用static ...

  7. IO(字节流、字符流)

      第1章 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream ...

  8. 1068 乌龟棋 2010年NOIP全国联赛提高组

    1068 乌龟棋 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Descrip ...

  9. echarts使用中的那些事儿(二)

    然而第二天的事情你怎么能猜到客户的心思呢: 客户:右边的是啥? 偶:可放大缩小查看各个时期的数据. 客户:把它去掉吧,全部直观显示. 偶:哦,不过数据过多的话会导致页面过长的. 客户:只有你知道这个可 ...

  10. AngularJS所有版本下载地址

    AngularJS官网本身采用AngularJS库构建,页面中的AngularJS库通过Google的CDN(内容分发网络)引入,所以国内访问会有问题. 大家可以从下面地址获取AngularJS所以版 ...