SQL 练习语句:
Create table student (s# varchar(10),sname nvarchar(10),
sage datetime,sex nvarchar)
Insert into student values (‘01’,N’赵雷’,’1993-02-07’,N’男’)
Insert into student values (‘02’,N’雅致’,’1992-03-04’,N’女’)
Insert into student values (‘03’,N’李四’,N’1997-10-16’,N’男’)
Creat table course(C# varcha(10),cname ncharvar(10),t#varchar(10))
Insert into course values(‘01’,N’数学’,N’雷奕明’)
Insert into course values(‘02’,N’语文’,N’sharly’)
Creat table teacher(T#varchar(10),Tname nvarchar(10))
Insert into teacher values('01' , N'张三')
Insert into teacher values(‘02’,N’李四’)
Creat table SC(S#varchar(10),C#varchar(10),score decimal(18,1))
Insert into SC values(‘01’,’01’,80)
Insert into SC values(‘01’,’02’,90)
Insert into SC values(‘01’,‘03’,80)
###创建表,以及插入数据
Create table student(Sno varchar(3),Sname varchar(8),Ssex varchar(2),Sbirthday datetime,Class varchar(5))
Create table Course(Cno varchar(5),Cname varchar(10),Tno varchar(3))
Create table Score(Sno varchar(3),Cno varchar(5),Degree Decimal(4,1))
Create table Teacher(Tno varchar(5),Tname varchar(10),Tsex varchar(2),Tbirthday datetime,Prof varchar(6),Depart varchar(10))
Create table student(Sno varchar(3),Sname varchar(8),Ssex varchar(2),Sbirthday datetime,Class varchar(5))
Insert into Student values(‘108’,’曾华’,’男’,’1977-09-01’,’95033’)
Insert into Student values(‘105’,’匡明’,’男’,’1975-10-02’,’95031’)
Insert into Student values(‘107’,’王丽’,’女’,’1976-01-23’,’95033’)
Insert into Student values(‘101’,’李军’,’男’,’1976-02-20’,’95033’)
Insert into Student values(‘109’,’王芳’,’女’,’1975-02-10’,’95031’)
Insert into Student values(‘103’,’陆军’,’男’,’1974-06-03’,’95031’)
Create table Course(Cno varchar(5),Cname varchar(10),Tno varchar(3))
Insert into Course values(‘3-105’,’计算机导论’,’825’)
Insert into Course values(‘3-245’,’操作系统’,’804’)
Insert into Course values(‘6-166’,’数字电路’,’856’)
Insert into Course values(‘9-888’,’高等数学’,’831’)
Create table Score(Sno varchar(3),Cno varchar(5),Degree Decimal(4,1))
Insert into Score(‘103’,’3-245’,’86’)
Insert into Score(‘105’,’3-245’,’75’)
Insert into Score(‘109’,’3-245’,’68’)
Insert into Score(‘103’,’3-105’,’92’)
Insert into Score(‘105’,’3-105’,’88’)
Insert into Score(‘109’,’3-105’,’76’)
Insert into Score(‘101’,’3-105’,’64’)
Insert into Score(‘107’,’3-105’,’91’)
Insert into Score(‘108’,’3-245’,’78’)
Insert into Score(‘101’,’6-166’,’85’)
Insert into Score(‘107’,’6-166’,’79’)
Insert into Score(‘108’,’6-166’,’86’)
Create table Teacher(Tno varchar(5),Tname varchar(10),Tsex varchar(2),Tbirthday datetime,Prof varchar(6),Depart varchar(10))
Insert into Teacher values(‘804’,’李诚’,’男’,’1958-12-02’,’副教授’,’计算机系’)
Insert into Teacher values(‘856’,’张旭’,’男’,’1969-03-12’,’讲师’,’电子工程系’)
Insert into Teacher values(‘825’,’王萍’,’女’,’1972-05-05’,’助教’,’计算机系’)
Insert into Teacher values(‘831’,’刘冰’,’女’,’1977-08-14’,’助教’,’电子工程系’)
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
Select Sname ,ssex, class from student;
2、 查询教师所有的单位即不重复的Depart列。
Select distinct depart from teacher;
3、查询Student表的所有记录。
Select * from student;
4、查询Score表中成绩在60到80之间的所有记录。
Select * from Score
Where degree>=60°ree <=80;##这样写对吗?
或者
Where degree between 60 and 80;
5、查询Score表中成绩为85,86或88的记录。
Select * from score
Where degree in(‘85’,’86’,’88’);
6、查询Student表中“95031”班或性别为“女”的同学记录。
Select * from student
Where class=’95031’
or ssex=’女’;
7、以Class降序查询Student表的所有记录。
Select * from student
Order by class desc;##asc升序
8、以Cno升序、Degree降序查询Score表的所有记录。
Select * from score
Order by cno asc , degree desc;
9、查询“95031”班的学生人数。
Select count(*) from student
Where class=’95031’;###此处涉及到函数的使用
10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
Select sno,cno from student
Where degree=(select max(degree) from student);##查询的嵌套-子查询
下面的 SQL 语句选择价格高于平均价格的"ProductName" 和"Price" 记录:
实例
SELECT ProductName, Price FROM Products
WHERE Price>(SELECT AVG(Price) FROM Products);
与之类似
11、查询每门课的平均成绩,要按照课程分组group by,然后求没门课平均avg
Select avg(degree) ,cno from score
Group by cno;###select中的cno有必要写吗???
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
Select avg(degree)from score
Where(select count(sno)from score)>=5
And cno=’3%’;
Select cno from score
Where cno like‘3%’##like模糊查询,
Group by cno
Having count(*)>5;##having只能跟在group by后面
13、查询分数大于70,小于90的Sno列。
Select sno from score
Where degree between 70 and 90;
14、查询所有学生的Sname、Cno和Degree列。
Select sname,cno,degree from score join student on score.sno=student.sno;
##join 语句 on 加条件