参考:https://zhuanlan.zhihu.com/p/38354000
再次感谢作者的整理!!

1.数据已提前准备好了,已知有如下4张表:
学生表:student

成绩表:score(学号,课程号,成绩)

课程表:course

教师表:teacher

 2.1 简单查询

2.1.1 查询姓"曹"的学生名单

SELECT * FROM student WHERE `name` LIKE '曹%'

2.1.2 查询姓名中最后一个是"玉"的学生名单

SELECT * FROM student WHERE `name` LIKE '%玉'

2.1.3 查询姓名中带"星"的学生名单

SELECT * FROM student WHERE `name` LIKE '%星%'

 

2.2 汇总查询

2.2.1 查询课程编号为"0002"的总成绩

SELECT SUM(grade) as '总成绩' FROM score WHERE course_id='0002'

2.2.2 查询选了课程的学生人数

SELECT COUNT(DISTINCT stu_id)  as '选了课程的学生人数' FROM score # DISTINCT  去重

2.3 分组查询

2.3.1 查询各科成绩最高和最低得分

SELECT MAX(grade) as '最高分',MIN(grade) as '最低分' FROM score ORDER BY grade

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

SELECT course_id as '课程号' ,COUNT(stu_id) as '学生人数' FROM score GROUP BY course_id  

2.3.3 查询学生中男、女人数

SELECT sex,COUNT(sex) as '人数' FROM student GROUP BY sex

2.4 带条件的分组查询

2.4.1 查询平均成绩大于60分学生的学号和平均成绩

SELECT stu_id as '学号', AVG(grade) as '平均成绩' FROM score GROUP BY stu_id HAVING AVG(grade) > 60 

2.4.2 查询至少选修两门课程的学生学号以及课程数目

SELECT stu_id as '学生学号', COUNT(course_id) as '课程数目' FROM score GROUP BY stu_id HAVING COUNT(course_id) > 2

2.4.3 查询同名同姓学生名单并统计同名人数

SELECT name as '学生名单' ,COUNT( name) as '同名人数' FROM student GROUP BY `name`

2.4.4 查询不及格的课程并按照课程号从大到小排序

SELECT course_id,grade from score WHERE grade < 85 ORDER BY grade DESC

2.4.5 查询每门课程的平均成绩,结果按照平均成绩升序排序,平均成绩相同时,按照课程好降序排序

SELECT course_id as '课程号',AVG(grade) as '平均成绩' from score GROUP BY course_id ORDER BY AVG(grade) and course_id

2.4.6 查询其中课程编号为"0003"且分数小于90的学生学号,结果按照分数降序排列

SELECT course_id , stu_id, grade from score WHERE course_id='0003' AND grade < 90 ORDER BY grade DESC

2.4.7 查询课程号和选修此课程人数,查询结果按照人数排序降序,若人数相同,按照课程号升序排序

SELECT course_id as '课程号', COUNT(course_id) as '课程人数' from score GROUP BY course_id ORDER BY COUNT(course_id) DESC,course_id ASC

2.4.8 查询两门以上成绩不满85分的同学的学号及其平均成绩

SELECT stu_id as '学号', AVG(grade) as '平均成绩' from score WHERE grade <= 85 GROUP BY stu_id HAVING COUNT(course_id) >= 2 

2.4.9 查询各科成绩前两名的记录

(SELECT *  from score WHERE course_id = '0001' ORDER BY grade DESC LIMIT 2) union
(SELECT * from score WHERE course_id = '0002' ORDER BY grade DESC LIMIT 2) union
(SELECT * from score WHERE course_id = '0003' ORDER BY grade DESC LIMIT 2);

2.5 汇总查询

2.5.1 查询学生的成绩并进行排名

SELECT stu_id , SUM(grade) FROM score GROUP BY stu_id ORDER BY SUM(grade)

2.5.2 查询平均成绩大于80分的学生的学号和平均成绩

SELECT stu_id ,AVG(grade) FROM score GROUP BY stu_id HAVING AVG(grade) > 80  

2.5.3 查询所有课程成绩小于85分的学生的学号、姓名

SELECT id , name  FROM student WHERE  id in (SELECT stu_id  FROM score GROUP BY stu_id HAVING AVG(grade) < 85);

2.5.3 查询没有学全所有课程的学生的学号、姓名

SELECT id , name  FROM student WHERE  id in ( SELECT stu_id  FROM score GROUP BY stu_id HAVING COUNT(course_id) < 3);

2.5.4 查询1996年出生的学生名单

SELECT * FROM student WHERE year(brith) = 1996

2.5.5 查询各学生的年龄

SELECT id,`name`,TIMESTAMPDIFF(year,brith,now()) from student;

2.6 多表查询

2.6.1 查询所有学生的学号、姓名、选课数、总成绩

SELECT s1.id , s1.`name`, COUNT(s2.course_id) as 'count', SUM(grade) FROM student as s1 JOIN score as s2 WHERE s1.id = s2.stu_id GROUP BY s2.stu_id

2.6.2 查询平均成绩大于85分的所有学生的学号、姓名、平均成绩

SELECT s1.id , s1.`name`, AVG(grade) FROM student as s1 JOIN score as s2 WHERE s1.id = s2.stu_id GROUP BY s2.stu_id HAVING  AVG(grade) > 85

2.6.3 查询学生的选课情况:学号、姓名、课程号、课程名称

SELECT s1.id as '学号', s1.`name` as '姓名', s2.course_id as '课程号' ,c1.`name` as '课程名称' FROM student as s1 JOIN score as s2 JOIN course as c1 WHERE s1.id = s2.stu_id  and s2.course_id = c1.id;

2.6.4 查询出每门课程的大于80得人数和不大于80的人数

SELECT course_id, SUM(CASE WHEN grade > 80 THEN 1 ELSE 0 END) as '大于80', SUM(CASE WHEN grade <= 80 THEN 1 ELSE 0 END) as '小于80' FROM score  GROUP BY course_id

2.6.5 使用分段[90,100],[80-90),[70,80),[60,70)区间统计各科成绩,统计各分段人数和,课程号,课程名称

SELECT s.course_id as '课程号',c1.`name` as '课程号',
sum(case when s.grade >= 60 and s.grade < 70 then 1 else 0 end) as '[60,70)',
sum(case when s.grade >= 70 and s.grade < 80 then 1 else 0 end) as '[70,80)',
sum(case when s.grade >= 80 and s.grade < 90 then 1 else 0 end) as '[80,90)',
sum(case when s.grade >= 90 and s.grade < 100 then 1 else 0 end) as '[90,100)'
from score as s join course as c1 where s.course_id = c1.id group by s.course_id;

2.6.6. 查询课程编号为"0003"且课程成绩在90分以上的学生的学号和姓名

SELECT  s2.id as '学号',s2.`name` as '姓名'
from score as s1 join student as s2 where s1.stu_id = s2.id AND s1.course_id = '0003' AND s1.grade > 90;

2.6.7 数据的行列如何互换?

# 使用max()聚合函数将三个结果中的最大的提取出
select stu_id,
MAX(case course_id when '0001' then grade else 0 end ) as '课程号0001',
max((case course_id when '0002' then grade else 0 end)) as '课程号0002',
max((case course_id when '0003' then grade else 0 end)) as '课程号0003'
FROM score group by stu_id;

2.7 多表连接查询

2.7.1 查询课程号为"0001"的课程分数小于90的学生信息,按照分数降序排列

select s2.id,s2.name,s1.course_id,s1.grade FROM score as s1 JOIN student as s2
WHERE s1.stu_id = s2.id AND s1.course_id = '0001' HAVING grade < 90 ORDER BY grade desc

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

select c1.teacher_id, s1.course_id, c1.`name`, avg(grade)
FROM score as s1 JOIN course as c1
WHERE s1.course_id = c1.id GROUP BY c1.teacher_id ORDER BY AVG( s1.grade) DESC

2.7.3 查询课程名称为"数学",且分数低于90的学生姓名和分数

select s2.`name` as '学生姓名', s1.grade as '分数'
FROM score as s1 JOIN course as c1 JOIN student as s2
WHERE s1.course_id = c1.id AND s1.stu_id = s2.id
AND c1.`name` = '数学' and s1.grade < 90

2.7.4 查询两门及其以上课程小于85的同学的学号,姓名及其平均成绩

select s1.id as '学号',s1.name as '姓名',avg(s2.grade) as '平均成绩'
from student as s1 join score as s2 on s1.id = s2.stu_id and s2.grade < 85
group by s2.stu_id having count(s1.id) >= 2;

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

SELECT
DISTINCT s1.stu_id as '学生编号', s1.course_id as '课程编号', s1.grade as '学生成绩'
FROM score AS s1 JOIN score AS s2
ON s1.stu_id = s2.stu_id
WHERE s1.course_id != s2.course_id AND s1.grade = s2.grade

2.7.6 查询课程编号为“0002”的课程比“0001”的课程成绩高的所有学生的学号

SELECT
DISTINCT a.stu_id,a.grade as '0002成绩',b.grade as '0001成绩'
FROM
(SELECT stu_id,grade FROM score WHERE course_id = '0002' ) as a
join
(SELECT stu_id,grade FROM score WHERE course_id = '0001' ) as b
ON a.stu_id = b.stu_id
where a.grade > b.grade;

2.7.7 查询学过编号为“0001”的课程并且也学过编号为“0002”的课程的学生的学号、姓名

SELECT
a.stu_id as '学号',s1.`name` as '姓名'
FROM
(SELECT stu_id FROM score WHERE course_id = '0002' ) as a
join
(SELECT stu_id FROM score WHERE course_id = '0001' ) as b
ON a.stu_id = b.stu_id
JOIN student as s1 on s1.id = b.stu_id

2.7.8 查询学过“陈独秀”老师所教的所有课的同学的学号、姓名

SELECT
DISTINCT s1.stu_id as '学号',s2.`name` as '姓名'
FROM
(SELECT course_id,t1.`name` FROM teacher as t1 JOIN score as s1 on t1.id = s1.course_id WHERE t1.`name` = '陈独秀') as a
join
score as s1
JOIN
student as s2
WHERE a.course_id = s1.course_id AND s1.stu_id = s2.id

2.7.9 查询至少有一门课与学号为“0001”的学生所学课程相同的学生的学号和姓名`


SELECT
s1.id,s1.`name`
FROM student as s1
WHERE s1.id in
(SELECT DISTINCT(stu_id) from score WHERE course_id in (SELECT course_id FROM score WHERE stu_id = '0001'))
AND s1.id != '0001';

 

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

SELECT
s1.stu_id as '学生号',
MAX(CASE WHEN c1.`name` = '语文' THEN s1.grade ELSE 0 END) as '语文',
MAX(CASE WHEN c1.`name` = '数学' THEN s1.grade ELSE 0 END) as '数学',
MAX(CASE WHEN c1.`name` = '英语' THEN s1.grade ELSE 0 END) as '英语',
avg(s1.grade) as '平均成绩'
FROM course as c1 JOIN score as s1
ON c1.id = s1.course_id
GROUP BY s1.stu_id

SQL语句实战学习的更多相关文章

  1. SQL语句的学习

    SQL语句的学习 要交作业了,刚好把SQL查询语句的内容写成笔记,以后好查看.水一下 单表查询 DISTINCT:去掉结果中的重复行作用,将DISTINCT关键字放在select的后面.目标列名的前面 ...

  2. 50个Sql语句实战

    /* 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句. 问题及描述:--1.学生表Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生 ...

  3. 标准sql语句,学习

    标准SQL语句总结标准SQL语句总结,标准SQL语言基本上适用于下面所列出的数据库软件 -------------------------------------------------------- ...

  4. sql语句的学习(1)

    一.创建表 CREATE TABLE `student` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) DEFAULT NULL COMMENT '姓名', ` ...

  5. (转)新手C#SQL语句的学习2018.08.13

    1.创建数据库(create) CREATE DATABASE database-name 2.删除数据库(drop) drop database dbname 3.备份数据库 --- 创建 备份数据 ...

  6. Oracle PL/SQL语句基础学习笔记(上)

    PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE server和其它工具中了,近几年中很多其它的开发者和DBA開始使用PL/SQL,本文将讲述PL ...

  7. SQL语句优化 学习笔记

    sql语句时间花在哪了? 1 等待时间 2 执行时间 这两个时间并非孤立的,单条语句执行的快 其他语句等待的时间就少 执行时间花在哪了? 1 查找 沿着索引查找 慢者可能全表扫描 2 取出 查到行后, ...

  8. sql语句的学习(2)

    7.统计:学号.姓名.语文.数学.英语.总分.平均成绩 8.列出各门课程的平均成绩.课程,平均成绩 9.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名) 10.列出数学成绩在2-3名的学生( ...

  9. SQL语句的学习理解

    1.手电筒当天下载自定义活跃: SELECT user_dim.app_info.app_instance_id FROM [flashlight35-6aae4:com_scroll_sos_fla ...

随机推荐

  1. os模块,sys模块,json模块,subprocess模块

    os模块 一·什么是os模块 os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同 的平台进行相应的操作,在python编程时,经常和文件.目录打交道 ...

  2. 斯坦福NLP课程 | 第12讲 - NLP子词模型

    作者:韩信子@ShowMeAI,路遥@ShowMeAI,奇异果@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www. ...

  3. syc-day2

    第1题:mod注意负数. 第2题:dp 第3题:构造(奇偶性) 第4题:线段树

  4. WC2015 题解

    K小割 题目链接:WC2015 K小割 Description 题目很清楚了,已经不能说的更简洁了-- Solution 这道题出题人挺毒的,你需要针对不同的部分分施用不同的做法 . 第\(1\)部分 ...

  5. springcloud-- Alibaba-nacos--支持的几种服务消费方式

    通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...

  6. (干货)基于 veImageX 搭建海报生成平台 -- 附源码

    前言 618 年中促销即将来临,很多公司都会通过海报来宣传自己的促销方案,通常情况下海报由设计团队基于 PS.Sketch 等工具创作,后期若想替换海报文案.商品列表等内容则需打开原工程进行二次创作, ...

  7. 模块re正则

    正则表达式 内容概要 正则表达式前戏 正则表达式之字符组 正则表达式特殊符号 正则表达式量词 正则表达式贪婪与非贪婪匹配 正则表达式取消转义 python内置模块之re模块 内容详情 正则表达式前戏 ...

  8. 基于camunda开源流程引擎如何实现会签及会签原理解析

    一.背景 市场上比较有名的开源流程引擎有osworkflow.jbpm.activiti.flowable.camunda.由于jbpm.activiti.flowable这几个流程引擎出现的比较早, ...

  9. 网络协议之:memcached binary protocol详解

    目录 简介 memcached的协议包 memcached命令举例 总结 简介 前面讲到了memcached的文本协议,虽然文本协议看起来非常简单,但是对于客户端来说一般还是会选择效率更高的二进制协议 ...

  10. 用Typescript 的方式封装Vue3的表单绑定,支持防抖等功能。

    Vue3 的父子组件传值.绑定表单数据.UI库的二次封装.防抖等,想来大家都很熟悉了,本篇介绍一种使用 Typescript 的方式进行统一的封装的方法. 基础使用方法 Vue3对于表单的绑定提供了一 ...