SQL查询语句

发布时间 2024-01-09 20:03:33作者: lcryptk

简单查询(针对单表)

投影查询

1 select specialty from student     
2 select distinct specialty from student --去重
1 --取前5位同学信息
2 select top 5 * from student
3 --取前50%同学信息
4 select top 50 percent * from student 

选择查询

1 --(1)比大小
2 select * from student where specialty='网安'
3 select * from student where specialty!='网安'
4 select * from student where specialty<>'网安'
5 --(2)确定范围
6 select * from sc where score between 80 and 90
7 --是否包含边界值?
8 select * from sc where score between 81 and 85  --包含边界值
9 select * from sc where score>=81 and score<=85
10 --(3)确定集合
11 select * from student where specialty in ('网安','反恐')
12 select * from student where specialty not in ('网安','反恐')
13
14 --模糊查询(字符匹配)  like  not like
15 select * from student where sname like '杨%'
16 select * from student where sname like '杨_'
17 --查询姓名中含有“小"的同学信息
18 select * from student where sname like '%小%'  --%可以匹配多个字符
19 select * from student where sname like '_小_'  --_可以匹配单个字符
20 --空值查询 is null  is not null
21 select * from sc where score = null --错误
22 select * from sc where score = not null --错误
23 select * from sc where score is null  --正确  找出缺少成绩的学生成绩情况
24 select * from sc where score is not null --正确  找出有成绩的学生成绩情况

分组查询

 1 --分组查询  group by ... having 参与分组的字段必须显示,除了聚合函数,其余字段不能写入
 2 --查询每门课程的平均成绩。。。。
 3 select cno 课程,avg(score) 平均成绩 ,sum(score) 总成绩,
 4        min(score) 最低成绩,max(score) 最高成绩,
 5        count(*) 选课人数, count(sno) 选课人数2
 6 from sc 
 7 group by cno
 8 --?查询不同专业(不同性别)的人数
 9 select specialty 专业,ssex 性别,count(*) 人数 
10 from student 
11 group by specialty,ssex

排序order by

 1 --排序 order by   asc/desc
 2 --查询学生成绩信息,按成绩排序
 3 select * from sc order by score asc --升序排 
 4 select * from sc order by score desc  --降序排 
 5 
 6 --先按成绩降序排; 当成绩相同时,按学号降序排 
 7 select * from sc order by score desc,sno desc 
 8 
 9 --?查询成绩排前3的同学成绩信息
10 select top 3 * from sc order by score desc 

连接查询(用于多表之间的关联查询)

内连接、左连接、右连接、自连接

 1 --连接查询
 2 --内连接
 3 --查询每个学生的学号、姓名、课程号和成绩
 4 select student.sno,sname,cno,score 
 5 from student inner join sc on student.sno=sc.sno  --方法一
 6 
 7 select student.sno,sname,cno,score 
 8 from student, sc 
 9 where student.sno=sc.sno  --方法二 
10 
11 --?在上面基础上,结果增加课程名称
12 select student.sno,sname,sc.cno,cname,score 
13 from student inner join sc on student.sno=sc.sno  --方法一
14              inner join course on sc.cno=course.cno
15 
16 select student.sno,sname,sc.cno,cname,score 
17 from student, sc , course
18 where student.sno=sc.sno and sc.cno=course.cno --方法二
19 
20 --表也可以起别名
21 select a.sno,sname,b.cno,cname,score 
22 from student a, sc b, course c
23 where a.sno=b.sno and b.cno=c.cno
24 
25 --(2)自连接 --可以解决同名、同住、同行等问题
26 --查询同名学生信息
27 select a.* from student a, student b
28 where a.sname=b.sname and a.sno!=b.sno
29 
30 --(3)外连接(左外连接),返回左表的所有内容(即使右表没有),若右表没有则返回null值
31 select * from student left join sc on student.sno=sc.sno

union和union all

 

子查询

SELECT语句可以嵌套在其他语句中(比如SELECT、INSERT、UPDATE及DELETE等),这些嵌套的SELECT语句被称为子查询

相关子查询

相关子查询的执行依赖于外部查询。多数情况下是子查询的WHERE子句中引用了外部查询的表。

执行过程如下:

(1)从外层查询中取出一个元组,将元组相关列的值传给内层查询。

(2)执行内层查询,得到子查询操作的值。

(3)外查询根据子查询返回的结果或结果集得到满足条件的行。

(4)然后外层查询取出下一个元组重复做步骤1-3,直到外层的元组全部处理完毕。

 1 SElECT 图书名,出版社,类编号,价格
 2   FROM Books As a
 3   WHERE 价格 >
 4   (
 5     SELECT AVG(价格)
 6     FROM Books AS b
 7     WHERE a.类编号=b.类编号
 8   )
 9 '''代码的执行过程:先将Books表中的第一条记录的“类编号”的值“2”代入子查询中,子查询变为:
10 
11       SELECT AVG(价格)
12           FROM Books AS b
13          WHERE b.类编号=2
14 
15   子查询的结果为该类图书的平均价格,所以外部查询变为:
16 
17       SElECT 图书名,出版社,类编号,价格
18          FROM Books As a
19        WHERE 价格 > 34
20 
21  如果WHERE条件为True,则第一条结果包括在结果集中,则否不包括。对Books表中的所有行运行相同的过程,最后形成的结果集及最后返回结果。'''

无关子查询

与相关子查询不同,无关子查询的子查询并不依赖于父查询,先进行子查询,并将子查询的结果传递给父查询。

 

参考文章

https://www.cnblogs.com/Ryan_j/archive/2010/10/20/1857026.html