SQL经典实例大全2

一、数据表的查询(select)

  select 字段列表 [as 别名], * from 数据表名

  [where 条件语句]

  [group by 分组字段]

  [order by 排序字段列表 desc]

  [LIMIT startrow,rownumber]

  1、Select 字段列表 From 数据表

  例:①、select id,gsmc,add,tel from haf (* 表示数据表中所有字段)

  ②、select 单价,数量,单价*数量 as 合计金额 from haf (As 设置字段的别名)

  2、Select … from … Where 筛选条件式

  筛选条件式:①、字符串数据: select * from 成绩单 Where 姓名='李明'

  ②、万用字符: select * from 成绩单 Where 姓名 like '李%'

  select * from 成绩单 Where 姓名 like '%李%'

  select * from 成绩单 Where 姓名 like '%李_'

  ③、特殊的条件式:

  ⑴= / > / < / <> / >= / <=

  ⑵AND(逻辑与) OR(逻辑或) NOT(逻辑非)

  ⑶Where 字段名称 in(值一,值二)

  ⑷Where 字段名称 Is Null / Where 字段名称 Is Not Null

  3、Select … from … group by 字段

  SQL函数:

  SELECT sex,count(id) as women from `user` group by 'sex';

  函数名描述函数名描述

  AVG平均值Count计数

  MAX最大值MIN最小值

  Sum求和

  4、Select … from … Order by 字段列表 desc(倒,如果直接写为顺序)

  5、Select … from … LIMIT ".$start_rowno.",".($pagesize+1)

  第二节 SQL语句实例应用

  数据库说明:

  student(学生表):

  stdid int(11) id号

  son char(5) 学号

  sname char(20) 姓名

  ssex tinyint(1) 性别

  sage char(3) 年龄

  sdept char(20) 所在系

  course(课程表):

  couid int(11) id号

  cno char(5) 课程号

  cname char(20) 课程名

  cpno char(6) 选修课号

  ccredit char(50) 学分

  sc(学生选课表):

  scid int(11) id号

  cno char(5) 课程号

  grade float 成绩

  sno char(5) 学号

二、单表查询:

  一)、选择表中的若干字段:

  查询指定列:

  1、查询全体学生的学号与姓名;

  select son,sname from student

  2、查询全体学生的姓名、学号、所在系;

  select sname,son,sdept from student

  3、查询全体学生的详细记录;

  select * from student

  查询经过计算的值:

  4、查全体学生的姓名及其出生年份

  select sname,year(now())-sage as '出生年份' from student

  5、查询全体学生的姓名、出生年份和所有系,要求用大(小)写字母表示所有系名

  select sname as '姓名','出生与',year(now())-sage as '出生年份',UPPER(sdept) as '系别' from student

  select sname as '姓名','出生与',year(now())-sage as '出生年份',lower(sdept) as '系别' from student

  二)、选择表中的若干记录:

  消除取值重复的行:

  6、查询选修了课程的学生学号

  select distinct sno from sc

  查询满足条件的记录:

  比较大小:

  7、查询计算机全体学生的名单

  select sname from student where sdept='cs'

  8、查询所有年龄在20岁以下的学生姓名及其年龄

  select sname,sage from student where sage<20

  9、查询考试成绩小于90分的学生的学号

  select distinct sno from sc where grade<90

  确定范围:

  10、查询年龄在18-20岁之间的学生的姓名、系别和年龄。

  select sname,sdept,sage from student where sage between 18 and 20

  11、查询年龄不在19-20岁之间的学生的姓名、系别和年龄。

  select sname,sdept,sage from student where sage not between 19 and 20

  确定集合:

  12、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。

  select sname,ssex from student where sdept in('is','ma','cs')

  13、查询不是信息系(is)、数学系(ma)的学生的姓名、系别和年龄。

  select sname,ssex from student where sdept not in('is','ma')

  字符匹配(like '<匹配串>' %代表任意长度(长度可以为0)的字符串 ; _代表任意单个字符,汉字得用两个"__"):

  14、查询学号为95001的学生的详细情况

  select * from student where son like '95001'

  15、查询所有姓名李的学生的姓名、学号和性别。

  select sname,son,ssex from student where sname like '李%'

  16、查询姓名是两个字学生的姓名、学号和性别。

  select sname,son,ssex from student where sname like '____'

  17、查询所有不姓李的学生姓名。

  select sname from student where sname not like '李__'

  涉及空值的查询:

  18、某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩,查询缺少成绩的学生的学号和相应的课程号。

  select sno,cno from sc where grade is null

  19、查询所有有成绩的学生学号和课程号。

  select sno,cno from sc where grade is not null

  多重条件查询(and or):

  20、查询计算机系年龄在20岁的学生姓名。

  select sname from student where sdept='cs' and sage=20

  21、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。

  select sname,ssex from student where sdept='is' or sdept='ma' or sdept='cs'

  三)、对查询结果排序:

  22、查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列。

  select sno,grade from sc where cno='3' order by grade desc

  23、查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

  select * from student order by sdept,sage desc

  四、使用集函数:

  24、查询学生总人数。

  select count(*) as '总人数' from student

  25、查询选修了课程的学生人数。

  select count(distinct sno) as '人数' from sc

  26、计算1号课程的学生平均成绩

  select format(avg(grade),2) as '平均成绩' from sc where cno='1'

  27、查询选修1号课程的学生最高分数。

  select max(grade) from sc where cno='1'

  五、对查询结果分组:

  28、求各个课程号及相应的选课人数。

  select cno as '课程号',count(sno) as '人数' from sc group by cno

  29、查询选修了3门以上课程的学生学号。

  select sno from sc group by sno having count(*)>2

  注:where 子句与 having 短语的区别在于作用对象不同,where 子句作用于基本表或视图,从中选择满足条件的记录,having短语作用于组,从中选择满足条件的组。

三、多表查询

  同时查询两个以上的表,称为连接查询。

  等值连接:当连接运算符为=时,为等值连接。

  1、查询每个学生及其选修课程的情况(等值连接)。

  select student.*,sc.* from student,sc where student.son=sc.sno

  自然连接:在等值连接中把目标列中重复的属性列去掉。

  2、查询每个学生及其选修课程的情况(自然连接)。

四、实验

SQL不分大小写!
!表格中的分类无法再增加或减小!

//创建一个名叫php的数据库
create database php

//删除一个名叫php的数据库
DROP DATABASE php

//创建一个表格
create table student(
Snum char(10) not null unique,
Sname char(10)not null,
Ssex char(2),
Sage smallint,
Sphone char(13),
Dnum char(4),
primary key(Snum),
check (Ssex in ('男','女'))
)

create table course(
Cnum char(5) primary key,
Cname char(20),
Cfreq numeric(2,1)
)


create table elect(
Snum char(10),
Cnum char(5),
Score smallint,
check(Score between 0 and 100),
primary key (Snum,Cnum),
foreign key(Snum)references student(Snum),
foreign key(Cnum)references course(Cnum)
)

?

//插入数据
insert into student(Snum,Sname,Ssex,Sage,Sphone,Dnum)
values ('S001','王明','男',19,'86824571','D2')
insert into student(Snum,Sname,Ssex,Sage,Sphone,Dnum)
values ('S002','李勇','男',23,'89454321','D3')
insert into student(Snum,Sname,Ssex,Sage,Sphone,Dnum)
values ('S003','刘燕','女',21,'','D1')
insert into student(Snum,Sname,Ssex,Sage,Sphone,Dnum)
values ('S004','王萍','女',23,'','D1')
insert into student(Snum,Sname,Ssex,Sage,Sphone,Dnum)
values ('S005','王佳','男',24,'13098765892','D3')
insert into student(Snum,Sname,Ssex,Sage,Sphone,Dnum)
values ('S006','赵婷','女',20,'','D1')

insert into course(Cnum,Cname,Cfreq)
values ('C1','数据库系统原理',4)
insert into course(Cnum,Cname,Cfreq)
values ('C2','C程序设计',4)
insert into course(Cnum,Cname,Cfreq)
values ('C3','计算机体系结构',3)
insert into course(Cnum,Cname,Cfreq)
values ('C4','自动控制原理',2)
insert into course(Cnum,Cname,Cfreq)
values ('C5','数据结构',4)


insert into elect(Snum,Cnum,Score)
values ('S001','C1',83)
insert into elect(Snum,Cnum,Score)
values ('S001','C2',89)
insert into elect(Snum,Cnum,Score)
values ('S001','C3',65)
insert into elect(Snum,Cnum,Score)
values ('S001','C4',85)
insert into elect(Snum,Cnum,Score)
values ('S001','C5',69)
insert into elect(Snum,Cnum,Score)
values ('S002','C3',78)
insert into elect(Snum,Cnum,Score)
values ('S002','C4',75)
insert into elect(Snum,Cnum,Score)
values ('S005','C1',95)
insert into elect(Snum,Cnum,Score)
values ('S004','C1',85)
insert into elect(Snum,Cnum,Score)
values ('S005','C2',92)
insert into elect(Snum,Cnum,Score)
values ('S005','C3',76)
//插入数据完毕


drop table student
//删除student表格


select * from student
select * from course
select * from elect
//查找表资料

实验三:

实验内容:

1:查询系编号D2学生的基本信息

select * from student where Dnum='D2'

2:查询学号为S006的学生姓名

select Sname from student where Snum='S006'

3:查询成绩在60~80分的学生学号

select distinct Snum from elect where (Score>=60 and Score<=80)

4:查询所有姓王,并且名字是两个字的学生信息

select * from student where Sname like '王%'

5:查询选修课号为“C1”且成绩非空的学生学号和成绩,成绩按150分制输出

select Snum,Score*1.5 as Score from elect
where cnum = 'c1'and score is not NULL

6:查询有选课记录的所有学生的学号,用DISTINCT限制结果中的学号不重复

select distinct Snum from student
where Dnum is not NULL

7:查询选修课“C1”的学生学号和成绩,结果按成绩升序排列,如果成绩相同则按学号的降序排列

select Snum,score
from elect
where Cnum='C1'
order by Score asc , Snum desc

2.1:查询所有学生的学号、姓名、选修的课程号和成绩

select student.Snum,Sname,Cnum,Score
from student,elect
where student.Snum = elect.Snum

2.2:查询所有学生的姓名、选修的课程名称和相应的学分

select Sname,Cname,Cfreq
from student,course
where Snum in
(select Snum
from elect
where elect.Cnum = course.Cnum
)

?

2.3:查询选修课程号为“C2”且成绩优秀(85分以上)的学生学号、姓名和成绩

select student.Snum,Sname,Score
from student,elect
where Cnum = 'C2' and Score >= 85 and student.Snum = elect.Snum

实验四:

实验内容:

1:查询选修了“计算机体系结构”的学生的基本信息

select *
from student
where Snum in(
select Snum
from elect
where Cnum in(
select Cnum
from course
where Cname = '计算机体系结构'
))

2:查询年龄比李勇小的学生的学号和成绩

select student.Snum,Score
from student,elect
where Sage <(
select Sage
from student
where Sname = '李勇')
and student.Snum = elect.Snum

3:查询其他系中比系编号“D1”的学生中年龄最小者要大的学生信息

select *
from student
where Dnum != 'D1' and Sage >
(select min(Sage)
from student
where Dnum = 'D1')

4:查询其他系中比系编号“D3”的学生年龄都大的学生的姓名

select *
from student
where Dnum != 'D3' and Sage >
(select max(Sage)
from student
where Dnum = 'D3')

5:查询“C1”课程的成绩高于70分的学生姓名

select Sname
from student
where Snum in(
select Snum
from elect
where Cnum = 'C1' and Score > 70 )

6:查询“C1”课程的成绩不高于70分的学生姓名

select Sname
from student
where Snum in(
select Snum
from elect
where Cnum = 'C1' and Score <= 70 )

7:查询选修了所有课程的学生姓名

select Sname
from student
where not exists(
select 1
from course
where not exists(
select 1
from elect
where elect.Snum = student.Snum
and elect.Cnum = course.Cnum))

//第一个not exits把要的答案给排除了,而第二个not exits却把那个被排除的答案又拿回来了

2):利用聚合函数实现以下查询:
2.1:查询学校开设的课程总数

select count(Cnum) as total
from course

2.2:查询选修了两门和两门以上课程的学生姓名

select Sname
from student
where Snum in(
select Snum
from elect
group by Snum
having count(Cnum) >= 2)

2.3:查询开设的课程和选修该课程的学生的总成绩、平均成绩、最高成绩和最低成绩

select Cname as course ,sum(Score) as score_sum,avg(Score) as score_avg,max(Score) as score_max,min(Score) as score_min
from course,elect
where course.Cnum = elect.Cnum
group by Cname

3:利用交、并等运算实现以下查询操作
1)查询系编号为“D2”且选修课程的最低成绩大于等于60的学生信息

select *
from student
where Snum in(
select Snum
from elect
group by Snum
having Dnum = 'D2' and min(Score) >=60)

2)查询系编号为“D2”或选修课程的最低成绩大于等于60的学生信息

select *
from student
where Snum in(
select Snum
from elect
group by Snum
having Dnum =

原文地址:https://www.cnblogs.com/yuanscn/p/11207280.html