注册 登录
编程论坛 SQL Server论坛

SQL经典练习

dplnk 发布于 2006-03-08 10:48, 5911 次点击

创建以下三个表:

学生表: Student(Sno,Sname,Ssex,Sbirthday,Sage,Sdept)
Student 由学号 (Sno)、姓名 (Sname)、性别 (Ssex)、出生日期 (Sbirthday)、年龄 (Sage),
所在系 (Sdept) 五个属性组成,其中 Sno 为主键.

课程表: Coures(Cno,Cname,Cpno,Ccredit)
Coures 由课程号 (Cno)、课程名 (Cname)、选修课号 (Cpno)、学分 (Ccredit) 四个属性组成,
其中 Cno 为主键.

学生选课表: SC(Sno,Cno,Grade)
SC 由学号 (Sno)、 课程号 (Cno)、 成绩 (Grade) 三个属性组成, 主键为 (Sno,Cno).

按照以上三个表来做如下的习题:

1.查找全体学生的学号与姓名

2.查找全体学生的姓名、学号与所在系

3.查找全体学生的详细纪录

4.查找全体学生的姓名和其出生日期

5.查找全体学生的姓名、出生日期和所有系,要求用小写字母表示所有系名

6.查找全体学生的姓名、出生日期和所有系,要求给这几列起别名

7.查找选修了课程的学生学号(不能重复)

8.查找计算系全体学生的名单

9.查找所有年龄在20岁以下的学生姓名和其年龄(2种做法)

10.查找考试成绩有不及格的学生的学号(不能重复)

11.查找年龄在20--30岁(包括20岁和23岁)之间的学生的姓名、系别和年龄

12.查找年龄不在20--30岁(包括20岁和23岁)之间的学生的姓名、系别和年龄

13.查找信息系、数学系和计算机系学生的姓名与性别(用in做)

14.查找不是信息系、数学系,也不是计算机系的学生的姓名与性别(用not in做)

15.查找学号以为95001开头的学生的详细情况(用2种方法做)

16.查找所有以“刘”开头的学生的姓名、学号和性别

17.查找以“欧阳”开头的且三个汉字的学生的姓名

18.查找名字中第2个字为“阳”字的学生的姓名和学号

19.查找所有不以“刘”开头的学生姓名

20.查找以“C”开头的课程的课程号和学分

21.查找以“H”开头,且倒数第三个字符为“T”的课程的课程号和学分

22.某些学生选修课程后没有参加考试,所以有选课纪录,但没有考试成绩。查找缺少成绩的学生的学号和相应
的课程号

23.查找除了无成绩的学生的学号和课程号

24.查找计算机系年龄在20岁以下的学生姓名

25.查找是计算机系 或者是 数学系 或者是 信息系的学生姓名和性别

26.查找选修了3号课程的学生的学号与其成绩,其查找结果按分数的降序排列

27.查找全体学生情况,查找结果按所在系的系号升序排列,同一系中的学生按年龄降序排列

28.查找学生总人数

29.查找选修了课程的学生人数

30.计算1号课程的学生平均成绩

27 回复
#2
快乐的小菜2006-03-08 23:02


是挺经典的```

#3
xzwmdj2006-03-09 08:50

学习中

#4
日向sakura2006-03-10 11:59
答案呢.............
#5
dplnk2006-03-11 16:59
答案是我做的,这你不会也要把~~~~~~
#6
lwmmaple2006-03-11 21:49
    就是了~~既然都说明是练习啦,当然是自己慢慢研究了`
#7
iceqier2006-03-13 21:41

create table Student
(
Sno varchar(10) not null primary key,
Sname varchar(10) not null,
Ssex varchar(1) not null,
Sbirthday datetime not null,
Sage int not null,
Sdept varchar(20)
)
go
create table Coures
(
Cno int not null primary key,
Cname varchar(20) not null,
Cpno varchar(20) not null,
Ccredit int
)
go
create table SC
(
Sno int not null primary key,
Cno int not null,
Grade int
)
go

--1.
select Sno,Sname from Student

--2.
select Sname,Sno,Sdept from Student

--3.
select * from Student

--4.
select Sname,Sbirthday from Student

--5.
select Sname,Sbirthday,Sdept from Student where Sdept>='a' and Sdept<='z'

--6.
select Sname as 姓名,Sbirthday as 出生日期,Sdept as 系课 from Student

--7.
select distinct Sno from SC where Cno is not null

--8.
select Sname from Student where Sdept='计算机'

--9.
select Sname,Sage from Student where Sage<20 --方法1
select Sname,Sage from Student where not Sage>=20 --方法2

--10.
select distinct Sno from SC where Grade>=60

--11.
select Sname,Sdept,Sage from Student where Sage>=20 and Sage<=30

--12.
select Sname,Sdept,Sage from Student where Sage<20 or Sage>30

--13.
select Sname,Ssex from Student where Sno in('信息','数学','计算机')

--14.
select Sname,Ssex from Student where Sno not in('信息','数学','计算机')

--15.
select * from Student where Sno like '95001%' --方法1
select * from Student where Sno like '[9][5][0][0][1]%' --方法2

--16.
select Sname,Sno,Ssex from Student where Sname like '刘'

--17.
select Sname from Student where Sname like '欧阳_'

--18.
select Sname,Sno from Student where Sname like '_阳_'

--19.
select Sname from Student where Sname not like '刘%'

--20.
select Cno,Ccredit from Coures where Cname like 'C'

--21.
select Cno,Ccredit from Coures where Cname like 'H%T__'

--22.
select Sno,Cno from SC where Grade is null and Cno is not null

--23.
select Sno,Cno from SC where Grade is null

--24.
select Sname from Student where Sdept='计算机' and Sage<20

--25.
select Sname,Ssex from Student where Sdept='计算机' or Sdept='数学' or Sdept='信息'

--26.
select Sno,Grade from SC where Cno=3 order by Grade Desc

--27.题目好像有问题,在创建的三个表中没有找到系号

--28.
select count(*) from Student

--29.
select count(Cno) from SC where Sno is not null

--30.
select avg(Grade) as 平均成绩 from SC where Cno=1

/****************************************

* 我也刚学不久,楼主帮我看看哪里不对 *

****************************************/

#8
rainlily03152006-03-14 12:39
好像是数据库原理这本术上的东西!
#9
xyhyh2006-03-15 09:31
#10
领带2006-03-17 10:13
我没见过 但对我很有用 因为我刚学
#11
领带2006-03-17 10:15

--21.
select Cno,Ccredit from Coures where Cname like 'H%T__'
这一句中T__'是倒数第三吗?

#12
dplnk2006-03-17 13:17
一个 _  代表一位
#13
零下六度2006-03-21 10:09

不错 不错 适合初学者熟悉基本的 SELECT 方法

#14
ca2ju32006-03-22 11:36
顶了,很好的学习东西,
学习ing
#15
晓C→潇潇2006-12-22 20:46

拿走了
谢谢~!

#16
licaiks2007-04-24 14:39
哈哈哈
找到了
谢谢哦
#17
lbaichun2007-04-24 16:22
!!!!!

[ 本帖最后由 lbaichun 于 2015-4-3 17:20 编辑 ]
#18
jiushiwo2007-04-24 18:17

不错,不过几乎是数据库原理第四版的例题,还是辛苦了

#19
jinghot1422007-04-26 22:40

好东西....嘿嘿...谢谢....

#20
yhx6662007-05-08 13:03
大大大大大大顶顶
#21
Santos2009-08-18 20:39
做了一下 以前学的大多数没有忘记
#22
holydie2009-08-26 09:36
拿来练手正好,我新手,谢谢楼主啦
#23
lls4070636742009-09-06 12:18
create table Student
(
Sno varchar(10) not null primary key,
Sname varchar(10) not null,
Ssex varchar(1) not null,
Sbirthday datetime not null,
Sage int not null,
Sdept varchar(20)
)
go
create table Coures
(
Cno int not null primary key,
Cname varchar(20) not null,
Cpno varchar(20) not null,
Ccredit int
)
go
create table SC
(
Sno int not null primary key,
Cno int not null,
Grade int
)
go

--1.
select Sno,Sname from Student

--2.
select Sname,Sno,Sdept from Student

--3.
select * from Student

--4.
select Sname,Sbirthday from Student

--5.
select Sname,Sbirthday,Sdept from Student where Sdept>='a' and Sdept<='z'

--6.
select Sname as 姓名,Sbirthday as 出生日期,Sdept as 系课 from Student

--7.
select distinct Sno from SC where Cno is not null

--8.
select Sname from Student where Sdept='计算机'

--9.
select Sname,Sage from Student where Sage<20 --方法1
select Sname,Sage from Student where not Sage>=20 --方法2

--10.
select distinct Sno from SC where Grade>=60

--11.
select Sname,Sdept,Sage from Student where Sage>=20 and Sage<=30

--12.
select Sname,Sdept,Sage from Student where Sage<20 or Sage>30

--13.
select Sname,Ssex from Student where Sno in('信息','数学','计算机')

--14.
select Sname,Ssex from Student where Sno not in('信息','数学','计算机')

--15.
select * from Student where Sno like '95001%' --方法1
select * from Student where Sno like '[9][5][0][0][1]%' --方法2

--16.
select Sname,Sno,Ssex from Student where Sname like '刘'

--17.
select Sname from Student where Sname like '欧阳_'

--18.
select Sname,Sno from Student where Sname like '_阳_'

--19.
select Sname from Student where Sname not like '刘%'

--20.
select Cno,Ccredit from Coures where Cname like 'C'

--21.
select Cno,Ccredit from Coures where Cname like 'H%T__'

--22.
select Sno,Cno from SC where Grade is null and Cno is not null

--23.
select Sno,Cno from SC where Grade is null

--24.
select Sname from Student where Sdept='计算机' and Sage<20

--25.
select Sname,Ssex from Student where Sdept='计算机' or Sdept='数学' or Sdept='信息'

--26.
select Sno,Grade from SC where Cno=3 order by Grade Desc

--27.题目好像有问题,在创建的三个表中没有找到系号

--28.
select count(*) from Student

--29.
select count(Cno) from SC where Sno is not null

--30.
select avg(Grade) as 平均成绩 from SC where Cno=1
#24
huxiaoxi02232009-09-15 16:50
很全的题目啊,,
#25
olderdream2009-09-21 08:07
多谢分享,学习中!
用空拿来练习练习,刚入门,正需要这些东东!谢谢!
#26
olderdream2009-09-21 08:12
那位老大弄点实例数据呗,好像这样更容易理解!
#27
itcenter12010-05-11 08:06
初学者,谢谢分享
#28
tengbogulang2011-09-04 22:01
看来还要努力学习啊
1