表(MYSQL)
Student(sid,Sname,Sage,Ssex) 学生表
CREATE TABLE student (
sid varchar(10) NOT NULL,
sName varchar(20) DEFAULT NULL,
sAge datetime DEFAULT ‘1980-10-12 23:12:36’,
sSex varchar(10) DEFAULT NULL,
PRIMARY KEY (sid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Course(cid,Cname,tid) 课程表
CREATE TABLE course (
cid varchar(10) NOT NULL,
cName varchar(10) DEFAULT NULL,
tid int(20) DEFAULT NULL,
PRIMARY KEY (cid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SC(sid,cid,score) 成绩表
CREATE TABLE sc (
sid varchar(10) DEFAULT NULL,
cid varchar(10) DEFAULT NULL,
score int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Teacher(tid,Tname) 教师表
CREATE TABLE taacher (
tid int(10) DEFAULT NULL,
tName varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
数据:(MySQL)
insert into teacher(tid,tName) values (1,‘李老师’),(2,‘何以琛’),(3,‘叶平’);
insert into student(sid,sName,sAge,sSex) values (‘1001’,‘张三丰’,‘1980-10-12 23:12:36’,‘男’),(‘1002’,‘张无极’,‘1995-10-12 23:12:36’,‘男’),(‘1003’,‘李奎’,‘1992-10-12 23:12:36’,‘女’),(‘1004’,‘李元宝’,‘1980-10-12 23:12:36’,‘女’),(‘1005’,‘李世明’,‘1981-10-12 23:12:36’,‘男’),(‘1006’,‘赵六’,‘1986-10-12 23:12:36’,‘男’),(‘1007’,‘田七’,‘1981-10-12 23:12:36’,‘女’);
insert into sc(sid,cid,score) values (‘1’,‘001’,80),(‘1’,‘002’,60),(‘1’,‘003’,75),(‘2’,‘001’,85),(‘2’,‘002’,70),(‘3’,‘004’,100),(‘3’,‘001’,90),(‘3’,‘002’,55),(‘4’,‘002’,65),(‘4’,‘003’,60);
insert into course(cid,cName,tid) values (‘001’,‘企业管理’,3),(‘002’,‘马克思’,3),(‘003’,‘UML’,2),(‘004’,‘数据库’,1),(‘005’,‘英语’,1);
ORACLE(表+数据)
CREATE TABLE student (
sid varchar2(10) NOT NULL,
sName varchar2(20) DEFAULT NULL,
sAge date ,
sSex varchar2(10) DEFAULT NULL,
PRIMARY KEY (sid)
)
CREATE TABLE course (
cid varchar2(10) NOT NULL,
cName varchar2(10) DEFAULT NULL,
tid number(20) DEFAULT NULL,
PRIMARY KEY (cid)
)
CREATE TABLE sc (
sid varchar2(10) DEFAULT NULL,
cid varchar2(10) DEFAULT NULL,
score number(10) DEFAULT NULL
)
CREATE TABLE teacher (
tid number(10) DEFAULT NULL,
tName varchar2(10) DEFAULT NULL
)
insert into course(cid,cName,tid) values (‘001’,‘企业管理’,3);
insert into course(cid,cName,tid) values (‘002’,‘马克思’,3);
insert into course(cid,cName,tid) values (‘004’,‘数据库’,1);
insert into course(cid,cName,tid) values (‘005’,‘英语’,1);
insert into sc(sid,cid,score) values (‘1001’,‘001’,80);
insert into sc(sid,cid,score) values (‘1001’,‘002’,60);
insert into sc(sid,cid,score) values (‘1001’,‘003’,70);
insert into sc(sid,cid,score) values (‘1002’,‘001’,85);
insert into sc(sid,cid,score) values (‘1002’,‘002’,70);
insert into sc(sid,cid,score) values (‘1003’,‘004’,90);
insert into sc(sid,cid,score) values (‘1003’,‘001’,90);
insert into sc(sid,cid,score) values (‘1003’,‘002’,99);
insert into sc(sid,cid,score) values (‘1004’,‘002’,65);
insert into sc(sid,cid,score) values (‘1004’,‘003’,50);
insert into sc(sid,cid,score) values (‘1005’,‘005’,80);
insert into sc(sid,cid,score) values (‘1005’,‘004’,70);
insert into sc(sid,cid,score) values (‘1003’,‘003’,10);
insert into sc(sid,cid,score) values (‘1003’,‘005’,10);
insert into student(sid,sName,sAge,sSex) values (‘1001’,‘张三丰’,to_date(‘1980-10-12 23:12:36’,‘YYYY-MM-DD HH24:MI:SS’),‘男’);
insert into student(sid,sName,sAge,sSex) values (‘1002’,‘张无极’,to_date(‘1995-10-12 23:12:36’,‘YYYY-MM-DD HH24:MI:SS’),‘男’);
insert into student(sid,sName,sAge,sSex) values (‘1003’,‘李奎’,to_date(‘1992-10-12 23:12:36’,‘YYYY-MM-DD HH24:MI:SS’),‘女’);
insert into student(sid,sName,sAge,sSex) values (‘1004’,‘李元宝’,to_date(‘1980-10-12 23:12:36’,‘YYYY-MM-DD HH24:MI:SS’),‘女’);
insert into student(sid,sName,sAge,sSex) values (‘1005’,‘李世明’,to_date(‘1981-10-12 23:12:36’,‘YYYY-MM-DD HH24:MI:SS’),‘男’);
insert into student(sid,sName,sAge,sSex) values (‘1006’,‘赵六’,to_date(‘1986-10-12 23:12:36’,‘YYYY-MM-DD HH24:MI:SS’),‘男’);
insert into student(sid,sName,sAge,sSex) values (‘1007’,‘田七’,to_date(‘1981-10-12 23:12:36’,‘YYYY-MM-DD HH24:MI:SS’),‘女’);
insert into teacher(tid,tName) values (1,‘李老师’);
insert into teacher(tid,tName) values (2,‘何以琛’);
insert into teacher(tid,tName) values (3,‘叶平’);
1.查询“001”课程比“002”课程成绩高的所有学生的学号;
SELECT a.sid"s_no",c.sName,a.score'001',b.score'002'FROM
(
SELECT sid,cid,score FROM sc WHERE cid='001'
) AS a
INNER JOIN
(
SELECT sid,cid,score FROM sc WHERE cid='002'
)AS b ON a.sid=b.sid
INNER JOIN student AS c on c.sid=a.sid
WHERE a.score>b.score
2、查询平均成绩大于60分的同学的学号和平均成绩;
SELECT sid,avg(score)
FROM sc
GROUP BY sid HAVING avg (score)>60
SELECT a.sid,a.sName,COUNT(b.cid),
sum(b.score)
FROM student AS a
LEFT JOIN sc AS b ON a.sid=b.sid
GROUP BY sid,a.sName
sum(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END)
SELECT
COUNT(DISTINCT tName)
FROM
teacher
WHERE
tName LIKE '李%'
5、查询没学过“叶平”老师课的同学的学号、姓名;
SELECT
sid,
Sname
FROM
Student
WHERE
sid NOT IN (
SELECT DISTINCT
(SC.sid)
FROM
SC,Course,Teacher
WHERE
SC.cid = Course.cid
AND Teacher.tid = Course.tid
AND Teacher.Tname = '叶平'
);
SELECT sid,sName FROM student
WHERE sid NOT in
(
SELECT sid FROM sc AS s
INNER JOIN course AS c ON s.cid=c.cid
INNER JOIN teacher AS t ON c.tid=t.tid
WHERE t.tname='叶平'
)
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
SELECT sid,sName FROM student
WHERE sid in
(SELECT a.sid FROM
(SELECT * FROM sc
WHERE cid='001') AS a
INNER JOIN
(SELECT * FROM sc
WHERE cid='002')as b
ON a.sid=b.sid
)
SELECT st.sid,st.sname,s.cid,c.cName,c.tid,t.tName
FROM student AS st
INNER JOIN sc AS s ON s.sid=st.sid
INNER JOIN course AS c ON s.cid=c.cid
INNER JOIN teacher AS t on t.tid=c.tid
WHERE t.tName='叶平'
ORDER BY st.sid
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
SELECT a.sid"s_no",c.sName,a.score'001',b.score'002' FROM
(
SELECT sid,cid,score FROM sc WHERE cid='001'
) AS a
INNER JOIN
(
SELECT sid,cid,score FROM sc WHERE cid='002'
)AS b ON a.sid=b.sid
INNER JOIN student AS c ON c.sid=a.sid
WHERE a.score>b.score
SELECT
sid,
Sname
FROM
Student
WHERE
sid NOT IN (
SELECT
Student.sid
FROM
Student,
SC
WHERE
student.sid = SC.sid
AND score > 60
);
SELECT
st.*, scc.*
FROM
student AS st
LEFT JOIN sc AS scc ON st.sid = scc.sid
GROUP BY
st.sid
HAVING
COUNT(DISTINCT scc.cid) < (
SELECT
COUNT(DISTINCT cid)
FROM
course
)
11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
SELECT sid,sName FROM student
WHERE sid in (
SELECT DISTINCT sid FROM sc
WHERE cid in (
SELECT cid FROM sc
WHERE sid='1001'
)and sid!='1001'
)
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
UPDATE sc,
(
SELECT
c.cid,AVG(score) avgs
FROM
sc,course c,teacher t
WHERE
sc.cid = c.cid
AND c.tid = t.tid
AND t.tName = '叶平'
GROUP BY
c.cid
) sc_2
SET sc.score = sc_2.avgs
WHERE
sc.cid = sc_2.cid
select * from sc where sc.cid in (001,002)(查询结果)
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT sid FROM sc
WHERE cid in(
SELECT cid FROM sc WHERE sid='1002'
)
GROUP BY sid HAVING COUNT(*)=(SELECT count(*)
FROM sc WHERE sid= '1002')
15、删除学习“叶平”老师课的SC表记录;
DELETE FROM sc
WHERE sc.cid in(
SELECT sc.cid FROM course c,teacher t
WHERE sc.cid=c.cid
and c.tid=t.tid
AND t.tName='叶平')
17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,数据库,企业管理,英语,有效课程数,有效平均分
两种方法
SELECT sid "学号"
,MAX(case when cid='004' THEN score ELSE NULL END) "数据库"
,MAX(case when cid='001' THEN score ELSE NULL END) "企业管理"
,MAX(case when cid='001' THEN score ELSE NULL END) "英语"
,COUNT(*) AS 有效课程数
,AVG(score) "平均成绩"
FROM sc
GROUP BY sid
ORDER BY AVG(score) DESC
SELECT
sid AS 学生ID,
(
SELECT
score
FROM
SC
WHERE
SC.sid = t.sid
AND cid = '004'
) AS 数据库,
(
SELECT
score
FROM
SC
WHERE
SC.sid = t.sid
AND cid = '001'
) AS 企业管理,
(
SELECT
score
FROM
SC
WHERE
SC.sid = t.sid
AND cid = '005'
) AS 英语,
COUNT(*) AS 有效课程数,
AVG(t.score) AS 平均成绩
FROM
SC AS t
GROUP BY
sid
ORDER BY
avg(t.score)
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT cid"课程",MAX(s.score),MIN(s.score)
FROM sc as s
GROUP BY cid
SELECT t.cid AS 课程号,MAX(course.Cname)AS 课程名,IFNULL(AVG(score),0) AS 平均成绩
,100 * SUM(CASE WHEN IFNULL(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数
FROM SC T,Course
WHERE t.cid=course.cid
GROUP BY t.cid
ORDER BY 100 * SUM(CASE WHEN IFNULL(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC
20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)
SELECT
SUM(CASE WHEN cid ='001' THEN score ELSE 0 END )/SUM(CASE cid WHEN '001' THEN 1 ELSE 0 END) AS 企业管理平均分
,100*SUM(CASE WHEN cid= '001' AND score>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '001' THEN 1 ELSE 0 END) as 企业管理百分数
,SUM(CASE WHEN cid ='002' THEN score ELSE 0 END )/SUM(CASE cid WHEN '002' THEN 1 ELSE 0 END) AS 马克思平均分
,100*SUM(CASE WHEN cid= '002' AND score>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '002' THEN 1 ELSE 0 END) as 马克思百分数
,SUM(CASE WHEN cid ='003' THEN score ELSE 0 END )/SUM(CASE cid WHEN '003' THEN 1 ELSE 0 END) AS OOUML平均分
,100*SUM(CASE WHEN cid= '003' AND score>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '003' THEN 1 ELSE 0 END) as OOUML百分数
,SUM(CASE WHEN cid ='004' THEN score ELSE 0 END )/SUM(CASE cid WHEN '004' THEN 1 ELSE 0 END) AS 数据库平均分
,100*SUM(CASE WHEN cid= '004' AND score>=60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '004' THEN 1 ELSE 0 END) as 数据库百分数
FROM sc
21、查询不同老师所教不同课程平均分从高到低显示 要求显示:教师ID,教师姓名,课程ID,课程名称,平均成绩
SELECT MAX(t.tid) "教师ID",MAX(t.tName) "教师姓名",c.cid "课程ID", MAX(c.cName) "课程名称" ,AVG(sc.score) "平均成绩"
FROM sc,course c,teacher t WHERE sc.cid = c.cid AND c.tid = t.tid GROUP BY c.tid,c.cid
ORDER BY AVG(sc.score) DESC
23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
SELECT c.cid,c.cName
,sum(CASE when sc.score<=100 AND sc.score>85 THEN 1 ELSE 0 END) "[100,85)"
,sum(CASE when sc.score<=85 AND sc.score>70 THEN 1 ELSE 0 END) "[85,70)"
,sum(CASE when sc.score<=70 AND sc.score>60 THEN 1 ELSE 0 END) "[70,60)"
,sum(CASE when sc.score<60 THEN 1 ELSE 0 END) "(<60)"
FROM sc,course c WHERE sc.cid=c.cid
GROUP BY c.cid,c.cName
26、查询每门课程被选修的学生数
SELECT c.cid,c.cName,COUNT(sid)
FROM sc,course c WHERE sc.cid=c.cid
GROUP BY c.cid;
SELECT sid,sname
FROM student
WHERE sid in(
SELECT sid
FROM sc
GROUP BY sid HAVING COUNT(DISTINCT cid)=1
)
第一种
SELECT sSex,count(sid) FROM student
GROUP BY sSex
第二种
SELECT sSex,count(sSex) FROM student
GROUP BY sSex
第三种
SELECT
SUM(CASE WHEN sSex='男' THEN 1 ELSE 0 END)"男"
,SUM(CASE WHEN sSex='女' THEN 1 ELSE 0 END)"女"
FROM student
也可以用sum也可以用count的时候else后是null
第四种
Select count(Ssex) as 男生人数 from Student group by Ssex having Ssex='男';
Select count(Ssex) as 女生人数 from Student group by Ssex having Ssex='女';
SELECT * FROM student WHERE sName LIKE '%张%'-- 含有张
SELECT * FROM student WHERE sName LIKE '%张'-- 以张开头
SELECT * FROM student WHERE sName LIKE '张%'-- 以张结尾
SELECT sName,sSex ,COUNT(*) FROM student GROUP BY sName,sSex HAVING COUNT(*) > 1
31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
SELECT * FROM student
WHERE YEAR (sAge)=1981
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
SELECT sc.cid,course.cName,AVG(score) AS avg_score FROM sc,course
WHERE sc.cid=course.cid
GROUP BY sc.cid
ORDER BY avg_score ASC,sc.cid DESC
SELECT sname,sc.sid,AVG(score) FROM sc,student
WHERE student.sid=sc.sid
GROUP BY sc.sid ,sname HAVING avg(score)>65;
85没有结果所以改了一下数字
– 34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
SELECT * FROM course c,sc,student t
WHERE sc.sid=t.sid AND sc.cid=c.cid AND c.cname='数据库'AND score<60;
数据库是没有低于60的所以又改了一下测试
35、查询所有学生的选课情况;
SELECT sc.sid,sc.cid,sname,cname FROM student t,course c,sc
WHERE sc.sid=t.sid AND sc.cid=c.cid
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT sname,cname,score FROM sc,course c,student t
WHERE sc.sid= t.sid AND sc.cid=c.cid AND sc.score>70
SELECT * FROM sc,course,student
WHERE score<60 AND sc.cid=course.cid AND student.sid=sc.sid
ORDER BY sc.cid DESC
– 38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
SELECT student.sname,sc.sid FROM sc,student
WHERE cid= '003'AND score>80 AND student.sid=sc.sid
SELECT count(*) FROM sc
40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
```sql
Sname,score
select Sname,score
from Student s,SC,Course C,Teacher t
where S.sid=SC.sid and SC.cid=C.cid and C.tid=T.tid and T.Tname='叶平'
and SC.score=(select max(score)from SC where cid=C.cid );
SELECT cname,COUNT(*) FROM sc,course c
WHERE c.cid=sc.cid
GROUP BY sc.cid
select distinct a.cid,A.sid,B.score from SC A ,SC B where A.Score=B.Score and A.cid <>B.cid
ORDER BY score
SELECT * FROM
sc s1
WHERE (
SELECT count(*)
FROM sc s2
WHERE s1.cid=s2.cid
AND s2.score>=s1.score
)<=2 ORDER BY s1.cid
44、统计每门课程的学生选修人数(超过10人的课程才统计)。
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT cid "课程号",COUNT(*) "选修人数"
FROM sc GROUP BY cid HAVING COUNT(*) >10
ORDER BY COUNT(*) DESC,cid
SELECT sid,count(DISTINCT cid) FROM sc
GROUP BY sid HAVING count(DISTINCT cid)>=2
SELECT s.sName,c.cName, COUNT(*) FROM student s,course c, sc
WHERE s.sid = sc.sid AND sc.cid = c.cid
GROUP BY sc.cid HAVING COUNT(*) = (SELECT COUNT(*) FROM student)
SELECT DISTINCT Sname FROM Student WHERE sid NOT IN (SELECT sid FROM Course,Teacher,SC WHERE Course.tid=Teacher.tid AND SC.cid=course.cid AND Tname='叶平');
select sid,avg(ifnull(score,0))
from SC where sid in (select sid from SC
where score <60
group by sid having count(*)>2)group by sid;
49、检索“004”课程分数小于60,按分数降序排列的同学学号
select sid from SC where cid='004'and score <60
order by score desc;
delete from Sc where sid='1002' and cid='001';