MySQL查询语句练习

  • Post author:
  • Post category:mysql


出道半年了,总发现自己的SQL查询语句非常的弱,所以趁还没开始做项目的时候有时间练习,今天也是主要从网上摘来的练习,从简单到复杂。

数据库表以及数据

创建表:

--学生表
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`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('01', '赵雷', '1990-01-01', '男');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('02', '钱电', '1994-12-15', '男');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('03', '孙风', '1990-05-20', '男');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('04', '李云', '1990-08-06', '男');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('05', '周梅', '1991-12-01', '女');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('06', '吴兰', '1992-03-01', '女');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('07', '郑竹', '1989-07-01', '女');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('08', '王菊', '1990-01-20', '女');
INSERT INTO `Student`(`s_id`, `s_name`, `s_birth`, `s_sex`) VALUES ('09', '赵雷', '1999-12-15', '男');

成绩表:

INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('01', '01', 80);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('01', '02', 90);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('01', '03', 99);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('02', '01', 70);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('02', '02', 60);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('02', '03', 80);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('03', '01', 80);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('03', '02', 80);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('03', '03', 80);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('04', '01', 50);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('04', '02', 30);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('04', '03', 20);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('05', '01', 76);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('05', '02', 87);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('06', '01', 31);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('06', '03', 34);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('07', '02', 89);
INSERT INTO `Score`(`s_id`, `c_id`, `s_score`) VALUES ('07', '03', 98);

教师表:

INSERT INTO `Teacher`(`t_id`, `t_name`) VALUES ('01', '张三');
INSERT INTO `Teacher`(`t_id`, `t_name`) VALUES ('02', '李四');
INSERT INTO `Teacher`(`t_id`, `t_name`) VALUES ('03', '王五');
INSERT INTO `Teacher`(`t_id`, `t_name`) VALUES ('04', '李梅');

课程表:

INSERT INTO `Course`(`c_id`, `c_name`, `t_id`) VALUES ('01', '语文', '02');
INSERT INTO `Course`(`c_id`, `c_name`, `t_id`) VALUES ('02', '数学', '01');
INSERT INTO `Course`(`c_id`, `c_name`, `t_id`) VALUES ('03', '英语', '03');

查询语句:

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数  
SELECT
	a.*,b.s_score
FROM
	Student	a
	JOIN Score b ON a.s_id = b.s_id 
	AND b.c_id = '01'
	LEFT JOIN Score c ON a.s_id=c.s_id AND c.c_id='02' OR c.c_id=NULL WHERE b.s_score>c.s_score;
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT
	a.s_id,a.s_name,ROUND(AVG( b.s_score ),2) AS avg_score 
FROM
	Student a
	JOIN Score b ON b.s_id = a.s_id GROUP BY a.s_id HAVING AVG( b.s_score ) >= 60;
	
	-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
        -- (包括有成绩的和无成绩的)

SELECT
	a.s_id,a.s_name,ROUND( AVG( b.s_score ), 2 ) AS avg_score 
FROM Student a
JOIN Score b ON b.s_id = a.s_id 
GROUP BY a.s_id HAVING AVG( b.s_score ) < 60 
UNION
SELECT
	a.s_id,a.s_name,0 AS avg_score FROM Student a WHERE a.s_id NOT IN ( SELECT DISTINCT s_id FROM Score )
	
	-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
	
SELECT
	a.s_id,a.s_name,COUNT( b.c_id ),ROUND(AVG( b.s_score ),2 )
FROM
	Student a
	left JOIN Score b ON a.s_id = b.s_id 
GROUP BY
	a.s_id,a.s_name
	
-- 6、查询"李"姓老师的数量 
SELECT COUNT(t.t_name) FROM Teacher t WHERE t_name LIKE '李%'
-- 7、查询学过"张三"老师授课的同学的信息
SELECT a.* FROM Student a 
JOIN Score b ON b.s_id=a.s_id 
JOIN Course c ON b.c_id=c.c_id 
JOIN Teacher d ON d.t_id=c.t_id
AND d.t_name='张三';

SELECT a.* FROM Student a JOIN Score b ON b.s_id=a.s_id WHERE b.c_id IN 
(
	SELECT c_id FROM Course WHERE t_id IN 
	(
		SELECT t_id FROM Teacher WHERE t_name='张三'
	)
);

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

SELECT e.* FROM Student e WHERE e.s_id NOT IN (
	SELECT a.s_id FROM Student a 
	JOIN Score b ON b.s_id=a.s_id 
	JOIN Course c ON b.c_id=c.c_id 
	JOIN Teacher d ON d.t_id=c.t_id
	AND d.t_name='张三'
);

SELECT a.* FROM Student a WHERE a.s_id NOT IN 
(
	SELECT b.s_id FROM Student b JOIN Score c ON b.s_id=c.s_id WHERE c.c_id IN 
	(
		SELECT c_id FROM Course WHERE t_id IN 
		(
			SELECT t_id FROM Teacher WHERE t_name='张三'
		)
	)
);
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT a.* 
FROM Student a,Score b,Score c 
WHERE a.s_id = b.s_id AND a.s_id = c.s_id AND b.c_id = '01' AND c.c_id = '02'

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

SELECT a.* FROM Student a WHERE a.s_id IN 
(
	SELECT s_id FROM Score  WHERE c_id='01'
) AND a.s_id NOT IN (SELECT s_id FROM Score WHERE c_id='02')

-- 11、查询没有学全所有课程的同学的信息 
SELECT  DISTINCT a.* FROM Student a JOIN Score b ON a.s_id=b.s_id AND  a.s_id NOT IN 
(
	SELECT s_id FROM Score   GROUP BY s_id HAVING COUNT(s_id)=(SELECT COUNT(*) FROM Course)
);

select s.* from 
    Student s where s.s_id in(
        select s_id from Score where s_id not in(
            select a.s_id from Score a 
                join Score b on a.s_id = b.s_id and b.c_id='02'
                join Score c on a.s_id = c.s_id and c.c_id='03'
            where a.c_id='01'));

-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT a.* FROM Student a WHERE a.s_id IN 
(
	SELECT DISTINCT(s_id) FROM Score b WHERE b.c_id IN 
	(
		SELECT c.c_id FROM Score c WHERE c.s_id='01'
	)
)
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 
SELECT a.* FROM Student a WHERE a.s_id IN 
(
	SELECT b.s_id FROM Score b WHERE b.s_id!='01' AND b.c_id IN 
	(
		SELECT c_id FROM Score WHERE s_id='01'
	)
	GROUP BY b.s_id
	HAVING COUNT(*)=(SELECT COUNT(*) FROM Score WHERE s_id='01')
)
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 

SELECT a.s_name FROM Student a WHERE a.s_id NOT IN
(
	SELECT b.s_id FROM Score b WHERE b.c_id IN 
	(
		SELECT c.c_id FROM Course c JOIN Teacher t ON c.t_id =t.t_id  AND t.t_name='张三'	
	)
);
select a.s_name from Student a where a.s_id not in (
    select s_id from Score where c_id = 
                (select c_id from Course where t_id =(
                    select t_id from Teacher where t_name = '张三')) 
                group by s_id);
								
		
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 
SELECT a.s_id,a.s_name,ROUND(AVG(b.s_score),2) FROM Student a JOIN Score b ON a.s_id=b.s_id
WHERE a.s_id IN 
(
	SELECT s_id FROM Score WHERE s_score<60 GROUP BY s_id HAVING COUNT(1)>2
)
GROUP BY a.s_id,a.s_name
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT a.*,b.s_score FROM Student a JOIN Score b ON a.s_id=b.s_id AND  a.s_id IN 
(
	SELECT s_id FROM Score WHERE c_id='01' AND s_score<60
)
ORDER BY b.s_score DESC

-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT 
	 ROUND(AVG(b.s_score),2) as 平均成绩,
	 (select s_score from Score where s_id=b.s_id and c_id='01') as 语文 ,
   (select s_score from Score where s_id=b.s_id and c_id='02') as 数学,
   (select s_score from Score where s_id=b.s_id and c_id='03') as 英语
FROM Score b 
GROUP BY b.s_id 
ORDER BY b.s_score DESC

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

SELECT a.c_id,b.c_name,MAX(a.s_score) AS 最高成绩,MIN(a.s_score) AS 最低成绩,ROUND(AVG(a.s_score),2)AS 平均成绩 ,
ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率,
ROUND(100*(SUM(CASE WHEN a.s_score>=70  AND a.s_score<80 THEN 1 ELSE 0 END)/SUM(CASE WHEN a.s_score THEN 1 ELSE 0 END)),2) AS 中等,
ROUND(100*(SUM(CASE WHEN a.s_score>=80  AND a.s_score<90 THEN 1 ELSE 0 END)/SUM(CASE WHEN a.s_score THEN 1 ELSE 0 END)),2) AS 优良,
ROUND(100*(SUM(CASE WHEN a.s_score>=90  THEN 1 ELSE 0 END)/SUM(CASE WHEN a.s_score THEN 1 ELSE 0 END)),2) AS 优秀
FROM Score a 
JOIN Course b ON a.c_id=b.c_id  
GROUP BY a.c_id,b.c_name

-- 19、按各科成绩进行排序,并显示排名(实现不完全)
SELECT a.s_id,a.c_id,
@i:=@i+1 as i,
@k:=(CASE WHEN @score=a.s_score THEN @k ELSE @i END) as rank,
@score:=a.s_score as score
    from (
        select s_id,c_id,s_score from Score WHERE c_id='01' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from Score WHERE c_id='02' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
    union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from Score WHERE c_id='03' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s

-- 20、查询学生的总成绩并进行排名
	
		SELECT a.s_id ,
		 @i:=@i+1 as i,
		 @k:=(case when @score=a.sum_score then @k else @i end) as 排行,
		 @score:=a.sum_score  AS 总成绩
		FROM 
		(
			SELECT a.s_id,SUM(a.s_score) AS sum_score FROM Score a GROUP BY a.s_id ORDER BY SUM(a.s_score) DESC
		) a,
		(select @k:=0,@i:=0,@score:=0) s
		
-- 21、查询不同老师所教不同课程平均分从高到低显示 
SELECT a.t_id,t.t_name,ROUND(AVG(b.s_score),2) FROM Course a 
JOIN Score b ON a.c_id=b.c_id
JOIN Teacher t ON t.t_id=a.t_id
GROUP BY t.t_id
ORDER BY AVG(b.s_score) DESC
-- 25、查询各科成绩前三名的记录
            -- 1.选出b表比a表成绩大的所有组
            -- 2.选出比当前id成绩大的 小于三个的

  select a.s_id,a.c_id,a.s_score from Score a 
            left join Score b on a.c_id = b.c_id and a.s_score<b.s_score
            group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
            ORDER BY a.c_id,a.s_score DESC
-- 28、查询男生、女生人数 
SELECT s_sex,COUNT(s_sex) FROM Student GROUP BY s_sex
-- 29、查询名字中含有"风"字的学生信息
SELECT * FROM Student WHERE s_name LIKE '%风%'
-- 30、查询同名同性学生名单,并统计同名人数 
SELECT a.s_name,a.s_sex,COUNT(*) FROM Student a 
JOIN Student b ON a.s_id!=b.s_id AND a.s_name=b.s_name AND a.s_sex=b.s_sex
GROUP BY a.s_name,a.s_sex
-- 31、查询1990年出生的学生名单
select s_name from Student where s_birth like '1990%'
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 
SELECT a.c_id,ROUND(AVG(a.s_score),2) FROM Score a GROUP BY a.c_id ORDER BY AVG(a.s_score) DESC, a.c_id ASC
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 
SELECT b.s_name,a.s_score FROM Score a LEFT JOIN Student b ON a.s_id=b.s_id WHERE a.c_id IN
(
	select c_id from Course WHERE c_name='数学' AND a.s_score<60
)
-- 35、查询所有学生的课程及分数情况;
SELECT
	a.s_id,a.s_name ,
	SUM(CASE c.c_name WHEN '语文' THEN b.s_score ELSE 0 END) AS '语文',
	SUM(CASE c.c_name WHEN '数学' THEN b.s_score ELSE 0 END) AS '数学',
	SUM(CASE c.c_name WHEN '英语' THEN b.s_score ELSE 0 END) AS '英语',
	SUM(b.s_score) AS '总成绩'
FROM
	Student a
	LEFT JOIN Score b ON a.s_id = b.s_id
	LEFT JOIN Course c ON b.c_id=c.c_id
GROUP BY
	a.s_id,s_name
	
-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
SELECT
	a.s_id,a.s_name,c.c_name,b.s_score 
FROM
	Course c
	LEFT JOIN Score b ON c.c_id = b.c_id
	LEFT JOIN Student a ON b.s_id = a.s_id 
WHERE
	b.s_score >= 70

-- 37、查询不及格的课程
SELECT
	a.c_id,a.c_name,b.s_score 
FROM
	Course a
	JOIN Score b ON a.c_id = b.c_id 
WHERE
	b.s_score < 60

-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
SELECT
	a.s_id,b.s_name 
FROM
	Score a
	LEFT JOIN Student b ON a.s_id = b.s_id 
WHERE
	a.c_id = '01' 
	AND a.s_score >= 80
-- 39、求每门课程的学生人数 
SELECT
	b.c_name AS '课程',COUNT( a.c_id ) AS '选课人数' 
FROM
	Score a
	JOIN Course b ON a.c_id = b.c_id 
GROUP BY
	b.c_name
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT a.*,b.s_score,c.c_name FROM Student a LEFT JOIN Score b ON a.s_id=b.s_id 
LEFT JOIN Course c ON c.c_id=b.c_id
WHERE b.c_id IN
(
	SELECT c.c_id FROM Course c JOIN Teacher t ON c.t_id=t.t_id WHERE t.t_name='张三'
)
AND b.s_score IN 
(
	SELECT MAX(s_score) FROM Score WHERE c_id='02'
)

-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 
SELECT b.s_id,b.c_id,b.s_score  FROM Score a,Score b WHERE a.c_id!=b.c_id AND a.s_score=b.s_score
-- 42、查询每门功成绩最好的前两名 
SELECT a.s_id,a.c_id,a.s_score FROM Score a WHERE 
(
	SELECT  COUNT(1) FROM Score b WHERE b.c_id=a.c_id AND b.s_score>=a.s_score
)<=2
ORDER BY 
a.c_id

-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列  
  select c_id,count(*) as total from Score GROUP BY c_id HAVING total>5 ORDER BY total,c_id ASC
-- 45、查询选修了全部课程的学生信息 
SELECT * FROM  Student WHERE s_id IN 
(
	SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(*)=(SELECT COUNT(*) FROM Course)
)
-- 46、查询各学生的年龄
    -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT *,
DATE_FORMAT(NOW(),'%Y')-(DATE_FORMAT(s_birth,'%Y')-(CASE WHEN DATE_FORMAT(NOW(),'%m%d')<DATE_FORMAT(s_birth,'%m%d') THEN 0 ELSE 1 END)) AS age
FROM Student

希望有什么不对的或者不好的路过的大神多多指教。