SQL练习题

  • 其它

准备数据 : 测试表格

-数据库文件:

-1.学生表

Student(s_id,s_name,s_birth,s_sex) -学生编号,学生姓名出生年月,学生性别

-2.课程表

Course(c_id,c_name,t_id) ––课程编号课程名称教师编号

-3.教师表

Teacher(t_id,t_name) –教师编号,教师姓名

-4.成绩表

Score(s_id,c_id,s_score) -学生编号,课程编号,分数

---------------------

--建表

 

–-1.学生表

 

CREATE TABLE `Student`(

 

    `s_id` VARCHAR(20),

 

    `s_name` VARCHAR(20) NOT NULL DEFAULT '',

 

    `s_birth` VARCHAR(20) NOT NULL DEFAULT '',

 

    `s_sex` VARCHAR(10) NOT NULL DEFAULT '',

 

    PRIMARY KEY(`s_id`)

 

);

 

--课程表

 

CREATE TABLE `Course`(

 

    `c_id`  VARCHAR(20),

 

    `c_name` VARCHAR(20) NOT NULL DEFAULT '',

 

    `t_id` VARCHAR(20) NOT NULL,

 

    PRIMARY KEY(`c_id`)

 

);

 

--教师表

 

CREATE TABLE `Teacher`(

 

    `t_id` VARCHAR(20),

 

    `t_name` VARCHAR(20) NOT NULL DEFAULT '',

 

    PRIMARY KEY(`t_id`)

 

);

 

--成绩表

 

CREATE TABLE `Score`(

 

    `s_id` VARCHAR(20),

 

    `c_id`  VARCHAR(20),

 

    `s_score` INT(3),

 

    PRIMARY KEY(`s_id`,`c_id`)

 

);

 

--插入学生表测试数据

 

insert into Student values('01' , '赵雷' , '1990-01-01' , '');

 

insert into Student values('02' , '钱电' , '1990-12-21' , '');

 

insert into Student values('03' , '孙风' , '1990-05-20' , '');

 

insert into Student values('04' , '李云' , '1990-08-06' , '');

 

insert into Student values('05' , '周梅' , '1991-12-01' , '');

 

insert into Student values('06' , '吴兰' , '1992-03-01' , '');

 

insert into Student values('07' , '郑竹' , '1989-07-01' , '');

 

insert into Student values('08' , '王菊' , '1990-01-20' , '');

 

--课程表测试数据

 

insert into Course values('01' , '语文' , '02');

 

insert into Course values('02' , '数学' , '01');

 

insert into Course values('03' , '英语' , '03');

 

 

 

--教师表测试数据

 

insert into Teacher values('01' , '张三');

 

insert into Teacher values('02' , '李四');

 

insert into Teacher values('03' , '王五');

 

 

 

--成绩表测试数据

 

insert into Score values('01' , '01' , 80);

 

insert into Score values('01' , '02' , 90);

 

insert into Score values('01' , '03' , 99);

 

insert into Score values('02' , '01' , 70);

 

insert into Score values('02' , '02' , 60);

 

insert into Score values('02' , '03' , 80);

 

insert into Score values('03' , '01' , 80);

 

insert into Score values('03' , '02' , 80);

 

insert into Score values('03' , '03' , 80);

 

insert into Score values('04' , '01' , 50);

 

insert into Score values('04' , '02' , 30);

 

insert into Score values('04' , '03' , 20);

 

insert into Score values('05' , '01' , 76);

 

insert into Score values('05' , '02' , 87);

 

insert into Score values('06' , '01' , 31);

 

insert into Score values('06' , '03' , 34);

 

insert into Score values('07' , '02' , 89);

 

insert into Score values('07' , '03' , 98);

 

--------------------- 

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

  SELECT A.s_id,A.s_score

FROM (SELECT * FROM score WHERE c_id=1) A,

     (SELECT * FROM score WHERE c_id=2 ) B

WHERE A.s_id=B.s_id

   AND A.s_score>B.s_score;

-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

  SELECT A.s_id,A.s_score

FROM (SELECT * FROM score WHERE c_id=1) A,

     (SELECT * FROM score WHERE c_id=2 ) B

WHERE A.s_id=B.s_id

   AND A.s_score>B.s_score;

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT A.s_id,B.s_name,AVG(A.s_score)

FROM Score A,Student B

WHERE A.s_id=B.s_id

GROUP BY A.s_id,B.s_name

HAVING AVG(A.s_score)>=60;

-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

SELECT A.s_id,B.s_name,AVG(A.s_score)

FROM Score A,Student B

WHERE A.s_id=B.s_id

GROUP BY A.s_id,B.s_name

HAVING AVG(A.s_score)<60

or AVG(A.s_score) is null;

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT E.s_id,E.s_name,COUNT(C.c_id)OVER(PARTITION BY C.s_id,E.s_name),

SUM(C.s_score)OVER(PARTITION BY C.s_id,C.c_id)

FROM Student E,Score C

WHERE E.s_id=C.s_id;

-- 6、查询""姓老师的数量

SELECT COUNT(t_id)

FROM Teacher

WHERE t_name LIKE '%';

-- 7、查询学过"张三"老师授课的同学的信息

SELECT S.*

FROM Student S,Score  C

WHERE S.s_id=C.s_id AND C.c_id IN

(SELECT E.c_id FROM Course E,Teacher T

WHERE E.c_name=T.t_name AND T.t_name='张三');

-- 8、查询没学过"张三"老师授课的同学的信息

SELECT  S.*

FROM Student S WHERE NOT EXISTS

(SELECT s_id  FROM Score X,Course C,Teacher T

WHERE X.c_id=C.c_id AND C.t_id=T.t_id AND T.t_name='张三' AND S.s_id=X.s_id)

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

SELECT DISTINCT S.* FROM Score C,Student S

WHERE S.S_ID=C.S_ID AND C_ID=1

UNION

SELECT DISTINCT S.* FROM Score C,Student S

WHERE S.S_ID=C.S_ID AND C_ID=2

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT DISTINCT S.* FROM Score C,Student S

WHERE S.S_ID=C.S_ID AND C_ID=1

UNION

SELECT  DISTINCT S.* FROM Student S

WHERE NOT EXISTS

(SELECT s_id  FROM Score C WHERE C_ID=2  AND S.s_id=C.s_id)

-- 11、查询没有学全所有课程的同学的信息

SELECT S.* FROM Student S,Score C

WHERE S.S_ID=C.S_ID

GROUP BY S.S_ID,S.s_name

HAVING COUNT(C.C_ID)!=(SELECT COUNT(C_ID)FROM COURSE);

-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT DISTINCT S.* FROM STUDENT S,SCORE C

WHERE S.S_ID!=1 AND C.C_ID  IN

(SELECT C_ID FROM SCORE WHERE S_ID=1);

-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

SELECT DISTINCT S.*

FROM STUDENT S,SCORE C

WHERE S.S_ID=C.S_ID AND C.S_ID!=1  

GROUP BY S.S_ID,S.S_NAME

HAVING COUNT( C.C_ID)=

(SELECT COUNT(C_ID) FROM SCORE WHERE S_ID=1);

-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名

SELECT S_ID,S_NAME FROM STUDENT WHERE S_ID NOT IN

(SELECT C.S_ID

FROM SCORE C,COURSE E,TEACHER T

WHERE  C.C_ID=E.C_ID AND E.T_ID=T.T_ID AND T.T_NAME='张三' );

-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select s_name,avg(s_score)

from Student S,Score sc

where s.s_id=sc.s_id and s_score<60

group by s_name

having count(s_score)>=2

-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息

SELECT S_ID FROM SCORE

WHERE C_ID=1 AND S_SCORE<60

ORDER BY S_SCORE DESC;

-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select distinct A.c_id,c_name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

from Score A

left join Course on A.c_id=Course.c_id

left join (select c_id,MAX(s_score) 最高分,

MIN(s_score) 最低分,AVG(s_score) 平均分

from Score  group by c_id)B  on A.c_id=B.c_id

left join (select c_id,(convert(decimal(5,2),

(sum(case when s_score>=60  then 1 else 0 end)*1.00)/COUNT(*))*100) 及格率

from Score group by c_id)C  on A.c_id=C.c_id

left join (select c_id,(convert(decimal(5,2),

(sum(case when s_score >=70 and s_score<80 then 1 else 0 end)*1.00)/COUNT(*))*100) 中等率

from Score group by c_id)D on A.c_id=D.c_id

left join (select c_id,(convert(decimal(5,2),

(sum(case when s_score >=80 and s_score<90 then 1 else 0 end)*1.00)/COUNT(*))*100) 优良率

from Score group by c_id)E  on A.c_id=E.c_id

left join (select c_id,(convert(decimal(5,2),

(sum(case when s_score >=90 then 1 else 0 end)*1.00)/COUNT(*))*100) 优秀率

 from Score group by c_id)F  on A.c_id=F.c_id

-- 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

 

select *,RANK()over(order by s_score desc)排名 from Score

-- 19、查询下月过生日的学生           (sql server)

select s.* from Student s

where to_char(s.s_birth, 'mm')=to_char(add_months(trunc(sysdate),1),'mm')

 

-- 20、查询本月过生日的学生            (sql server)

select s.* from Student s

 where to_char(s.s_birth, 'mm') = to_char(sysdate,'mm')

 

-- 21、查询下周过生日的学生            (sql server)

select s.* from student s

 where to_char(s.s_birth, 'mmdd') between

to_char(trunc(sysdate, 'iw')+7, 'mmdd') and       

to_char(trunc(sysdate, 'iw') + 13, 'mmdd')

 

-- 22、查询本周过生日的学生            (sql server)

select st.*  from student s

 where to_char(s.s_birth, 'mmdd')  between

to_char(trunc(sysdate, 'iw'), 'mmdd') and

to_char(trunc(sysdate, 'iw') + 6, 'mmdd')

 

-- 23、查询各学生的年龄,按照出生日期来算,当前月日 出生年月的月日则,年龄减一            (sql server)

select s.*, (to_char(sysdate, 'yyyy') - to_char(s.s_birth, 'yyyy')) "年龄"  from student s

 

-- 24、查询选修了全部课程的学生信息

SELECT S.* FROM STUDENT S,SCORE C

WHERE S.S_ID=C.S_ID

GROUP BY S.S_ID,S.s_name

HAVING COUNT(C.C_ID) IN (SELECT COUNT(C_ID)FROM COURSE);

-- 25、检索至少选修两门课程的学生学号

SELECT S_ID,COUNT(C_ID)FROM SCORE GROUP BY S_ID HAVING COUNT(C_ID)>2;

-- 26、查询每门功成绩最好的前两名

select c_id,COUNT(s_id)选修人数 from Score

group by c_id

having COUNT(s_id)>5

order by 选修人数 desc,c_id

-- 27、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT S.S_ID,C.S_SCORE

FROM STUDENT S,SCORE C,COURSE E,TEACHER T

WHERE S.S_ID=C.S_ID AND C.C_ID=E.C_ID AND E.T_ID=T.T_ID AND T.T_NAME='张三'

AND C.S_SCORE=(SELECT MAX(S_SCORE)FROM SCORE WHERE C_ID=C.C_ID );

-- 28、查询所有学生的课程及分数情况

select a.s_id,a.s_name,b.s_id,c.c_name,b.s_score

         from student a,Score b,Course c

         where a.s_id=b.s_id and b.c_id=c.c_id;

-- 29、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

SELECT S.S_ID,S_NAME,AVG(S_SCORE) FROM STUDENT S,SCORE C

WHERE S.S_ID=C.S_ID GROUP BY S.S_ID,S_NAME

HAVING AVG(S_SCORE)>=85;

-- 30、查询同名同性学生名单,并统计同名人数

select s_name,count(s_name) from student

 group by s_name having count(s_name)>1 ;