码迷,mamicode.com
首页 > 数据库 > 详细

w3resource_MySQL练习:Subquery

时间:2019-03-15 23:18:18      阅读:645      评论:0      收藏:0      [点我收藏+]

标签:http   from   etc   sel   --   ppi   mysql   man   dep   

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

标签:http   from   etc   sel   --   ppi   mysql   man   dep   

原文地址:https://www.cnblogs.com/xingyucn/p/10540074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!