练习1:显示表departments表结构:describe departments
练习2:查询departments里面的全部数据:select * from departments
练习3:select 100+1.5输出结果是多少:101.5
练习4:select '123'+100的输出结构是多少:223
练习5:select 'johin'+100的输出结果是多少:100
说明: myql中+的作用:当其中的一个操作数为字符型,则试图将此字符型转换为数值型
如果转换成功,则直接做加法运算
如果转换失败,则此字符值当做哦0,继续运算
练习6:select null +'johin'输出结果是多少:null
当其中的一个操作数为null,则结果为null
练习7:select 'jack'+'rose'+100+200输出结果是多少:300
练习8:查询员工表里工资大于12000的员工姓名(last_name)和工资(salary)
select last_name,salary
from employees
where salary >12000;
练习9:选择工资不在5000到12000之间的员工姓名(last_name)和工资(salary)
select last_name,salary
from employees
WHERE
not (salary >=5000 and salary <=12000)
练习10:选择在20号或50号部门工作的员工姓名和部门号
select last_name,department_id
from employees
where department_id=20 or department_id=50;
练习11:查找员工表中,有奖金的的员工姓名,工资,和奖金级别
select last_name,salary,commission_pct
from employees
where commission_pct is not null;
练习12:选择姓名中(last_name)中包含有字母a和e的员工姓名
select last_name
from employees
where last_name like '%a%' and last_name like '%e%';
练习13:查询出员工表中,first_name以e结尾的员工信息
select *
from employees
where first_name like '%e';
练习14:查询员工表,把first_name 显示为名称,last_name显示为姓氏,salary显示为月薪
select first_name as "名称",last_name as "姓氏",salary as "月薪"
from employees;
练习15:查询部门编号是30号的员工信息
select *
from employees
where department_id=30;
练习16:查询部门编号不是30号的员工信息
select *
from employees
where department_id<>30;
练习17:查询员工表中,工资在10000-20000之间的员工信息
select *
from employees
where salary>=10000 and salary<=20000;
练习18:查询员工表中job_id=AD_VIP或department_id>80的员工信息
select *
from employees
where job_id='AD_VIP' or department_id>80
练习19:查询部门编号不是在80-100之间的员工信息
select *
from employees
where not (department_id>=80 and department_id<=100);
练习20:查询工具>10000或者department_id在30-80之间的员工信息
select *
from employees
where salary>10000
or (department_id>=30 and department_id<=80);
练习21:查询姓名中包含e的员工信息
select *
from employees
where last_name like '%e%';
练习22:查询姓名中第三个字符为a的员工信息
select *
from employees
where last_name like '__a%';
练习23:查询员工表里工资在5000-12000之间的员工的姓名和工资(要求用2种方法实现)
第1种方法:
select last_name,salary
from employees
where salary between 5000 and 12000;
第2种方法:
select last_name,salary
from employees
where salary >=5000 and salary<=12000;
练习24:查询部门编号(department_id)为30、50、90的员工信息(要求用2种方法实现)
第1种方法:
select *
from employees
where department_id in (30,50,90);
第2种写法:
select *
from employees
where department_id=30 or department_id=50 or department_id=90
练习25:查询没有奖金的员工(commission_pct)
判断为空 is null
判断不为空 is not null
select *
from employees
where commission_pct is NULL;
练习26:查询员工的信息,按工资降序(按单个字段进行排序)
select *
from employees
ORDER BY salary desc;
练习27:按年薪进行降序
备注说明:
ifnull()
函数的作用:是用于判断第一个表达式是否为null,如果为null则返回第二个参数的值,如果不为null则返回第一个参数的值。
select salary*12*(1+IFNULL(commission_pct,0))
from employees
order by salary*12*(1+IFNULL(commission_pct,0));
练习28:按别名进行排序
select salary*12*(1+IFNULL(commission_pct,0)) 年薪
from employees
order by 年薪 desc;
练习29:查询员工表里面的last_name信息,按姓名长度的字节数从大到小排序
select last_name,LENGTH(last_name)
from employees
ORDER BY LENGTH(last_name)desc;
练习30:查询员工表的信息,按工资进行升序,工资一样再按部门编号倒序排列(多个字段排序)
select *
from employees
order by salary ASC,department_id desc;
练习31:查询员工表中,部门编号在80-100之间的last_name,employee_id信息,并且按工资降序排列
select last_name,employee_id
from employees
where department_id BETWEEN 80 and 100
ORDER BY salary desc;
练习32:查询没有奖金,且工资小于18000的salary,last_name信息
select salary,last_name
from employees
where commission_pct is NULL
and salary<18000;
练习33:查询员工表中,job_id不为“IT”或者工资为12000的员工信息
select *
from employees
where not (job_id='IT')
or salary=12000;
练习34:查询员工工号、姓名、工资、以工资的提高20%后的结果(new salary)显示
SELECT employee_id,last_name,salary,salary*1.2 as "new salary"
from employees;
练习35:where子句中是否可以使用组函数进行过滤?
不可以,只要是分组函数再做筛选,就要使用having进行
练习36:查询公司员工工资的最大值,最小值,平均值,总和
select max(salary),MIN(salary),avg(salary),sum(salary)
from employees;
练习37:查询各个工种(job_id)的员工工资的最大值,最小值,平均值,总和,并且按job_id升序排列
select max(salary),min(salary),avg(salary),sum(salary),job_id
from employees
GROUP BY job_id
ORDER BY job_id asc;
练习38:查询员工表中最高工资和最低工资的差距(difference)
select max(salary)-min(salary) difference
from employees;
练习39:查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不在计算内,
提示:管理者:manager_id
select min(salary),manager_id
from employees
where manager_id is not NULL
group by manager_id
HAVING min(salary)>=6000
练习40:查询所有部门的编号、员工数量和工资平均值,并按平均工资降序
select department_id,count(*) as "员工数量",avg(salary) as "平均工资"
from employees
GROUP BY department_id
order by 平均工资 desc;
练习41:选择具有各个工种job_id的员工人数
select job_id,count(*) as "人数"
from employees
GROUP BY job_id;
练习42:查询每个部门的最低工资
select department_id,min(salary) as "最低工资"
from employees
GROUP BY department_id;
练习43:查询女神表中每个男神的女朋友个数(采用多表连接)
练习44:男神编号<5的,每个男神的女朋友个数(采用多表连接)
练习45:查询每个部门的有奖金的员工的最高工资
select department_id,max(salary)
from employees
where commission_pct is not NULL
group by department_id;
练习46:查询哪个部门有奖金员工最高工资 >10000的部门编号和最高工资
select department_id,max(salary)
from employees
where commission_pct is not NULL
group by department_id
having max(salary)>10000;
练习47:having别名
查询哪个工种员工的平均工资<5000
#1筛选每个工种的平均工资
#筛选,分析筛选针对的是原始表还是分组后的结果
select job_id,avg(salary) as "平均工资"
from employees
GROUP BY job_id
HAVING 平均工资<5000;
练习48:按多个字段进行分组,并且排序
查询每个部门每个工种的员工人数,并且倒序排序
select department_id,job_id,count(*) as "人数"
from employees
GROUP BY department_id,job_id
ORDER BY 人数 desc;
-----------------------------多表联合查询------------------------------
练习49:查询员工名和对应的部门名
分析:
员工名来自employees表的last_name
部门名称来自于departments表的中depart_name
这两个表通过字段department_id关联
select e.last_name,d.department_name
from employees e,departments d
where e.department_id=d.department_id;
练习50:查询有奖金的员工名和工种名(和筛选搭配)
分析:
员工名来自employess表
工种名来自jobs里的job_title
select e.last_name,j.job_title
from employees e,jobs j
where e.job_id=j.job_id
and commission_pct is not null;
练习51:查询每个部门的部门名称和员工个数(和分组搭配)
分析:
部门名称来自于departments
员工个数来自于employees表
这两张表是通过department_id关联的
查询 每个部门需要分组
select department_name,count(*) 员工个数
from departments d,employees e
where d.department_id=e.department_id
GROUP BY e.department_id;
练习52:查询员工个数>5的部门名称(和分组搭配而且和排序搭配),并且按倒序排列
select department_name,count(*) 个数
from departments d,employees e
where d.department_id=e.department_id
group by e.department_id
HAVING 个数>5
ORDER BY 个数 desc;
练习53(三表查询)
查询员工名、部门名、城市名
select last_name,department_name,city
from departments d,employees e,locations l
where e.department_id=d.department_id
and d.location_id=l.location_id;
来源:CSDN
作者:金朝阳
链接:https://blog.csdn.net/JCY58/article/details/103914190