标签:http from etc sel -- ppi mysql man dep
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
标签:http from etc sel -- ppi mysql man dep
原文地址:https://www.cnblogs.com/xingyucn/p/10540074.html