SQL Server - select語句練習

來源:互聯網
上載者:User

標籤:

建立表和輸入資料

CREATE TABLE STUDENT
(SNO VARCHAR(3) NOT NULL,
   SNAME VARCHAR(4) NOT NULL,
   SSEX VARCHAR(2) NOT NULL,
   SBIRTHDAY DATETIME,
   CLASS VARCHAR(5))
go
CREATE TABLE COURSE
(CNO VARCHAR(5) NOT NULL,
   CNAME VARCHAR(10) NOT NULL,
   TNO VARCHAR(10) NOT NULL)
go
CREATE TABLE SCORE
(SNO VARCHAR(3) NOT NULL,
   CNO VARCHAR(5) NOT NULL,
   DEGREE NUMERIC(10, 1) NOT NULL)
go
CREATE TABLE TEACHER
(TNO VARCHAR(3) NOT NULL,
   TNAME VARCHAR(4) NOT NULL, TSEX VARCHAR(2) NOT NULL,
   TBIRTHDAY DATETIME NOT NULL, PROF VARCHAR(6),
   DEPART VARCHAR(10) NOT NULL)

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,‘曾華‘ ,‘男‘ ,1977-09-01,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,‘匡明‘ ,‘男‘ ,1975-10-02,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,‘王麗‘ ,‘女‘ ,1976-01-23,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,‘李軍‘ ,‘男‘ ,1976-02-20,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,‘王芳‘ ,‘女‘ ,1975-02-10,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,‘陸君‘ ,‘男‘ ,1974-06-03,95031);
GO
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-105‘ ,‘電腦導論‘,825)
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-245‘ ,‘作業系統‘ ,804);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘6-166‘ ,‘資料電路‘ ,856);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘9-888‘ ,‘高等數學‘ ,100);
GO
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,‘3-245‘,86);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,‘3-245‘,75);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,‘3-245‘,68);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,‘3-105‘,92);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,‘3-105‘,88);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,‘3-105‘,76);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,‘3-105‘,64);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,‘3-105‘,91);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,‘3-105‘,78);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,‘6-166‘,85);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,‘6-106‘,79);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,‘6-166‘,81);
GO
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (804,‘李誠‘,‘男‘,‘1958-12-02‘,‘副教授‘,‘電腦系‘);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (856,‘張旭‘,‘男‘,‘1969-03-12‘,‘講師‘,‘電子工程系‘);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (825,‘王萍‘,‘女‘,‘1972-05-05‘,‘助教‘,‘電腦系‘);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (831,‘劉冰‘,‘女‘,‘1977-08-14‘,‘助教‘,‘電子工程系‘);

練習題目

1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。
2、 查詢教師所有的單位即不重複的Depart列。
3、 查詢Student表的所有記錄。
4、 查詢Score表中成績在60到80之間的所有記錄。
5、 查詢Score表中成績為85,86或88的記錄。
6、 查詢Student表中“95031”班或性別為“女”的同學記錄。
7、 以Class降序查詢Student表的所有記錄。
8、 以Cno升序、Degree降序查詢Score表的所有記錄。
9、 查詢“95031”班的學生人數。
10、查詢Score表中的最高分的學生學號和課程號。
11、查詢‘3-105’號課程的平均分。
12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
13、查詢最低分大於70,最高分小於90的Sno列。
14、查詢所有學生的Sname、Cno和Degree列。 [笛卡爾積]
15、查詢所有學生的Sno、Cname和Degree列。
16、查詢所有學生的Sname、Cname和Degree列。
17、查詢“95033”班所選課程的平均分。
18、假設使用如下命令建立了一個grade表:
create table grade(low   number(3,0),upp   number(3),rank   char(1));
insert into grade values(90,100,’A’);
insert into grade values(80,89,’B’);
insert into grade values(70,79,’C’);
insert into grade values(60,69,’D’);
insert into grade values(0,59,’E’);
commit;
現查詢所有同學的Sno、Cno和rank列。
19、查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
20、查詢score中選學一門以上課程的同學中分數為非最高分成績的記錄。
21、查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。
22、查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。
23、查詢“張旭“教師任課的學產生績。
24、查詢選修某課程的同學人數多於5人的教師姓名。
25、查詢95033班和95031班全體學生的記錄。
26、查詢存在有85分以上成績的課程Cno.
27、查詢出“電腦系“教師所教課程的成績表。
28、查詢“電腦系”與“電子工程系“不同職稱的教師的Tname和Prof。 [題目與答案不符]
29、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。
30、查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree.
31、查詢所有教師和同學的name、sex和birthday.
32、查詢所有“女”教師和“女”同學的name、sex和birthday.
33、查詢成績比該課程平均成績低的同學的成績表。
34、查詢所有任課教師的Tname和Depart.
35、查詢所有未講課的教師的Tname和Depart.
36、查詢至少有2名男生的班號。
37、查詢Student表中不姓“王”的同學記錄。
38、查詢Student表中每個學生的姓名和年齡。
39、查詢Student表中最大和最小的Sbirthday日期值。
40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。
41、查詢“男”教師及其所上的課程。
42、查詢最高分同學的Sno、Cno和Degree列。
43、查詢和“李軍”同性別的所有同學的Sname.
44、查詢和“李軍”同性別並同班的同學Sname.
45、查詢所有選修“電腦導論”課程的“男”同學的成績表

SQL語句練習題參考答案
1、 select Sname,Ssex,Class from Student;
2、 select distinct depart from teacher;
3、 select Sno as ‘學號‘,Sname as ‘姓名‘,Ssex as ‘性別‘,Sbirthday as‘出生日期‘,Class as‘班號‘from student;

select Sno as 學號,Sname as 姓名,Ssex as 性別,Sbirthday as 出生日期,Class as 班號 from student;
4、 select * from score where degree between 60 and 80;
或select * from score where degree>=60 and degree<=80;
5、 select * from score where degree in (85,86,88);
6、 select * from student where class=‘95031‘or Ssex=‘女‘;
7、 select * from student order by class desc;
8、 select * from score order by cno asc ,degree desc;
或select * from score order by cno ,degree desc;
9、 select count(*) as CNT from student where class=‘95031‘;
10、select Sno as ‘學號‘,cno as ‘課程號‘, degree as ‘最高分‘ from score
where degree=(select max(degree) from score)
11、select avg(degree)as 課程平均分 from score where cno=‘3-105‘;
12、select cno,avg(degree) from score where cno like‘3%‘group by cno having   count(*) >5;
13、select Sno from score group by Sno having min(degree)>70 and max(degree)<90;
14、select student.Sname,score.Cno,score.degree from student,score where student.Sno=score.Sno;
15、select x.Sno,y.Cname,x.degree from score x,course y where x.Cno=y.Cno;
16、select x.Sname,y.Cname,z.degree from student x,course y,score z where x.Sno=z.Sno and z.Cno=y.Cno;
17、select y.Cno,avg(y.degree) from student x,score y where x.Sno=y.Sno and x.class=‘95033‘group by y.cno;
18、select Sno,Cno,rank from score,grade where degree between low and upp order by rank;
19、select x.Cno,x.Sno,x.degree from score x,score y
where x.cno=‘3-105‘ and x.degree>y.degree and y.sno=‘109‘and y.cno=‘3-105‘;
20、
1,查詢成績非本科最高 select *   from score b   where degree <(select max(degree) from score a where a.cno=b.cno);
2,查詢成績非本科最高並且選2門以上的學生的成績:
21、select x.cno,x.Sno,x.degree from score x,score y where x.degree>y.degree and y.sno=‘109‘and y.cno=‘3-105‘;
select cno,sno,degree from score   where degree >(select degree from score where sno=‘109‘ and cno=‘3-105‘)
22、select sno,sname,sbirthday from student where to_char(sbirthday,‘yyyy‘)=(select to_char(sbirthday,‘yyyy‘) from student where sno=‘108‘);
23、select cno,sno,degree from score where cno=(select x.cno from course x,teacher y where x.tno=y.tno and y.tname=‘張旭‘);
24、select tname from teacher where tno in(select x.tno from course x,score y where x.cno=y.cno group by x.tno having count(x.tno)>5);
25、select * from student where class in(‘95033‘,‘95031‘);
26、select distinct cno from score where degree in (select degree from score where degree>85);
27、select * from score where cno in(select x.cno from course x,teacher y where y.tno=x.tno and y.depart=‘電腦系‘);
28、select tname,prof from teacher where depart=‘電腦系‘ and prof not in (select prof from teacher where depart=‘電子工程系‘);
29、select * from score where cno=‘3-105‘ and degree>any (select degree from score where cno=‘3-245‘)order by degree desc;
30、select * from score where cno=‘3-105‘ and degree>all(select degree from score where cno=‘3-245‘);
31、select tname,tsex,tbirthday from teacher
union select sname,ssex,sbirthday from student;
32、select tname,tsex,tbirthday from teacher where tsex=‘女‘
union select sname,ssex,sbirthday from student where ssex=‘女‘;
33、select * from score a where degree<(select avg(degree)
from score b where a.cno=b.cno);
34、select tname,depart from teacher a where exists
(select * from course b where a.tno=b.tno);
35、select tname,depart from teacher a where not exists
(select * from course b where a.tno=b.tno);
36、select class from student where ssex=‘男‘group by class having count(*)>=2;
37、select * from student where sname not like‘王_‘;
38、select sname as 姓名,(to_char(sysdate,‘yyyy‘)-to_char(sbirthday,‘yyyy‘)) as 年齡 from student
39、select sname,sbirthday as 最大 from student where   sbirthday =(select min (sbirthday) from student)
union select sname,sbirthday as 最小 from student where sbirthday =(select max(sbirthday) from student)  
40、select class,sname,sbirthday from student order by class desc,sbirthday;
41、select x.tname,y.cname from teacher x,course y where x.tno=y.tno and x.tsex=‘男‘;
42、select * from score where degree=(select max(degree)from score);
43、select sname from student where ssex=(select ssex from student where sname=‘李軍‘);
44、select sname from student where ssex=(select ssex from student where sname=‘李軍‘) and class=(select class from student where sname=‘李軍‘);
45、select * from score where sno in(select sno from student where ssex=‘男‘) and cno=(select cno from course
where cname=‘電腦導論‘);

SQL Server - select語句練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.