1.全表查询
select * from stu;
2.查询指定的列
select id,age from stu;
3.列别名
select id i from stu;
4.求总行数
select count(*) from stu;
5.求最大值
select max(age) max_age from stu;
6.求最小值
select min(age) min_age from stu;
7.求总和
select sum(age) sum_sal from stu;
8.求平均值
select avg(age) avg_sal from stu;
9.限制查询返回行数
select * from stu limit 100;
10.查询年龄大于20的学生
select * from stu where age>20;
11.查询年龄等于20的学生
select * from stu where age=20;
12.查询年龄在20到25之间的学生
select * from stu where age between 20 and 25;
13.查询年龄为空的学生
select * from stu where age is null;
14.查询年龄为20或25的学生
select * from stu where age in (20,25);
15.查找年龄以1开头的学生的信息
select * from stu where age like '1%';
16.查找第二个值为4的年龄学生的信息
select * from stu where age like '_4%';
17.查找年龄含有6的学生的信息
select * from stu where rlike '[2]';
18.查询薪水大于2000,部门是10
select * from emp where sal>2000 and deptno=10;
19.查询薪水大于2000,或者部门是10
select * from emp where sal>2000 or deptno=10;
20.查询除了20部门和40部门以外的员工信息
select * from emp where deptno not IN(20, 40);
21.计算emp表每个部门的平均工资
select t.deptno, avg(t.sal) avg_sal from emp t group by t.deptno;
22.计算emp每个部门中每个岗位的最高薪水
select t.deptno, t.job, max(t.sal) max_sal from emp t group by t.deptno, t.job;
23.查询平均薪水大于3000的部门
select deptno, avg(sal) avg_sal from emp group by deptno having avg_sal > 3000;
24.内连接
select e.empno, e.ename, d.dname from
emp e join dept d
on e.deptno = d.deptno;
25.左外连接(JOIN操作符左边表中符合WHERE子句的所有记录将会被返回)
select e.empno, e.ename, d.deptno from
emp e left join dept d
on e.deptno= d.deptno;
26.右外连接(JOIN操作符右边表中符合WHERE子句的所有记录将会被返回)
select e.empno, e.ename, d.deptno from
emp e right join dept d
on e.deptno= d.deptno;
27.满外连接(将会返回所有表中符合WHERE语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用NULL值替代。)
select e.empno, e.ename, d.deptno from
emp e full join dept d
on e.deptno = d.deptno;
28.多表连接(连接 n个表,至少需要n-1个连接条件。例如:连接三个表,至少需要两个连接条件。)
SELECT e.ename, d.deptno, l.loc_name from
emp e join dept d
on d.deptno = e.deptno
join location l
on d.loc = l.loc;