內容:sql
– 1.學生表
Student(S,Sname,Sage,Ssex) – S 學生編號,Sname 學生姓名,Sage 出生年月,Ssex 學生性別
– 2.課程表
Course(C,Cname,T) – C – 課程編號,Cname 課程名稱,T 教師編號
– 3.教師表
Teacher(T,Tname) – T 教師編號,Tname 教師姓名
– 4.成績表
SC(S,C,score) – S 學生編號,C 課程編號,score 分數
*/
– 建立測試數據函數
create table Student(S varchar(10),Sname varchar(10),Sage datetime,Ssex nvarchar(10));
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' , '女');
create table Course(C varchar(10),Cname varchar(10),T varchar(10));
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數學' , '01');
insert into Course values('03' , '英語' , '03');
create table Teacher(T varchar(10),Tname varchar(10));
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
create 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' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);學習
題目
一、查詢」01」課程比」02」課程成績高的學生的信息及課測試
SELECT stu.*, sc.C,sc.score FROM student stu,sc sc1,sc sc2,sc sc
WHERE stu.S = sc.s AND stu.S = sc1.S AND stu.S = sc2.S AND sc1.C = '01' AND sc2.C = '02' AND sc1.score > sc2.score.net
二、查詢」01」課程比」02」課程成績低的學生的信息及課程分數排序
SELECT stu.*, sc.C, sc.score FROM student stu, sc sc1, sc sc2, sc sc
WHERE stu.S = sc.s AND stu.S = sc1.S AND stu.S = sc2.S AND sc1.C = '01' AND sc2.C = '02' AND sc1.score < sc2.scoreci
三、查詢平均成績大於等於60分的同窗的學生編號和學生姓名和平均成績get
– 注:where後面不能有彙集函數,如sum(),avg()等,而having能夠數學
SELECT stu.S AS '學號', stu.Sname AS '姓名', AVG(sc.score) AS '平均成績' FROM student stu, sc sc
WHERE stu.S = sc.s GROUP BY sc.s HAVING AVG(sc.score) >= 60it
四、查詢平均成績小於60分的同窗的學生編號和學生姓名和平均成績
SELECT stu.S AS '學號', stu.Sname AS '姓名', AVG(sc.score) AS '平均成績'
FROM student stu, sc sc
WHERE stu.S = sc.s GROUP BY sc.s HAVING AVG(sc.score) < 60
五、查詢全部同窗的學生編號、學生姓名、選課總數、全部課程的總成績
SELECT
stu.s AS '學號',
stu.Sname AS '姓名',
COUNT(sc.C),
SUM(sc.score)
FROM
student stu,
sc sc
WHERE
1 = 1
AND stu.s = sc.s
GROUP BY
sc.s
六、查詢」李」姓老師的數量
SELECT
COUNT(*)
FROM
teacher t
WHERE
t.tname LIKE CONCAT('李', '%');
七、查詢學過」張三」老師授課的同窗的信息
SELECT
stu.*
FROM
student stu,
sc sc,
teacher t
WHERE
1 = 1
AND stu.S = sc.s
AND sc.c = t.T
AND sc.C IN (
SELECT
c.C
FROM
teacher t,
course c
WHERE
t.T = c.T
AND Tname = '張三'
);
八、查詢沒學過」張三」老師授課的同窗的信息
SELECT
*
FROM
student
WHERE
s NOT IN (
SELECT
stu.S
FROM
student stu,
sc sc,
teacher t
WHERE
1 = 1
AND stu.S = sc.s
AND sc.c = t.T
AND sc.C IN (
SELECT
c.C
FROM
teacher t,
course c
WHERE
t.T = c.T
AND Tname = '張三'
)
);
九、查詢學過編號爲」01」而且也學過編號爲」02」的課程的同窗的信息
SELECT
stu.*
FROM
student stu,
sc sc1,
sc sc2
WHERE
stu.s = sc1.s
AND stu.s = sc2.s
AND sc1.c = '01'
AND sc2.c = '02';
十、查詢學過編號爲」01」可是沒有學過編號爲」02」的課程的同窗的信息
SELECT
stu.*
FROM
student stu,
sc sc
WHERE
stu.s = sc.s
AND sc.c = '01'
AND NOT EXISTS (
SELECT
1
FROM
sc sc2
WHERE
sc2.s = sc.s
AND sc2.c = '02'
);
十一、查詢沒有學全全部課程的同窗的信息
SELECT
stu.*
FROM
student stu,
sc sc
WHERE
sc.s = stu.s
GROUP BY
sc.s
HAVING
COUNT(sc.c) < (SELECT COUNT(*) FROM course);
十二、查詢至少有一門課與學號爲」01」的同窗所學相同的同窗的信息
SELECT DISTINCT
student.*
FROM
student,
sc
WHERE
student.S = sc.s
AND sc.c IN (SELECT c FROM sc WHERE s = '01')
AND student.s <> '01';
1三、查詢和」01」號的同窗學習的課程徹底相同的其餘同窗的信息
SELECT
s.*
FROM
(
SELECT
sc.s,
group_concat(sc.c ORDER BY c) AS c01
FROM
sc sc
WHERE
sc.s <> '01'
GROUP BY
sc.s
) a,
(
SELECT
group_concat(sc.c ORDER BY c) AS c01
FROM
sc sc
WHERE
sc.s = '01'
) b,
student s
WHERE
a.c01 = b.c01
AND a.s = s.s;
1四、查詢沒學過」張三」老師講授的任一門課程的學生姓名
SELECT
stu.sname
FROM
student stu
WHERE
stu.s NOT IN (
SELECT DISTINCT
sc.s
FROM
sc sc,
course c,
teacher t
WHERE
sc.c = c.C
AND c.T = t.T
AND t.tname = '張三'
);
1五、查詢兩門及其以上不及格課程的同窗的學號,姓名及其平均成績
SELECT
s.s,
s.sname,
avg(sc.score)
FROM
student s,
sc sc
WHERE
s.s = sc.s
AND s.s IN (
SELECT
sc1.s
FROM
sc sc1
WHERE
sc1.score < 60
GROUP BY
sc1.s
HAVING
count(1) > 1
)
GROUP BY
s.s,
s.sname;
1六、檢索」01」課程分數小於60,按分數降序排列的學生信息
SELECT DISTINCT
s.*, sc.score AS score01
FROM
student s,
sc sc
WHERE
s.s = sc.s
AND sc.c = '01'
AND sc.score < 60
ORDER BY
sc.score DESC;
1七、按平均成績從高到低顯示全部學生的全部課程的成績以及平均成績
SELECT
a.s,
a.sname,
sum(IFNULL(a.score01, 0)) AS score01,
sum(IFNULL(a.score02, 0)) AS score02,
sum(IFNULL(a.score03, 0)) AS score03,
(
sum(IFNULL(a.score01, 0)) + sum(IFNULL(a.score02, 0)) + sum(IFNULL(a.score02, 0))
) / 3 AS avgScore
FROM
(
SELECT s.s,s.sname,
CASE
WHEN sc.c = '01' THEN
ifnull(sc.score, 0)
END AS score01,
CASE
WHEN sc.c = '02' THEN
ifnull(sc.score, 0)
END AS score02,
CASE
WHEN sc.c = '03' THEN
ifnull(sc.score, 0)
END AS score03
FROM
student s
LEFT JOIN sc sc ON s.s = sc.s
) a
GROUP BY
a.s,
a.sname
order by avgScore desc;
1八、查詢各科成績最高分、最低分和平均分:以以下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率
– 及格爲>=60,中等爲:70-80,優良爲:80-90,優秀爲:>=90
SELECT sc.c AS 課程ID, c.Cname AS 課程name, MAX(sc.score) AS 最高分, MIN(sc.score) AS 最低分, AVG(sc.score) AS 平均分,
( (SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 60 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '及格率(%)',
( ( SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 70 AND sc1.score < 80 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '中等率(%)',
( ( SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 80 AND sc1.score < 90 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '優良率(%)',
( ( SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 90 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '優秀率(%)'
FROM sc sc, course c WHERE 1 = 1 AND sc.c = c.c GROUP BY sc.c ORDER BY score;
1九、按各科成績進行排序,並顯示排名
# 實現的過程有點問題
CREATE view result as
SELECT stu.S,stu.Sname,c.Cname,sc.score
FROM student stu,sc sc,course c
WHERE stu.s = sc.s AND c.c = sc.c GROUP BY sc.c, sc.s;
SELECT tmp.S,tmp.Sname,tmp.Cname,tmp.score,@j :=@j + 1 AS j
FROM(SELECT * FROM result) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
where tmp.Cname = '語文' order by tmp.score desc;
SELECT tmp.S,tmp.Sname,tmp.Cname,tmp.score,@j :=@j + 1 AS j
FROM(SELECT * FROM result ) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
where tmp.Cname = '數學' order by tmp.score desc;
SELECT tmp.S,tmp.Sname,tmp.Cname,tmp.score,@j :=@j + 1 AS j
FROM(SELECT * FROM result ) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
where tmp.Cname = '英語' order by tmp.score desc;
參考答案:
--19.1 sql 2000用子查詢完成
--Score重複時保留名次空缺
select t.* , px = (select count(1) from SC where C = t.C and score > t.score) + 1 from sc t order by t.C , px
--Score重複時合併名次
select t.* , px = (select count(distinct score) from SC where C = t.C and score >= t.score) from sc t order by t.C , px
--19.2 sql 2005用rank,DENSE_RANK完成
--Score重複時保留名次空缺(rank完成)
select t.* , px = rank() over(partition by C order by score desc) from sc t order by t.C , px
--Score重複時合併名次(DENSE_RANK完成)
select t.* , px = DENSE_RANK() over(partition by C order by score desc) from sc t order by t.C , px
20、查詢學生的總成績並進行排名
-- 建立視圖
CREATE view result2 as
SELECT stu.S,stu.Sname,SUM(sc.score) AS score FROM student stu,sc sc WHERE stu.S = sc.s GROUP BY sc.S ORDER BY SUM(sc.score) DESC;
SELECT tmp.S,tmp.Sname,tmp.score,@j :=@j + 1 AS '排名'
FROM(SELECT * FROM result2) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
2一、查詢不一樣老師所教不一樣課程平均分從高到低顯示
SELECT t.Tname,c.Cname,(SUM(sc.score) / (SELECT COUNT(*) FROM sc sc1 WHERE sc1.c = sc.c) ) AS avgerage FROM teacher t,course c,sc sc WHERE t.T = c.T AND sc.C = c.C GROUP BY sc.c ORDER BY avgerage DESC;
2二、查詢全部課程的成績第2名到第3名的學生信息及該課程成績
create view result3 as
SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s;
select * from result3 where Cname = '語文' order by score desc limit 1,2;
select * from result3 where Cname = '數學' order by score desc limit 1,2;
select * from result3 where Cname = '英語' order by score desc limit 1,2;
2三、統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所佔百分比
SELECT c.C AS '課程編號',c.Cname AS '課程名稱',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score >= 85) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[85-100](%)',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score < 85 and sc2.score >= 70) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[70-80](%)',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score <70 and sc2.score >= 60) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[60-70](%)',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score <60) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[0-60](%)'
FROM sc sc,course c WHERE sc.C = c.C GROUP BY sc.c;
2四、查詢學平生均成績及其名次
select b.*,@j:=@j+1 as '排名' from (SELECT stu.S,stu.Sname,avg(sc.score) AS score FROM student stu,sc sc WHERE stu.S = sc.s GROUP BY sc.S ORDER BY avg(sc.score) DESC) as b,(SELECT @j := 0 ,@pre_score := 0) sdcore;
2五、查詢各科成績前三名的記錄
create view result4 as
SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s;
select * from result4 where Cname = '語文' order by score desc limit 0,3;
select * from result4 where Cname = '數學' order by score desc limit 0,3;
select * from result4 where Cname = '英語' order by score desc limit 0,3;
2六、查詢每門課程被選修的學生數
select c.cname as '課程',count(sc.s) as '人數' from sc sc,course c where sc.c = c.c group by sc.c
2七、查詢出只有兩門課程的所有學生的學號和姓名
select s2.s,s2.sname from student s2,( select s.s,count(sc.c) as num from student s,sc sc where s.s=sc.s GROUP BY sc.s ) b where b.num =2 and b.s = s2.s;
2八、查詢男生、女生人數
select ssex as '性別',COUNT(1) as '人數' from student group by Ssex;
2九、查詢名字中含有」風」字的學生信息
select s.* from student s where s.sname like '%風%';
30、查詢同名同性學生名單,並統計同名人數
select sname,ssex,count(sname) from student group by sname,ssex having count(sname) >1
3一、查詢1990年出生的學生名單(注:Student表中Sage列的類型是datetime)
select * from student where year(sage) = 1990
3二、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號
select c,avg(score) from sc group by c order by avg(score) desc,c
3三、查詢平均成績大於等於85的全部學生的學號、姓名和平均成績
select b.* from (select s.s,s.sname,avg(sc.score) as avgScore from student s,sc sc where s.s = sc.s group by sc.s) b where b.avgScore >= 85
3四、查詢課程名稱爲」數學」,且分數低於60的學生姓名和分數
select s.sname,sc.score from student s,sc sc,course c where s.s=sc.s and sc.c = c.c and c.cname = '數學' and sc.score<60
3五、查詢全部學生的課程及分數狀況
select s2.s,s2.sname,b.cname,b.score from student s2 left join (select sc.s,s.sname,c.cname,sc.score from student s,sc sc,course c where s.s = sc.s and sc.c = c.c) b on s2.s = b.s
3六、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數
select s.sname,c.cname,sc.score from student s,sc sc,course c where s.s = sc.s and sc.c = c.c and sc.score >= 70
3七、查詢不及格的課程
select s.sname,c.cname,sc.score from student s,sc sc,course c where s.s = sc.s and sc.c = c.c and sc.score < 60
3八、查詢課程編號爲01且課程成績在80分以上的學生的學號和姓名
select s.s,s.sname from student s,sc sc where s.s = sc.s and sc.c='01' and sc.score >= 80
3九、求每門課程的學生人數
select c.cname,COUNT(1) from sc sc,course c where sc.c =c.c group by sc.c
40、查詢選修」張三」老師所授課程的學生中,成績最高的學生信息及其成績(不考慮最高分相同)
select s.*,sc.score from student s,sc sc,teacher t,course c where s.s = sc.s and c.t = t.t and sc.c = c.c and t.tname = '張三' order by sc.score desc limit 1
4一、查詢不一樣課程成績相同的學生的學生編號、課程編號、學生成績
select sc.* from sc sc,(select sc1.c, sc1.score from sc sc1 group by sc1.c,sc1.score having count(1) > 1) n
where sc.C= n.C and sc.score = n.score order by sc.C , sc.score , sc.S
4二、查詢每門課程成績最好的前兩名
select b.* from (SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s) b where b.Cname = '語文' order by b.score desc limit 2;
select b.* from (SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s) b where b.Cname = '數學' order by b.score desc limit 2;
select b.* from (SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s) b where b.Cname = '英語' order by b.score desc limit 2;
4三、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
select b.c,b.num from (select c,count(1) as num from sc group by c) b where b.num > 5 order by b.num desc,b.c asc
4四、檢索至少選修兩門課程的學生學號
select b.s from (select s,count(1) as num from sc group by s) b where b.num >= 2
4五、查詢選修了所有課程的學生信息
select s.* from student s,(select s,count(1) as num from sc group by s) b where s.s = b.s and b.num = (select count(1) as courseNum from course)
4六、查詢各學生的年齡
-- 只考慮年份
select s,sname,(select year(now()) - (select year(sage))) as age from student group by s
4七、查詢本週過生日的學生
select * from student where ((select WEEKOFYEAR(now()))- (select WEEKOFYEAR(sage))) =0
4八、查詢下週過生日的學生
select * from student where ((select WEEKOFYEAR(now()))- (select WEEKOFYEAR(sage))) = -1
4九、查詢本月過生日的學生
select * from student where ((select month(now()))- (select month(sage))) = 0
50、查詢下月過生日的學生
select * from student where ((select month(now()))- (select month(sage))) = -1