1.字段查询
select empno,ename from emp;
2.过滤where,limit,distinct
select * from emp where sal >2500;
select * from emp limit 2;
select distinct deptno from emp;
3.过滤between and,is null
select * from emp where sal between 2000 and 3000;
select * from emp where comm is null;
4.聚合函数count,sum,avg,max,min
select count(1) from emp;
select avg(sal) avg_sal from emp;
5.group by,having
要求:求每个部门的平均工资
select deptno,avg(sal) from emp group by deptno;
要求:部门平均工资大于2000的部门。
select deptno,avg(sal) avg_sal from emp group by deptno having avg_sal>2000;
6.join
-》展示emp中的数据
-》等值join:匹配连接字段两边共有的值
select e.empno,e.ename ,d.deptno ,e.sal from emp e join dept d on e.deptno=d.deptno;
-》左join left:以左边的该字段的值为标准
select e.empno,e.ename ,d.deptno ,e.sal from emp e left join dept d on e.deptno=d.deptno;
-》右join right:以右边的该字段的值为标准
select e.empno,e.ename ,d.deptno ,e.sal from emp e right join dept d on e.deptno=d.deptno;
-》全join full:以两张表中连接字段的所有值
select e.empno,e.ename ,d.deptno ,e.sal from emp e full join dept d on e.deptno=d.deptno;