常用的50条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);

题目

1、查询”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

2、查询”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

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

– 注: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) >= 60

4、查询平均成绩小于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

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

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

6、查询”李”姓老师的数量

SELECT
    COUNT(*)
FROM
    teacher t
WHERE
    t.tname LIKE CONCAT('李', '%');

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

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 = '张三'
);

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

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 = '张三'
        )
    );

9、查询学过编号为”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';

10、查询学过编号为”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'
);

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

SELECT
    stu.*
FROM
    student stu,
    sc sc
WHERE
    sc.s = stu.s
GROUP BY
    sc.s
HAVING
    COUNT(sc.c) < (SELECT COUNT(*) FROM course);

12、查询至少有一门课与学号为”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';

13、查询和”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;

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

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 = '张三'
    );

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

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;

16、检索”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;

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

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;

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程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;

19、按各科成绩进行排序,并显示排名

# 实现的过程有点问题
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

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

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;

22、查询所有课程的成绩第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;

23、统计各科成绩各分数段人数:课程编号,课程名称,[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;

24、查询学生平均成绩及其名次

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;

25、查询各科成绩前三名的记录

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;

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

select c.cname as '课程',count(sc.s) as '人数' from sc sc,course c where sc.c = c.c group by sc.c

27、查询出只有两门课程的全部学生的学号和姓名

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;

28、查询男生、女生人数

select ssex as '性别',COUNT(1) as '人数' from student group by Ssex;

29、查询名字中含有”风”字的学生信息

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

31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)

select * from student where year(sage) = 1990

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

select c,avg(score) from sc group by c order by avg(score) desc,c

33、查询平均成绩大于等于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

34、查询课程名称为”数学”,且分数低于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

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

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

36、查询任何一门课程成绩在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

37、查询不及格的课程

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

38、查询课程编号为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 

39、求每门课程的学生人数

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

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

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

42、查询每门课程成绩最好的前两名

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;

43、统计每门课程的学生选修人数(超过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

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

select b.s from (select s,count(1) as num from sc group by s) b where b.num >= 2

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

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)

46、查询各学生的年龄

-- 只考虑年份
  select s,sname,(select year(now()) - (select year(sage))) as age from student group by s

47、查询本周过生日的学生

select * from student where ((select WEEKOFYEAR(now()))- (select WEEKOFYEAR(sage))) =0  

48、查询下周过生日的学生

select * from student where ((select WEEKOFYEAR(now()))- (select WEEKOFYEAR(sage))) = -1

49、查询本月过生日的学生

select * from student where ((select month(now()))- (select month(sage))) = 0

50、查询下月过生日的学生

select * from student where ((select month(now()))- (select month(sage))) = -1

你可能感兴趣的:(数据库,练习)