数据库SQL语言语法总结3---查询语句

来源:互联网 发布:toad for oracle mac 编辑:程序博客网 时间:2024/05/19 09:17

SQL查询语句,即在数据库中查询符合条件的记录

语法格式:
select [all|distinct] <目标列表达式> [,<目标列表达式>] […..]
from 表名|视图表
[where <条件表达式>]
[group by <列名> [having <条件表达式>]] ———————按属性列分组显示
[order by <列名> [asc|desc]] ————————————–表示按属性列进行排序

语法格式表达式中[]表示括号中内容可以省略。|的两边二选一,即为或。而<>无意义
且语法格式中all为全部查询显示出来,distinct为删除相同的记录,将不同的记录查询显示出来

目标列表达式:
1) * : 表示“from”后所跟的关系列表中的全部属性(因为“from”后可以跟有多个列表,*将所有列表的属性全部显示)
2) 表名.* :表示单个表的全部属性(将“from”后跟着的多个表中的一个选定的表的全部属性全部显示)
3) count(*):统计关系表中符合条件的记录的个数
4) 字段名表达式:可以是属性列,也可以是属性列与库函数结合使用

库函数:
1)count [distinct] + 属性列名称:在此属性列下不同的值的个数
2)sum +属性列名称:求和
3)avg + 属性列名称 :求平均值
4)max + 属性列名称 :求最大值
5)min + 属性列名称 :求最小值
(每个库函数都可以在其关键字后加[distinct]来将此属性下相同的记录去除,所以到底加不加distinct 视情况而定)

where条件
1)字段名 运算符 {字段名|常量}
2)字段名 [not] between {字段名|常量} and {字段名|常量}
3)字段名 [not] like 字符串常数
4)字段名 is [not] null
5){字段名|常量} {[not] in | 运算符 {any | all}}{值1[,值2]……|select子句}
6)[not] exists (select子句)
7)条件表达式{and|or}条件表达式

下面结合几个例子来进一步了解SQL查询语句

前提条件:数据库中现存在3个关系表
Student (Sno ,Sname,Ssex,Sage,Sdept);
解释:学生(学号,学生姓名,学生性别,学生年龄,学生专业);
Course(Cno,Cname,Cpno);
解释:课程(课程号,课程名,可能的先修课程);
SC(Sno,Cno,Grade);
解释:学生选课情况(学号,课程号,成绩);

查询语句:
1) 查询学生的学号,姓名 —— select Sno,Sname from Student
2)查询学生的全部信息——- select * from Student
3)查询学生的姓名和出生年份——-select Sname,2017-Sage from Student 其中Sage是年龄,所以2017-Sage表示出生年份
4)查询学生的姓名出生年月和专业,其中出生年月以Year of birth为标题,专业以小写显示并以Department为标题——–select Sname,2001-Sage ‘Year of birth’,is lower(Sdept) Department from Student 其中,在属性名后加一个空格,再跟着一串字符即为标题,而is lower为转化为小写的函数,将专业转化为小写,而如果标题内部没有空格则不需要用‘’,此例中Department中无空格则直接写,Year of birth因为有空格则加‘’。
5)查询选了课的学生的学号——select distinct Sno from SC
6)显示选了课的学生的学号,不删除相同的记录———-select Sno from SC = select all Sno from SC
7)查询专业为CS的学生的姓名——–select Sname from Student where Sdept = ‘CS’
8)查询成绩<60分的学生的学号(删除相同的记录)——–select distinct Sno from SC where Grade <60
9)查询年龄在20至30岁的学生的姓名专业和年龄 ——–select Sname,Sdept,Sage from Student where Sage between 20 and 30
10)查询专业为CS MA IS 三者中任何一个的学生的姓名和性别——–select Sname,Ssex from Student where Sdept in (‘CS’,’MA’,’IS’)
11)查询名字不以“刘”开头的学生的全部信息——–select * from Student where Sname not like ‘刘%’ (%为通配符,可以匹配任意长度的字符串,刘%就表示姓刘的所有人)
12)查询姓欧阳,然后名为一个字的学生的信息———select * from Student where Sname like ‘欧阳_ ‘( 为通配符,但只匹配一个字节,而中文是两个字节,所以匹配一个字要用两个 _ )
13)查询课程名为DB_ Design的课程的全部信息———select * from Course where Cname like ‘DB\ _ Design’(当字符串中出现通配符%或者_ 时,需要在通配符前加入转义字符\即DB\ _Design为DB_Design)
14)查询分数为空即没有成绩的学生的学号和该课程号———select Sno,Cno from SC where Grade is null
15)查询专业为CS且年龄<20岁的学生的姓名———select Sname from Student where Sdept = ‘CS’ and Sage <20

查询结果排序:
1)查询选修课程号为3的记录,按照分数从高到低降序排列,desc为降序排列———select * from SC where Cno = ‘3’ order by Grade desc
2) 将Student关系表按照学生所在专业的ASCII码的升序排列,专业名相同时按照年龄的降序排列———select * from Student order by Sdept,Sage desc(默认为升序排列,显性表示desc为降序排列)

库函数的使用:
1)查询Student表中的记录数———select count(*) from Student
2)查询选了课的学生的数量———select count(distinct(Sno)) from SC(选了课的学生的数量即在SC中不同的学号的数量)
3)查询选修了1号课程的学生的平均分———select avg(Grade) from SC where Cno = ‘1’
4)查询选修了1号课程的学生的最高分———select max(Grade) from SC where Cno = ‘1’

对查询结果进行分组
1)查询各课程的课程号以及选修了这门课的学生的人数———select Cno,count(Sno) from SC group by Cno(按照课程号分组,即一个课程号为一组。一个课程号对应的学号的数量即为选修了这门课的学生的人数)
2)查询选修了3门课以上的学生的学号———- select Sno from SC group by Sno having count(*) >3(按照学号进行分组,一个学号为一组,SC中对应的记录为一个学号选修了一门课的选修情况,所以一个学号对于的记录大于3为选修了超过3门课的学生)

连接查询:
1)等值连接:查询每个学生的信息和选修课程情况—————–select Student.* from Student,SC where Student.Sno = SC.no
2)复合条件连接:查询选修了2号课程且分数在90分以上的学生的全部信息————–select Student.* from Student,SC where Student.Sno = SC.no and SC.Cno = ‘2’ and SC.Grade >90( Student.Sno = SC.no为隐含条件,需要注意)

嵌套查询:
1)查询与‘刘晨’在同一个专业学习的学生———select Sno,Sname,Sdept from Student where Sdept in (select Sdept from Student where Sname = ‘刘晨’)(分析时可以从后向前分析,如此例分析:select Sdept from Student where Sname = ‘刘晨’ 为 刘晨 所学的专业,select Sno,Sname,Sdept from Student where Sdept in 为查询某个专业的学生的学号,姓名,专业,所以共同组成了这个查询,即为查询在刘晨所学的专业中的学生的学号,姓名,专业。显示专业因为刘晨可能重名,并且在不用的专业,所以因此要显示详细信息)
2)查询选修了‘信息系统’的学生学号和姓名————–select Sno,Sname from Student where Sno in (select Sno from SC where Cno in (select Cno from Course where Cname = ‘信息系统’))
(分析:select Cno from Course where Cname = ‘信息系统’表示查询课程名为信息系统的课程号,select Sno from SC where Cno in表示查询选修信息系统的学生的学号,select Sno,Sname from Student where Sno in为查询选修了信息系统的学生的相关信息)等价于 select Sno,Sname from Student,SC,Course where Student.Sno = SC.Sno and SC.Cno = Course. Cno and Course.Cname = ‘信息系统’

带比较运算符的子查询:
查询与‘刘晨’在同一个专业学习的学生————-select * from Student where Sdept = (select Sdept from Student where Sname = ‘刘晨’)

带any,all子查询
1)查询其他系统比信息系某一学生年龄小的学生姓名和年龄—————select Sname,Sage from Student where Sage < any(select Sage from Student where Sdept = ‘IS’)and Sdept!=’IS’
等价于 select Sname,Sage from Student where Sage <(select max(Sage) from Student where Sdept = ‘IS’)and Sdept!=’IS’
(any为满足查询到的所有记录中的任意一个即可,此例为小于查询到的记录中的任何一个)
2)查询其他系统比信息系全部学生年龄小的学生姓名和年龄—————select Sname,Sage from Student where Sage < all(select Sage from Student where Sdept = ‘IS’)and Sdept!=’IS’
等价于 select Sname,Sage from Student where Sage <(select min(Sage) from Student where Sdept = ‘IS’)and Sdept!=’IS’
(all为满足条件的所有记录,此例表达意为<所有查询到的记录)

带exists 的子查询
1) 查询选修了1号课程的学生的姓名 —————select Sname from Student where exists(select * from SC where Sno = Student.Sno and Cno=’1’)
2)查询选修了课程1或者课程2的学生的学号———— select Sno from SC where Cno = ‘1’ union select Sno from SC where Cno = ‘2’
等价于select Sno from SC where Cno = ‘1’ or Cno = ‘2’
等价于select Sno from SC where Cno in(‘1’ , ‘2’)

1 0