1、查询课程编号‘01’比课程编号‘02’成绩高的所有学生学号

select a.s_id from
(select * from score where c_id ='01') as a
inner JOIN
(select * from score where c_id ='02') as b on a.s_id = b.s_id
where a.s_score > b.s_score

扩展、同时查询出该学生的姓名和得分

select a.s_id,c.s_name,a.s_score from
(
(select * from score where c_id ='01') as a
inner JOIN
(select * from score where c_id ='02') as b on a.s_id = b.s_id
) INNER JOIN student as c on c.s_id = b.s_id
where a.s_score > b.s_score

2、查询平均成绩大于60分得学生的学号和平均成绩

select s_id,avg(s_score)
from score
group by s_id
having avg(s_score)>60

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

select a.s_id,a.s_name,COUNT(b.c_id),sum(s_score)
from student as a
left JOIN score as b on a.s_id=b.s_id
group by a.s_id

改进null

select a.s_id,a.s_name,COUNT(b.c_id),
sum(case when b.s_score is null then 0 else b.s_score END)
from student as a
left JOIN score as b on a.s_id=b.s_id
group by a.s_id

4、查询姓“猴”的老师的个数

select count(t.t_id)
from teacher as t
where t.t_name like '猴%'


扩展 含有猴的老师的个数
==%==表示多个字符串

select count(t.t_id)
from teacher as t
where t.t_name like '%猴%'

扩展二、几个以“张”开头的不重复姓名。需要去重

select count(distinct t.t_name)
from teacher as t
where t.t_name like '张%'

5、查询没有学过张三老师课的学生的学号和姓名

select s_id,s_name
from student
where 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 ='张三'
)
)
)


使用连接

select s_id,s_name from student where s_id not in(

select score.s_id from teacher as t
inner join course as c on t.t_id = c.t_id
inner join score on c.c_id=score.c_id
where t.t_name='张三' )

6、查询学过张三老师所教的所有课的同学的学号和姓名

select s_id,s_name
from student
where s_id 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 ='张三'
)
)
)


扩展

select s_id,s_name from student where s_id  in(

select score.s_id from teacher as t
inner join course as c on t.t_id = c.t_id
inner join score on c.c_id=score.c_id
where t.t_name='张三' )

7、查询学过编号为01的课程并且也学过编号为02的课程的学生的学号和姓名

select s_id,s_name from student
where s_id in( select a.s_id from
(select * from score where c_id = '01') as a
INNER JOIN
(select * from score where c_id = '02') as b
on a.s_id = b.s_id )

8、查询课程编号为02的总成绩

select sum(s_score) from score group by c_id having c_id='02'


扩展 avg sum count

每门课的选课人数、平均数、总分数

select c_id, sum(s_score),avg(s_score),count(s_score) from score group by c_id 

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

select s_id,s_name from student
where s_id in
(
select a.s_id from (
select s_id,count(c_id) as cnt
from score
where s_score < 60
group by s_id
) as a
INNER JOIN
(
select s_id ,count(c_id) as cnt
from score
group by s_id
) as b on a.s_id=b.s_id where a.cnt = b.cnt )

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

select a.s_id from
(select * from score where c_id ='01') as a
inner JOIN
(select * from score where c_id ='02') as b on a.s_id = b.s_id
where a.s_score > b.s_score

补充知识
inner join(等值连接) 只返回两个表中联结字段相等的行

left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录

right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录

INNER JOIN 语法:

INNER JOIN 连接两个数据表的用法:

SELECT * FROM 表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号

INNER JOIN 连接三个数据表的用法:


SELECT * FROM (表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号

sql面试50题------(1-10)的更多相关文章

  1. SQL面试50题------(初始化工作、建立表格)

    文章目录 1.建表 1.1 学生表和插入数据 1.2 教师表和数据 1.3 课程表和数据 1.4 成绩表和数据 2.数据库数据 2.1 学生表 2.2 教师表 2.3 课程表 2.4 得分表 1.建表 ...

  2. SQL面试50题

    1.查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(重点) SELECT a.s_id,a.s_score FROM (') as a INNER JOIN (') as b on ...

  3. sql面试50题------(11-20)

    文章目录 11.查询至少有一门课与学号为'01'的学生所学课程相同的学生的学号和姓名 12.查询和'01'号同学所学课程完全相同的其他同学的学号 13.查询两门及其以上不及格课程的同学的学号,姓名及其 ...

  4. sql面试50题------(21-30)

    文章目录 21.查询不同老师所教不同课程平均分从高到低显示 23.使用分段[100,85),[85,70),[70,60),[<60] 来统计各科成绩,分别统计各分数段人数:课程ID和课程名称 ...

  5. 剑指offer 面试50题

    面试50题: 题目:第一个只出现一次的字符 题:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 解题思路一:利用Python特 ...

  6. 转:sql 经典50题--可能是你见过的最全解析

    题记:从知乎上看到的一篇文章,刚好最近工作中发现遇到的题目与这个几乎一样,可能就是从这里来的吧.^_^ 里面的答案没有细看,SQL求解重在思路,很多时候同一种结果可能有多种写法,比如题中的各科成绩取前 ...

  7. SQL语句50题

    -- 一.创建教学系统的数据库,表,以及数据 --student(sno,sname,sage,ssex) 学生表--course(cno,cname,tno) 课程表--sc(sno,cno,sco ...

  8. sql查询50题

    一个项目涉及到的50个Sql语句问题及描述:--1.学生表Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2 ...

  9. Linux系统管理员面试50题

    命令nslookup是做什么的? Nslookup 是一个 监测网络中 DNS 服务器是否能正确实现域名解析的命令行工具. 你如何把CPU占用率最高的进程显示出来? top -c 按照cpu排序 如果 ...

随机推荐

  1. 汇编语言基于8086CUP(想学操作系统的前奏!!!)

    汇编语言基于8086CUP(想学操作系统的前奏!!!) 1.汇编语言的产生 1.1.思维图 1.2.单位转换 1B=8bit 1KB=1024B 1MB=1024KB 1GB=1024MB 1TB=1 ...

  2. 新版 Ubuntu 中 gnome-terminal 可恶的行间距问题逼我退回了 Ubuntu 20.04

    不知道从什么时候起(可能是 Ubuntu 21.04,也可能是 Ubuntu 21.10),Ubuntu 中的 gnome-terminal 的行间距就加大了,看起来极其不爽,特别是和 Powerli ...

  3. LuoguP1240 诸侯安置

    本来是来练组合的,不知怎么又开始水普及DP了 #include <cstdio> #include <iostream> #include <cstring> #i ...

  4. java-Servlet编码/异常处理

    1. Servlet输出中文(1)为什么会有乱码?out.println方法在输出时或者表单提交的时候,浏览器会对表单中的中文参数值进行编码; 注:会使用表单所在的页面打开时使用的编码方式进行编码服务 ...

  5. java-运算符与判断

    运算符: 1)算术运算符:+-*/%,++,-- 进行加.减.乘.除.取余数.自增.自减 2)关系运算符:>,<,>=,<=,==,!=    boolean类型 判断两个整形 ...

  6. iommu分析之---intel iommu初始化

    intel 的iommu 是iommu框架的一个实现案例. 由于intel 的iommu 实现得比arm smmv3复杂得多,里面概念也多,所以针对intel 实现的iommu 案例的初始化部分进行一 ...

  7. [多校 NOIP 联合模拟 20201130 T4] ZZH 的旅行(斜率优化dp,启发式合并,平衡树)

    题面 题目背景 因为出题人天天被 ZZH(Zou ZHen) 吊打,所以这场比赛的题目中出现了 ZZH . 简要题面 数据范围 题解 (笔者写两个log的平衡树和启发式合并卡过的,不足为奇) 首先,很 ...

  8. 事物的隔离性和MVCC

    事物的隔离性 mysql的服务端是支持多个客户端同时与之连接的,每个客户端可能还并发了好几个连接,所以mysql是需要同时处理很多事情的,每一件独立的事情就叫做事务.我们知道事务有一个叫隔离性的特性, ...

  9. 从0到1写一款自动为Markdown标题添加序号的Jetbrains插件

    1. markdown-index 最近做了一个Jetbrains的插件,叫markdown-index,它的作用是为Markdown文档的标题自动添加序号,效果如下: 目前已经可以在Jetbrain ...

  10. ubuntu20.4 sgx环境配置

    一.driver安装 1.在该下载地址将3个.bin文件下载下来,下载地址:https://download.01.org/intel-sgx/latest/linux-latest/distro/u ...