志在指尖
用双手敲打未来

sql查询语句大全讲解及实例

sql查询语句大全讲解及实例

1.创立3张表
//学生表创立
CREATEtablestudent(
SnoCHAR(9)PRIMARYKEY,
SnameCHAR(20)UNIQUE,
Ssexchar(2),
SageSMALLINT,
Sdeptchar(20)
);
//课程表创立
CREATEtablecourse(
Cnochar(4)PRIMARYKEY,
Cnamechar(40)notNULL,
Cpnochar(4),
CcreditSMALLINT
);
//学生选课表创立
CREATEtableSC(
Snochar(9),
Cnochar(4),
GradeSMALLINT
);
2.向表中增加数据
也可运用图形化东西Navicat或其他进行输入,
//向学生表中增加数据
INSERTintoStudentvalues(
201215121,’李勇’,’男’,20,’CS’),
(201215122,’刘晨’,’女’,19,’CS’),
(201215123,’王敏’,’女’,18,’MA’),
(201215125,’张立’,’男’,19,’IS’
);sql
//向课程表中增加数据
insertintocourseVALUES(
‘1’,’数据库’,’5′,4),
‘2’,’数学’,”,2),
‘3’,’信息系统’,’1′,4),
(‘4′,’操作系统’,’6′,3),
(‘5′,’数据结构’,’7′,4),
(‘6′,’数据处理’,”,2),
(‘7′,’Java言语’,’6′,4)
//向学生选课表中增加数据
insertintoscvalues
(201215121,1,92),
(201215121,2,85),
(201215121,3,88),
(201215122,2,58),
(201215122,3,80)
3.数据查询
3.1单表查询
3.1.1查询全体学生的学号与名字
selectSno,Sname
fromstudent
1
2
3.1.2查询全体学生的名字,学号,地点系
selectSname,Sno,Sdept
fromstudent
31.3查询全体学生的详细记载
select*fromstudent
3.1.4查询全体学生的名字及其出世年份
selectSname,2020-Sage
fromStudent
3.1.5查询全体学生的名字,出世年份和地点的院系,要求用小写字母表示系名
selectSname,2020-Sage,lower(Sdept)
fromstudent
1
2
3.1.6查询选修了课程的学生学号
selectSno
fromSC
3.1.7查询计算机科学系全体学生的名单
selectSname
fromstudent
whereSdept=’CS’
3.1.8查询一切年纪在20岁以下的学生名字及其年纪
selectSname,Sage
fromstudent
whereSage<20
3.1.9查询考试成果不及格的学生的学号
selectSno
fromsc
whereGrade<60
3.1.10查询年纪在20-23岁(包含20和23)之间的学生的名字,系别,年纪
selectSname,Sdept,Sage
fromstudent
whereSagebetween20and23
3.1.11查询年纪不中20-23之间的学生名字,系别,年纪
selectSname,Sdept,Sage
fromstudent
whereSagenotbetween20and23
3.1.12查询计算机科学系(CS),数学系(MA),信息系(IS)学生的名字和性别
selectSname,Ssex
fromstudent
whereSdeptin(‘CS’,’MA’,’IS’)
3.1.13查询既不是CS,MA,也不是IS的学生的名字和性别
selectSname,Ssex
fromstudent
whereSdeptnotin(‘CS’,’MA’,’IS’)
3.1.14查询学号为201215121的学生的详细情况
select*
fromstudent
whereSno=’201215121′
3.1.15查询一切姓刘的学生的名字,学号,性别
selectSname,Sno,Ssex
fromstudent
whereSnamelike’刘%’
3.1.16查询姓欧阳且全名为3个汉字的学生的名字
selectSname
fromstudent
whereSnamelike’欧阳_’
3.1.17查询名字中第二个字为“阳”的学生的名字和性别
selectSname,Ssex
fromstudent
whereSnamelike’_阳%’
3.1.18查询一切不姓刘的学生的名字,学号和性别
selectSname,Sno,Ssex
fromstudent
whereSnamenotlike’刘%’
3.1.19查询短少成果的学生的学号和呼应的课程号
selectSno,Cno
fromsc
wheregradeisnull
3.1.20查询一切有成果的学生的学号和课程号
selectSno,Cno
fromsc
wheregradeisnotnull
3.1.21查询计算机科学系且年纪在20岁以下的学生的名字
selectSname
fromstudent
whereSdept=’CS’andSage<=20
3.1.22查询选修了3号课程的学生的学号及其成果,查询结果按分数的降序摆放
//orderby默许升序,ASC是升序,DESC是降序
selectSno,Grade
fromsc
whereCno=’3′
orderbyGradedesc
3.1.23查询全体学生情况,查询结果按地点系的系号升序摆放,同一系的学生按年纪降序摆放
select*
fromstudent
orderbySdept,SageDESC
3.1.24查询学生总人数
selectcount(*)
fromstudent
3.1.25查询选修了课程的学生人数
//学生可以选多门课程,防止重复需在count函数里加distinct短语
selectcount(distinctSno)
fromsc
3.1.26计算选修1号课程的学生均匀成果
selectavg(Grade)
fromsc
whereCno=’1′
3.1.27查询选修1号课程的学生最高分数
selectmax(Grade)
fromsc
whereCno=’1′
3.1.28查询学生201215121选修课程的总学分数
selectsum(Grade)
fromsc
whereSno=’201215121′
3.1.29求各个课程号及相应的选课人数
//groupup是将查询结果按某个特点进行分组
selectCno,Count(Sno)
fromsc
groupbyCno
3.1.30查询选修了3门以上课程的学生学号
//having效果于组,这儿先用groupby按Sno进行分组,再用调集函数count对每一组进行计数,用having提取出满足条件的组
selectSno
fromsc
groupbySno
havingcount(*)>3
3.1.31查询均匀成果大于等于90分的学生学号和均匀成果
//where句中不能用调集函数作为条件表达式
selectSno,avg(Grade)
fromsc
groupbySno
havingavg(Grade)>=90
3.2衔接查询
3.2.1查询每个学生及其选修课的情况
selectstudent.*,sc.*
fromStudent,sc
wherestudent.Sno=sc.Sno
3.2.2将上面的例子用天然衔接完结
selectstudent.Sno,Sname,Ssex,Sage,Sdept,Cno,grade
fromstudent,sc
wherestudent.sno=sc.sno
3.2.3查询选修2号课程且成果在90分以上的一切学生的学号和名字
selectstudent.Sno,Sname
fromStudent,sc
wherestudent.Sno=sc.Snoand
sc.Cno=’2’and
sc.Grade>=90
3.2.4查询每一门课的间接先修课(先修课的先修课)
//先对一门课找到其先修课,再按此先修课的课程号查找它的先修课,
//将表与本身衔接,就要取别名
selectFIRST.Cno,second.Cpno
fromCoursefirst,CourseSECOND
where`first`.Cpno=`SECOND`.Cno
1
3.2.5查询每个学生的学号,名字,选修的课程名及成果
selectstudent.Sno,Sname,Cname,Grade
fromstudent,sc,course
wherestudent.Sno=sc.SnoAND
sc.Cno=course.Cno
3.3嵌套查询
3.3.1查询与刘晨在同一个系学习的学生
selectSno,Sname,Sdept
fromstudent
whereSdeptin(
selectSdept
fromstudent
whereSname=’刘晨’
)
3.3.2查询选修了课程名为“信息系统”的学生学号和名字
//嵌套查询太多了,用衔接查询呈现出来
selectstudent.Sno,Sname
fromstudent,sc,course
wherestudent.Sno=sc.Snoand
sc.Cno=course.Cnoand
course.Cname=’信息系统’
3.3.3找出每个学生超过他自己选修课程均匀成果的课程号
selectSno,Cno
fromscx
whereGrade>=(
selectavg(Grade)
fromscy
wherey.Sno=x.Sno
)
3.3.4查询非计算机系中比计算机系恣意学生年纪小的学生名字和年纪
//恣意:any一切:all
selectSname,Sage
fromstudent
whereSageselectSage
fromstudent
whereSdept=’CS’
3.3.5查询非计算机系中比计算机系一切学生年纪都小的学生名字和年纪
selectSname,Sage
fromstudent
whereSageselectSage
fromstudent
whereSdept=’CS’
3.3.6查询一切选修了1号课程的学生名字
selectSname
fromstudent,sc
wherestudent.Sno=sc.Snoand
sc.Cno=’1′
3.3.7查询选修了悉数课程的学生名字
selectSname
fromstudent
wherenotexists(
select*
fromcourse
wherenotexists(
SELECT*
fromsc
whereSno=student.SnoAND
Cno=course.Cno
)
)
3.4调集查询
3.4.1查询选修了1号课程或则2号课程的学生
selectSno
fromsc
whereCno=’1′
UNION
selectSno
fromsc
whereCno=’2′
3.4.2查询既选修了课程1又选修了课程2的学生
selectSno
fromsc
whereCno=’1′
intersert
selectSno
fromsc
whereCno=’2′
4.数据更新
4.1插入数据
4.1.1将一个新学生元组(学号:201215128,名字:陈东,性别:男,系别:IS,年纪:18岁)
insertintostudent
values(‘201215128′,’陈东’,’男’,18,’IS’)
1
2
4.1.2插入一条选课记载
insertintosc(Sno,Cno)VALUES(‘201215128′,’1′)
1
4.1.3对每一个系,求学生的均匀年纪,并把结果存入数据库
//首要创立一张表来存放数据
createtableDeptage(
Sdeptchar(15),
avg_agesmallint
)
//计算数据,存放到表中
insertintoDeptage(Sdept,avg_age)
selectSdept,avg(Sage)
fromstudent
groupbySdept
4.2修正数据
4.2.1将学生201215121的年纪改为22岁
updatestudent
setSage=22
whereSno=’201215121′
4.2.2将一切学生的年纪增加1岁
updatestudent
setSage=Sage+1
1
2
4.2.3将计算机系全体学生的成果置0
updatestudent
setSage=0
whereSdept=’CS’
4.3删去数据
4.3.1删去学号为201215128的学生记载
delete
fromstudent
whereSno=’201215128′
4.3.2删去计算机系一切学生的选课记载
delete
fromsc
whereSnoin(
selectSno
fromstudent
whereSdept=’CS’
)
4.3.3删去学生表一切记载
truncatstudent//该句子是删去该张表,从头创立表,不是一条一条删去表中数据;且truncat只能效果于表,delete,drop可效果于表,视图
1
5.常用关键字总结
5.1distinct
5.1.1效果于单列
selectdistinct列名//去重
1
5.1.2效果多列
selectdistinct列名1,列名2,列名3//对3列都去重
1
5.1.3与函数一同运用
selectcount(distinct列名)//计算不重复的列名个数

未经允许不得转载:IT技术网站 » sql查询语句大全讲解及实例
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载