高级查询习题1-46

创建数据库

create table student(
  sno varchar(10) primary key,
  sname varchar(20),
  sage numeric(2),
  ssex varchar(5)
);

DROP table student;
DROP TABLE teacher;
create table teacher(
 tno varchar(10) primary key,
 tname varchar(20)
);


create table course(
  cno varchar(10),
  cname varchar(20),
  tno varchar(20),
  constraint pk_course primary key (cno,tno)
);


create table sc(
   sno varchar(10),
   cno varchar(10),
   score  numeric(4,2),
   constraint pk_sc primary key (sno,cno)
);
DROP TABLE sc;



/*******初始化学生表的数据******/
insert into student values ('s001','张三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','吴鹏',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王丽',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','刘玉',21,'男');
insert into student values ('s008','萧蓉',21,'女');
insert into student values ('s009','陈萧晓',23,'女');
insert into student values ('s010','陈美',22,'女');



/******************初始化教师表***********************/
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '谌燕');
insert into teacher values ('t003', '胡明星');


/***************初始化课程表****************************/
insert into course values ('c001','J2SE','t002');
insert into course values ('c002','Java Web','t002');
insert into course values ('c003','SSH','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t002');
insert into course values ('c008','DIV+CSS','t001');
insert into course values ('c009','PHP','t003');
insert into course values ('c010','EJB3.0','t002');

/***************初始化成绩表***********************/
insert into sc values ('s001','c001',78.9);
insert into sc values ('s002','c001',80.9);
insert into sc values ('s003','c001',81.9);
insert into sc values ('s004','c007',60.9);
insert into sc values ('s001','c002',82.9);
insert into sc values ('s002','c002',72.9);
insert into sc values ('s003','c002',81.9);
insert into sc values ('s001','c003','59');
insert into sc values ('s001','c007','67');
insert into sc values ('s001','c010','98');


– 1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;

-- 方法一:
        SELECT *
                        FROM 
        (SELECT * FROM sc WHERE cno = 'c001') as a,
        (SELECT * FROM sc WHERE cno = 'c002') as b
        WHERE a.sno = b.sno
            AND a.score>b.score
-- 方法二


        SELECT *
                FROM student a,sc b,sc c
                WHERE a.sno = b.sno
                AND b.cno = 'c001'
                AND a.sno = c.sno
                AND c.cno='c002'
                AND b.score>c.score

– 2、查询平均成绩大于60 分的同学的学号和平均成绩;

SELECT sno,GROUP_CONCAT(cno),avg(score)
        FROM sc
        GROUP BY sno;

– 3、查询所有同学的学号、姓名、选课数、总成绩;

-- 方法一:
    SELECT student.sno,sname,sage,ssex,GROUP_CONCAT(cno),sum(score),COUNT(cno) as '选课数'
            FROM sc,student
            WHERE student.sno = sc.sno
            GROUP BY sno;

– 方法二:

    SELECT student.sno,sname,sage,ssex,GROUP_CONCAT(cno),sum(score),COUNT(cno) as '选课数'
            FROM sc INNER JOIN student
            ON student.sno = sc.sno
            GROUP BY sno;

– 4、查询姓“刘”的老师的个数;

SELECT * ,COUNT(tname)
    FROM teacher
    WHERE `tname` LIKE '刘%'

– 5、查询没学过“谌燕”老师课的同学的学号、姓名;

    SELECT sno,sname
    FROM student 
    WHERE  sno  NOT in 
            (SELECT sno FROM sc WHERE cno  in
                    (SELECT cno FROM course WHERE tno=
                        (SELECT tno FROM teacher WHERE tname ='谌燕')
                    )
            )

– 6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名

方法一:
SELECT sno,sname 
FROM student
WHERE  sno= ANY
(SELECT  sno FROM sc WHERE sno in
(SELECT sno FROM sc WHERE cno = 'c001') AND cno = 'c002');:
方法二:
SELECT a.sno,a.sname 
FROM student a,sc b,sc c
WHERE a.sno = b.sno
AND b.cno = 'c001'
AND a.sno = c.sno
AND c.cno='c002'
方法三:
SELECT *
FROM 
(SELECT * FROM sc WHERE cno = 'c001') as a,
(SELECT * FROM sc WHERE cno = 'c002') as b
WHERE a.sno = b.sno 

7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;

SELECT stu.sno,stu.sname from student stu where stu.sno in(
SELECT s.sno from sc s where s.cno in(
SELECT cno FROM course where tno in(
SELECT tno FROM teacher where tname='谌燕'
)))

8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;

SELECT *
                FROM student a,sc b,sc c
                WHERE a.sno = b.sno
                AND b.cno = 'c001'
                AND a.sno = c.sno
                AND c.cno='c002'
                AND b.score


– 9、查询所有课程成绩小于60 分的同学的学号、姓名;

    SELECT * FROM student WHERE SNO IN(
                    SELECT sno
                            FROM sc
                                    WHERE SCORE<60)

– 10、查询没有学全所有课的同学的学号、姓名;

SELECT student.sno,sname,GROUP_CONCAT(sc.cno),COUNT(DISTINCT sc.cno)
        FROM student left JOIN sc
        ON student.sno=sc.sno 
            GROUP BY student.sno
            HAVING COUNT(DISTINCT sc.cno)< any(sELECT COUNT(cno) FROM course);

– 11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;

SELECT DISTINCT student.*
FROM student
WHERE sno IN (
        SELECT sno
        FROM sc
        WHERE cno IN (
        SELECT cno
        FROM sc WHERE sno = 's001'
            )
    )  and student.sno!='s001'

– 12、查询至少学过学号为“s004”同学所学一门课的其他同学学号和姓名;s.sno!=’s004’

                SELECT DISTINCT
    student.*
FROM
            student
WHERE
    sno IN (
        SELECT
            sno
        FROM
            sc
        WHERE
            cno IN (
                SELECT
                    cno
                FROM
                    sc
                WHERE
                    sno = 's004'
            )
    )  and student.sno!='s004'

13、把“sc”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
参考答案:

update sc as s set s.score=
(select avg(score) from (select * from sc) as k
    where s.cno=k.cno group by k.cno)
    where cno in (select a.cno from (select * from sc)a,course b,teacher c
        where a.cno=b.cno and b.tno=c.tno and c.tname='谌燕');


– 14、查询和“s002”号的同学学习的课程’完全相同’的其他同学学号和姓名;

SELECT * FROM student where sno in(
                SELECT b.sno FROM
(SELECT sno,GROUP_CONCAT(cno) as gr,COUNT(cno)  FROM sc WHERE sno='s002' GROUP BY sno ) as a,

(SELECT sno,GROUP_CONCAT(cno) as grc,COUNT(cno) FROM sc WHERE sno!='s002' GROUP BY sno) as b
    WHERE a.gr= b.grc
)

– 15、删除学习“谌燕”老师课的sc 表记录;


                DELETE FROM sc   WHERE cno in
                                    (
                SELECT cno FROM course WHERE tno=
                                        (   
                SELECT tno FROM teacher WHERE tname ='谌燕')

                                    )  

16、向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
此题 有很大差异 与题目



            create table sc1(
                 sno varchar(10),
                 cno varchar(10),
                 score  numeric(4,2),
                 constraint pk_sc primary key (sno,cno)
            );
insert into sc1 values ('s001','c001',78.9);
insert into sc1 values ('s002','c001',80.9);
insert into sc1 values ('s003','c001',81.9);
insert into sc1 values ('s004','c007',60.9);
insert into sc1 values ('s001','c002',82.9);
insert into sc1 values ('s002','c002',72.9);
insert into sc1 values ('s003','c002',81.9);
insert into sc1 values ('s001','c003','59');
insert into sc1 values ('s001','c007','67');
insert into sc1 values ('s001','c010','98');

                DELETE FROM sc1  WHERE cno='c002'
                (SELECT avg(sc.score) FROM sc WHERE sc.cno='c002') AS a
                UPDATE sc SET sc1.score = a;

参考答案

insert into sc (sno,cno,score) select sno,'c002',(select avg(score) from sc where cno='c002')
from sc where sno not in(select sno from sc where cno='c002');

– 17 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

SELECT cname as '课程ID',s  as '最高分',s3 as '最低分' FROM
    (SELECT * from course) as b,
    (SELECT sno,cno,GROUP_CONCAT(score) as a01,MAX(score) as s,MIN(score) as s3  FROM  sc GROUP BY sc.cno ) as a
                where  a.cno=b.cno

18按各科平均成绩从低到高和及格率的百分数从高到低顺序(做不出来)

参考答案

select cno,AVG(score),(
                        SELECT COUNT(*) FROM sc b where b.cno=sc.cno and score>60)*100/
(SELECT COUNT(*) FROM sc b where b.cno=sc.cno)"及格率" 
FROM sc GROUP BY cno ORDER BY 
AVG(score) asc,"及格率"desc;

19、查询不同老师所教不同课程平均分从高到低显示

select max(t.tno),max(t.tname),max(c.cno),max(c.cname),c.cno,avg(score) from sc , course c,teacher t
where sc.cno=c.cno and c.tno=t.tno group by c.cno order by avg(score) desc;

21、查询各科成绩前三名的记录:(不考虑成绩并列情况)

SELECT sno,cname,score
                FROM
                course as c,sc as s
        GROUP BY cname

                ORDER BY cname,score DESC LIMIT 0,3


                WHERE c.cno= s.cno
                and c.cname (SELECT cname FROM course WHERE cno in(
SELECT  DISTINCT cno FROM sc
))
                ORDER BY score DESC LIMIT 0,2

– 22、查询每门课程被选修的学生数


                SELECT cname,COUNT(sc.score) as "选修人数"
                                FROM
                                course ,sc 
                WHERE course.cno= sc.cno
                                GROUP BY cname

– 23、查询出只选修了一门课程的全部学生的学号和姓名

SELECT *  FROM student 
        WHERE   sno in(
                SELECT *
                                FROM
                                course ,sc 
                WHERE course.cno= sc.cno
                                GROUP BY sno
                                HAVING(COUNT(cname)=1))

– 24、查询男生、女生人数

        SELECT  ssex,COUNT(ssex)

            FROM student
                WHERE ssex= "男" 
UNION
                    SELECT  ssex,COUNT(ssex)

            FROM student
                WHERE ssex= "女" 

– 25、查询姓“张”的学生名单

SELECT *

            FROM student
                WHERE `sname` LIKE '张%'


– 26、查询同名同姓学生名单,并统计同名人数

select sname,count(*)from student group by sname having count(*)>1;

– 27、1996 年出生的学生名单(注:Student 表中Sage 列的类型是number)

select * FROM student WHERE  sage = 21

– 28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

SELECT course.cno,cname,AVG( score) 
                FROM course ,sc 
                WHERE course.cno= sc.cno
                GROUP BY cno
                ORDER BY AVG(score)  asc,cno DESC;

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

    SELECT student.sno,sname,avg(score)
                FROM student,sc 
                WHERE student.sno= sc.sno
                GROUP BY student.sno
                HAVING (avg(score)>85)

– 30、查询课程名称为“数据库”,且分数低于80 的学生姓名和分数

    SELECT * FROM student WHERE student.sno in (
                        SELECT sno
                        FROM course,sc 
                        WHERE course.cno= 'c002'
                        AND sc.score <80
                        GROUP BY sc.sno

        )

– 31、查询所有学生的选课情况;

select sname,GROUP_CONCAT(cname)
            FROM student , sc,course
            WHERE student.sno=sc.sno 
            AND     course.cno=sc.cno
            GROUP BY student.sno

– 32、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;

            SELECT sname,GROUP_CONCAT(cname),GROUP_CONCAT(score)
                FROM student , sc,course
                        WHERE student.sno=sc.sno 
                        AND     course.cno=sc.cno
                        and score>70
            GROUP BY student.sname 

– 33、查询不及格的课程,并按课程号从大到小排列

        SELECT cname,tno
                FROM student , sc,course
                        WHERE student.sno=sc.sno 
                        AND     course.cno=sc.cno
                            and score<70
                        ORDER BY tno DESC

– 34、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;

    SELECT *
                FROM student , sc,course
                        WHERE student.sno=sc.sno 
                        AND     course.cno=sc.cno
                            and score>80
                            AND sc.CNO = 'C001'

– 35、求选了课程的学生人数

    select sname
                            FROM student , sc,course
                            WHERE student.sno=sc.sno 
                            AND     course.cno=sc.cno
                            GROUP BY student.sname

– 36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
我的答案:

SELECT sname,score
                            FROM sc,student
                            WHERE  
                            score = (select max(score)from sc,course where sc.cno = course.cno)
                            AND cno in(SELECT cno FROM course WHERE tno in
                            (SELECT tno FROM teacher WHERE tname ='谌燕'))
                            AND sc.sno=student.sno


参考答案:
                select st.sname,score from student st,sc ,course c,teacher t
                            where st.sno=sc.sno 
                            and sc.cno=c.cno 
                            and c.tno=t.tno
                            and t.tname='谌燕' 
                            and sc.score=(select max(score)from sc where sc.cno=c.cno);

– 37、查询各个课程及相应的选修人数

    select cname, COUNT(sname) as '选修人数'
            from student st,sc,course c
            where st.sno=sc.sno 
            and sc.cno=c.cno 
            GROUP BY cname

– 38、查询不同课程成绩相同的学生的学号、课程号、学生成绩

    SELECT  a.sno,sname,b.cno,b.score
                FROM student a,sc b,sc c
                WHERE a.sno = b.sno
                AND a.sno = c.sno
                AND b.score=c.score
                AND c.cno   != b.cno
                GROUP BY sno

39、查询每门功课成绩最好的前两名

– 40、统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
– 若人数相同,按课程号升序排列

SELECT cname,count(score)   as '选修人数'
                FROM course ,sc 
                WHERE course.cno= sc.cno
                GROUP BY cname
                ORDER BY count(score) desc,course.cno aSC;

– 41、检索至少选修两门课程的学生学号

SELECT s.sno,GROUP_CONCAT(cname),COUNT(cname)
                FROM course as c,sc,student as s
                WHERE c.cno= sc.cno
                AND s.sno= sc.sno
                GROUP BY  sc.sno
                HAVING (COUNT(cname)>1)

– 42、查询全部学生都选修的课程的课程号和课程名

SELECT *,cname,count(score)
                FROM course as a,sc 
                WHERE a.cno= sc.cno 
                GROUP BY cname
                HAVING COUNT(score)= any(sELECT COUNT(cno) FROM course)

– 43、查询没学过“谌燕”老师讲授的任一门课程的学生姓名

    SELECT student.sname 
                    FROM student 
                    WHERE student.sno   not in
                    (SELECT cno FROM course WHERE tno in
                    (SELECT tno FROM teacher WHERE tname ='谌燕'))


– 44、查询两门以上不及格课程的同学的学号及其平均成绩

SELECT *,COUNT(score),GROUP_CONCAT(score)
                FROM course as a,sc 
                where score <70
                and a.cno = sc.cno
                HAVING COUNT(score)>2

– 45、检索“c004”课程分数小于60,按分数降序排列的同学学号

SELECT *
                fROM course as a,sc 
                where sc.cno = 'c004'
                and score <90
                and a.cno = sc.cno
                ORDER BY score DESC

– 46、删除“s002”同学的“c001”课程的成绩

delete from sc where sno='s002' and cno='c001';

你可能感兴趣的:(sql数据库)