别名、is null 和 is not null、去重、and和or、比较运算符 > < >= <= = != 和 <>、模糊查询 like、查询在两者之间 between x and y 包含xy、 in、排序 、分页、数值计算 + - * / %
1.别名 as
2.is null 和 is not null
3.distinct
4.and和or
5. > < >= <= = != 和 <>
6.like:%表示多个未知字符。-表示一个未知字符
7. between x and y
8.in(a,b,c)
9.默认升序、desc降序
10.limit(a,b)
11. + - * / %
练习:
表emp:empno,ename,job,mgr,hiredate,sal,comm,deptno
1. 案例:查询没有上级领导的员工的编号,姓名,工资
select e.empno,e.ename,e.sal from emp e where e.mgr is null;
2. 案例:查询emp表中没有奖金的员工的姓名,职位,工资,以及奖金
select e.ename,e.job,sal,comm from emp e where e.comm is null;
3. 案例:查询emp表中含有奖金的员工的编号,姓名,职位,以及奖金
select e.empno,e.ename,e.job,e.comm from emp e where e.comm is not null and comm !=0;
4. 案例:查询含有上级领导的员工的姓名,工资以及上级领导的编号
select e.ename,e.sal,e.mgr from emp e where e.mgr is not null and e.mgr!=0;
5. 案例:查询emp表中名字以‘S’开头的所有员工的姓名
select e.ename from emp e where e.ename like 'S%';
6. 案例:查询emp表中名字的最后一个字符是'S'的员工的姓名
select e.ename from emp e where e.ename like '%S';
7. 案例:查询倒数的第2个字符是‘E’的员工的姓名
select e.ename from emp e where e.ename like '%E_';
8. 案例:查询emp表中员工的倒数第3个字符是‘N’的员工姓名
select e.ename from emp e where e.ename like '%N__';
9. 案例:查询emp表中员工的名字中包含‘A’的员工的姓名
select e.ename from emp e where e.ename like '%A%';
10. 案例:查询emp表中名字不是以'K'开头的员工的所有信息
select e.ename from emp e where e.ename not like 'K%';
11. 案例:查询emp表中名字中不包含‘A’的所有员工的信息
select e.ename from emp e where e.ename not like '%A%';
12. 案例:做文员的员工人数(job 中 含有 CLERK 的)
select count(*) from emp e where e.job like '%CLERK%';
13. 案例:销售人员 job: SALESMAN 的最高薪水
select e.ename,max(sal) from emp e where e.job like '%SALESMAN%';
14. 案例:最早和最晚入职时间
select max(hiredate),min(hiredate) from emp e;
表t_item:
id,category_id,item_type,title,sell_point,price,num,barcode,image,
status,priority,created_time,modified_time,created_user,modified_user
15. 案例:查询类别 163的商品总库存量
select count(*) from t_item t where category_id=163;
16. 案例:查询 类别 163 的商品
select * from t_item where category_id=163;
17. 案例:查询商品价格不大于100的商品名称列表
select t.title from t_item t where t.price<=100;
18. 案例:查询品牌是联想,且价格在40000以上的商品名称和价格
select t.title,t.price from t_item t where t.title like '%联想%' and t.price >40000;
19. 案例:查询品牌是三木,或价格在50以下的商品名称和价格
select t.title,t.price from t_item t where t.title like '%三木%' and t.price<50;
20. 案例:查询品牌是三木、广博、齐心的商品名称和价格
select t.title,t.price from t_item t where t.title like '%三木%' or t.title like '%广博%' or t.title like '%齐心%' ;
21. 案例:查询品牌不是联想、戴尔的商品名称和价格
select t.title,t.price from t_item t where t.title not like '%联想%' and t.title not like '%戴尔%';
22. 案例:查找品牌是联想且价格大于10000的名称
select t.title from t_item t where t.price>10000 and t.title not like '%联想%';
23. 案例:查询联想或戴尔的电脑名称列表
select t.title from t_item t where t.category_id=163 and t.title like '%联想%' or t.title like '%戴尔%';
29. 案例:查询卖点含有'赠'产品名称
select t.title,t.sell_point from t_item t where sell_point like '%赠%';tr
表emp:empno,ename,job,mgr,hiredate,sal,comm,deptno
30. 案例:查询emp表中员工的编号,姓名,职位,工资,并且工资在1000~2000之间。
select e.empno,e.ename,e.job,e.sal from emp e where e.sal between 1000 and 2000;
select e.empno,e.ename,e.job,e.sal from emp e where e.sal >=1000 and e.sal <=2000;
31. 案例:查询emp表中员工在10号部门,并且含有上级领导的员工的姓名,职位,上级领导编号以及所属部门的编号
select e.ename,e.job,e.mgr,e.empno from emp e where e.deptno=10 and e.mgr is not null and e.mgr !=0;
32. 案例:查询emp表中名字中包含'E',并且职位不是MANAGER的员工的编号,姓名,职位,以及工资。
select e.empno,e.ename,e.job,e.sal from emp e where e.ename like '%E%' and e.job not like '%MANAGER%';
33. 案例:查询emp表中10号部门或者20号部门中员工的编号,姓名,所属部门的编号
select e.empno,e.ename,e.deptno from emp e where e.deptno in(10,20);
34. 案例:查询emp表中没有奖金或者名字的倒数第2个字母不是T的员工的编号,姓名,职位以及奖金
select e.empno,e.ename,e.comm from emp e where e.comm is not null and e.comm !=0 or e.ename not like '%T_';
35. 案例:查询工资高于3000或者部门编号是30的员工的姓名,职位,工资,入职时间以及所属部门的编号
select e.ename,e.job,e.sal,e.hiredate,e.deptno from emp e where e.sal>3000 or e.deptno=30;
36. 案例:查询不是30号部门的员工的所有信息
select * from emp e where e.deptno!=30;
37. 案例:查询奖金不为空的员工的所有信息
select * from emp e where e.comm is not null and e.comm !=0;
38. 案例:查询emp表中所有员工的编号,姓名,职位,根据员工的编号进行降序排列
select e.empno,e.ename,e.job from emp e order by e.empno desc;
39. 案例:查询emp表中部门编号是10号或者30号中,所有员工姓名,职务,工资,根据工资进行升序排列
select e.ename,e.job,e.sal from emp e where e.deptno in(10,30) order by e.sal ;
40. 案例:查询emp表中所有的数据,然后根据部门的编号进行升序排列,如果部门编号一致,根据员工的编号进行降序排列
select * from emp e order by e.deptno,e.empno desc;
41. 案例:查询emp表中工资高于1000或者没有上级领导的员工的编号,姓名,工资,所属部门的编号,以及上级领导的编号,
根据部门编号进行降序排列,如果部门编号一致根据工资进行升序排列。
select e.empno,e.ename,e.sal,e.deptno,e.mgr from emp e where e.sal>1000 or e.mgr is null or e.mgr=0 order by e.deptno desc,e.sal;
42. 案例:查询emp表中名字中不包含S的员工的编号,姓名,工资,奖金,根据工资进行升序排列,如果工资一致,根据编号进行降序排列
select e.empno,e.ename,e.sal,e.comm from emp e where e.ename not like '%S%' order by e.sal,e.empno desc;
43. 案例:统计emp表中员工的总数量
select count(*) from emp;
44. 案例:统计emp表中获得奖金的员工的数量
select count(*) from emp e where e.comm is not null and e.comm !=0;
45. 案例:求出emp表中所有的工资累加之和
select sum(e.sal) from emp e;
46. 案例:求出emp表中所有的奖金累加之和
select sum(e.comm) from emp e;
47. 案例:求出emp表中员工的平均工资
select avg(e.sal) from emp e;
48. 案例:求出emp表中员工的平均奖金
select avg(e.comm) from emp e;
49. 案例:求出emp表中员工的最高工资
select max(e.sal) from emp e;
50. 案例:求出emp表中员工编号的最大值
select max(e.empno) from emp e;
51. 案例:查询emp表中员工的最低工资。
select min(e.sal) from emp e;
52. 案例:查询emp表中员工的人数,工资的总和,平均工资,奖金的最大值,奖金的最小值,并且对返回的列起别名。
select count(*) as '人数',sum(e.sal) as '工资的总和', avg(sal) as '平均工资',max(comm) as '奖金的最大值',min(comm) as '奖金的最小值' from emp e;
.