当前位置:数据库 > SQL语言> 正文

常用SQL查询语句

时间:2014-8-7类别:数据库

常用SQL查询语句

常用SQL查询语句


一、基本常用查询


--select
select * from student;
 
 
--distinct 过滤重复
select distinct sex from student;
 
--count 统计
select count(*) from student;
select count(sex) from student;
select count(distinct sex) from student;
 
--top 取前N条记录
select top 3 * from student;
 
--alias column name 列重命名
select id as 编号, name '名称', sex 性别 from student;
 
--alias table name 表重命名
select id, name, s.id, s.name from student s;
 
--column 列运算
select (age + id) col from student;
select s.name + '-' + c.name from classes c, student s where s.cid = c.id;
 
--where 条件
select * from student where id = 2;
select * from student where id > 7;
select * from student where id < 3;
select * from student where id <> 3;
select * from student where id >= 3;
select * from student where id <= 5;
select * from student where id !> 3;
select * from student where id !< 5;
 
--and 并且
select * from student where id > 2 and sex = 1;
 
--or 或者
select * from student where id = 2 or sex = 1;
 
--between ... and ... 相当于并且
select * from student where id between 2 and 5;
select * from student where id not between 2 and 5;
 
--like 模糊查询
select * from student where name like  '%a';
select * from student where name not like  '%a%';
select * from student where name like 'ja%';
 
--in 子查询
select * from student where id in (1, 2);
 
--not in 不在其中
select * from student where id not in (1, 2);
 
--is null 是空
select * from student where age is null;
 
--is not null 不为空
select * from student where age is not null;
 
--order by 排序
select * from student order by name;
select * from student order by name desc;
select * from student order by name asc;
 
--group by 分组


按照年龄进行分组统计
select count(age), age from student group by age;


按照性别进行分组统计
select count(*), sex from student group by sex;


按照年龄和性别组合分组统计,并排序
select count(*), sex from student group by sex, age order by age;


按照性别分组,并且是id大于2的记录最后按照性别排序
select count(*), sex from student where id > 2 group by sex order by sex;


查询id大于2的数据,并完成运算后的结果进行分组和排序
select count(*), (sex * id) new from student where id > 2 group by sex * id order by sex * id;

 

--having 分组过滤条件


按照年龄分组,过滤年龄为空的数据,并且统计分组的条数和现实年龄信息
select count(*), age from student group by age having age is not null;
 
按照年龄和cid组合分组,过滤条件是cid大于1的记录
select count(*), cid, sex from student group by cid, sex having cid > 1;
 
按照年龄分组,过滤条件是分组后的记录条数大于等于2
select count(*), age from student group by age having count(age) >= 2;
 
按照cid和性别组合分组,过滤条件是cid大于1,cid的最大值大于2
select count(*), cid, sex from student group by cid, sex having cid > 1 and max(cid) > 2;
 

 

二、子查询(嵌入到其他sql语句中的select语句,也叫嵌套查询)

 

1、单行子查询

SELECT ename FROM emp WHERE deptno=(SELECT deptno FROM emp where ename='SMITH');

查询表中与smith同部门的人员名字。因为返回结果只有一行,所以用“=”连接子查询语句

 

2、多行子查询

SELECT ename,job,sal,deptno from emp WHERE job IN (SELECT DISTINCT job FROM emp WHERE deptno=10);

查询表中与部门号为10的工作相同的员工的姓名、工作、薪水、部门号。因为返回结果有多行,所以用“IN”连接子查询语句。

 

3、使用ALL

SELECT ename,sal,deptno FROM emp WHERE sal> ALL (SELECT sal FROM emp WHERE deptno=30);或

SELECT ename,sal,deptno FROM emp WHERE sal> (SELECT MAX(sal) FROM emp WHERE deptno=30);

查询工资比部门号为30号的所有员工工资都高的员工的姓名、薪水和部门号。以上两个语句在功能上是一样的,但执行效率上,函数会高 得多。

 

4、使用ANY

SELECT ename,sal,deptno FROM emp WHERE sal> ANY (SELECT sal FROM emp WHERE deptno=30);或

SELECT ename,sal,deptno FROM emp WHERE sal> (SELECT MIN(sal) FROM emp WHERE deptno=30);

查询工资比部门号为30号的任意一个员工工资高(只要比某一员工工资高即可)的员工的姓名、薪水和部门号。以上两个语句在功能上是 一样的,但执行效率上,函数会高得多。

 

5、多列子查询

SELECT * FROM emp WHERE (job, deptno)=(SELECT job, deptno FROM emp WHERE ename='SMITH');

 

6、在from子句中使用子查询

SELECT emp.deptno,emp.ename,emp.sal,t_avgsal.avgsal FROM emp,(SELECT emp.deptno,avg(emp.sal) avgsal FROM emp GROUP BY emp.deptno) t_avgsal where emp.deptno=t_avgsal.deptno AND emp.sal>t_avgsal.avgsal ORDER BY emp.deptno;

 

7、分页查询

数据库的每行数据都有一个对应的行号,称为rownum.

SELECT a2.* FROM (SELECT a1.*, ROWNUM rn FROM (SELECT * FROM emp ORDER BY sal) a1 WHERE ROWNUM<=10) a2 WHERE rn>=6;

指定查询列、查询结果排序等,都只需要修改最里层的子查询即可。

 

8、用查询结果创建新表

CREATE TABLE mytable (id,name,sal,job,deptno) AS SELECT empno,ename,sal,job,deptno FROM emp;

 

9、合并查询(union 并集, intersect 交集, union all 并集+交集, minus差集)

SELECT ename, sal, job FROM emp WHERE sal>2500 UNION(INTERSECT/UNION ALL/MINUS) SELECT ename, sal, job FROM emp WHERE job='MANAGER';

合并查询的执行效率远高于and,or等逻辑查询。

 

10、使用子查询插入数据

CREATE TABLE myEmp(empID number(4), name varchar2(20), sal number(6), job varchar2(10), dept number(2));

先建一张空表;

INSERT INTO myEmp(empID, name, sal, job, dept) SELECT empno, ename, sal, job, deptno FROM emp WHERE deptno=10;

再将emp表中部门号为10的数据插入到新表myEmp中,实现数据的批量查询。

 

11、使用了查询更新表中的数据

UPDATE emp SET(job, sal, comm)=(SELECT job, sal, comm FROM emp where ename='SMITH') WHERE ename='SCOTT';

 

 

三、聚合查询

 

1、 distinct去掉重复数据


select distinct sex from student;
select count(sex), count(distinct sex) from student;

 

2、 compute和compute by汇总查询


对年龄大于的进行汇总
select age from student
where age > 20 order by age compute sum(age) by age;
 
对年龄大于的按照性别进行分组汇总年龄信息
select id, sex, age from student
where age > 20 order by sex, age compute sum(age) by sex;
 
按照年龄分组汇总
select age from student
where age > 20 order by age, id compute sum(age);
 
按照年龄分组,年龄汇总,id找最大值
select id, age from student
where age > 20 order by age compute sum(age), max(id);

compute进行汇总前面是查询的结果,后面一条结果集就是汇总的信息。compute子句中可以添加多个汇总表达式,可以添加的信息如下:

     a、 可选by关键字。它是每一列计算指定的行聚合

     b、 行聚合函数名称。包括sum、avg、min、max、count等

     c、 要对其执行聚合函数的列

compute by适合做先分组后汇总的业务。compute by后面的列一定要是order by中出现的列。

 

3、 cube汇总

cube汇总和compute效果类似,但语法较简洁,而且返回的是一个结果集。


select count(*), sex from student group by sex with cube;
select count(*), age, sum(age) from student where age is not null group by age with cube;

cube要结合group by语句完成分组汇总

 

 

四、集合运算

 

操作两组查询结果,进行交集、并集、减集运算

1、 union和union all进行并集运算


--union 并集、不重复
select id, name from student where name like 'ja%'
union
select id, name from student where id = 4;
 
--并集、重复
select * from student where name like 'ja%'
union all
select * from student;

 

2、 intersect进行交集运算


--交集(相同部分)
select * from student where name like 'ja%'
intersect
select * from student;

 

3、 except进行减集运算


--减集(除相同部分)
select * from student where name like 'ja%'
except
select * from student where name like 'jas%';

 

 

五、连接查询

 

1、 简化连接查询


--简化联接查询
select s.id, s.name, c.id, c.name from student s, classes c where s.cid = c.id;

 

2、 left join左连接


--左连接
select s.id, s.name, c.id, c.name from student s left join classes c on s.cid = c.id;

 

3、 right join右连接


--右连接
select s.id, s.name, c.id, c.name from student s right join classes c on s.cid = c.id;

 

4、 inner join内连接


--内连接
select s.id, s.name, c.id, c.name from student s inner join classes c on s.cid = c.id;
 
--inner可以省略
select s.id, s.name, c.id, c.name from student s join classes c on s.cid = c.id;

 

5、 cross join交叉连接


--交叉联接查询,结果是一个笛卡儿乘积
select s.id, s.name, c.id, c.name from student s cross join classes c
--where s.cid = c.id;

 

6、 自连接(同一张表进行连接查询)


--自连接
select distinct s.* from student s, student s1 where s.id <> s1.id and s.sex = s1.sex;

 

标签:
上一篇下一篇

猜您喜欢

热门推荐