– 基础查询练习
显示出表employees部门编号在80-100之间 的姓名、职位
select last_name,job_id from employees
where department_id >= 80 and department_id <=100
显示出表employees的manager_id 是 100,101,110 的员工姓名、职位
select last_name,job_id from employees
WHERE manager_id=100 OR manager_id=101 OR manager_id = 110;
select last_name,job_id from employees
where manager_id in(100,101,110)
#案例1:查询姓名中包含字符 e的员工信息
select * from employees
where last_name like '%e%'
#案例2:查询姓名中第二个字符为e,第四个字符为a的员工信息
select * from employees
where last_name like '_e_a%'
#案例1:查询没有奖金的员工
select * from employees
where commission_pct is null
//有奖金
select *from employees
where commission_pct is not null
查询员工号为176的员工的姓名和部门号和年薪
select last_name,department_id,salary*12*(1+IFNULL(commission_pct,0))
from employees
where employee_id=176
选择工资不在5000到12000的员工的姓名和工资
SELECT last_name,salary from employees
where not(salary BETWEEN 5000 and 12000)
选择在20或50号部门工作的员工姓名和部门号
SELECT last_name,department_id from employees
where department_id in(20,50)
按单个字段进行排序
SELECT * from employees
order by salary asc
SELECT *FROM employees
ORDER BY salary DESC
按多个字段进行排序
select * from employees
ORDER BY salary desc,employee_id ASC
按表达式排序
#案例:按年薪降序
SELECT salary*12*(1+IFNULL(commission_pct,0)) 年薪 from employees
order by 年薪 DESC
按函数排序
#案例:按姓名中的字节长度大小降序
select last_name,LENGTH(last_name) from employees
order by LENGTH(last_name) DESC
#1.查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
SELECT last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) 年薪
from employees
ORDER BY 年薪 DESC,last_name ASC
#2.选择工资不在8000到17000的员工的姓名和工资,按工资降序
select last_name,salary from employees
where salary not BETWEEN 8000 and 17000
ORDER BY salary DESC
#3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT * FROM employees
where email like'%e%'
ORDER BY LENGTH(email) DESC,department_id asc
#01案例:查询每个部门的员工个数和部门名
SELECT COUNT(*) 个数,department_name
from employees e,departments d
where e.employee_id=d.department_id
GROUP BY e.department_id
having 个数>5
#案例2:查询名字中第三个字符为a,第五个字符为e的员工的工资以及对应的工资级别
select salary,grade_level from employees e,job_grades g
where e.salary BETWEEN lowest_sal and highest_sal
and e.last_name like '__a_e%'
#案例2:查询有奖金的员工名、部门名
select last_name,department_name
from departments d
join employees e on e.department_id=d.department_id
where commission_pct is not NULL
#案例3:查询城市名、员工名和部门名
select city,last_name,department_name from employees e
inner join departments d on e.department_id=d.department_id
inner join locations l on l.location_id=d.location_id
查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
#查询各个部门的平均工资
select AVG(salary),department_id from employees
where department_id is not null
group by department_id
#查询员工 工号 姓名 工资
select salary,last_name,employee_id,e.department_id
FROM employees e,
(
SELECT department_id,AVG(salary) 部门平均工资
FROM employees
GROUP BY department_id
)f
where e.department_id = f.department_id and e.salary > f.部门平均工资
查询工资比公司平均工资高的员工的员工号,姓名和工资。
select avg(salary) from employees
select employee_id,last_name,salary from employees
where salary>(
select avg(salary) from employees
)
查询姓名中包含字母u的员工在相同部门的员工的员工号和姓名
select department_id from employees
where last_name like '%u%'
select employee_id,last_name from employees
where department_id in(
select department_id from employees
where last_name like '%u%'
)
查询在部门的location_id为1700的部门工作的员工的员工号
select department_id from departments
where location_id = 1700
select employee_id from employees
where department_id in(
select department_id from departments
where location_id = 1700
)
查询管理者是King的员工姓名和工资
select employee_id from employees
where last_name ='King'
select last_name,salary from employees
where manager_id in(
select employee_id from employees
where last_name ='King'
)
查询平均工资最低的部门信息
select avg(salary) f,department_id from employees
group by department_id
select min(f) from (
select avg(salary) f,department_id from employees
group by department_id
)a
select avg(salary),department_id from employees
group by department_id
having AVG(salary)=(
select min(f) from (
select avg(salary) f ,department_id from employees
group by department_id
)a
)
select * from departments
where department_id=(
select department_id from employees
where department_id is not null
group by department_id
having AVG(salary)=(
select min(f) from (
select avg(salary) f ,department_id from employees
group by department_id
)a
)
)