標籤:
學了不少東西,感覺自己的sql語句還是很不好,從基礎學起吧。
來一段sql指令碼:
create database tongjigouse tongjigocreate table student(Sno varchar(20) not null primary key ,--學號Sname varchar(20) not null,--學生姓名Ssex varchar(20) not null, --學生性別Sbirthday datetime,--學生出生年月Class varchar(20)--學生所在班級)gocreate table teacher--老師(Tno varchar(20) not null primary key ,--教工編號(主碼)Tname varchar(20) not null,--教工姓名Tsex varchar(20) not null, --教工性別Tbirthday datetime,--教工出生年月Prof varchar(20),--職稱Depart varchar(20) not null--教工所在部門)gocreate table Course--課程(Cno varchar(20) not null primary key ,--課程號Cname varchar(20) not null,--課程名稱Tno varchar(20) not null references teacher(Tno), --教工編號(外碼))gocreate table Score--分數(Sno varchar(20) not null references student(Sno), --學號(外碼)Cno varchar(20) not null references Course(Cno), --課程號(外碼)primary key(Sno,Cno),Degree Decimal(4,1),--成績)
開始寫練習sql語句:
-- 1.查詢Student表中的所有記錄的Sname、Ssex和Class列。SELECT Sname,Ssex,Class FROM student--2.查詢教師所有的單位即不重複的Depart列。SELECT DISTINCT depart FROM teacher--3.查詢Score表中成績在60到80之間的所有記錄。SELECT * FROM Score WHERE degree BETWEEN 60 AND 80--4.查詢Score表中成績為85,86或88的記錄。select * from Score where degree in(85,86,88)--5.查詢Student表中“95031”班或性別為“女”的同學記錄。select * from student where [class]=‘95031‘ and Ssex=‘女‘--6.以Class降序查詢Student表的所有記錄。select * from student order by class desc--7.以Cno升序、Degree降序查詢Score表的所有記錄。select * from Score order by cno,degree desc--8.查詢“95031”班的學生人數。select COUNT(*) from student where Class=‘95031‘--9. 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)select Sno,Cno from Score where degree=(select MAX(degree) from score)--10.查詢每門課的平均成績。select cno,AVG(degree) from Score group by Cno--11.查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。select AVG(degree) from Score where cno like‘3%‘ group by Cno having COUNT(*)>=5select Avg(degree) from Score group by Cno having COUNT(*)>=5 and Cno like‘3%‘--12.查詢分數大於70,小於90的Sno列。select sno from Score where degree>70 and DEGREE<90--13.查詢所有學生的Sname、Cno和Degree列。select sname,cno,degree from score join student on score.sno = student.sno--14.查詢所有學生的Sno、Cname和Degree列。select sno,cname,degree from score join course on score.cno=course.cno--15.查詢所有學生的Sname、Cname和Degree列。select sname,cname,degree from score join student on student.sno=score.sno join course on score.cno = course.cnoselect sname,cname,degree from score,student,course where student.sno=score.sno and score.cno = course.cno
SqlServer基礎語句練習(一)