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. 我与Apache DolphinScheduler的成长之路

    关于 Apache DolphinScheduler社区 Apache DolphinScheduler(incubator) 于17年在易观数科立项,19年3月开源, 19 年8月进入Apache ...

  2. django自带的序列化组件

    1.什么是序列化组件 在django中,自带一个序列化组件,它是用来将数据进行整理.转化成特定的为一个特定的格式(比如json数据格式),然后传输给前端,以便前端对数据进行处理操作. 2.为什么要用序 ...

  3. 基于ASP.NET Core 6.0的整洁架构

    大家好,我是张飞洪,感谢您的阅读,我会不定期和你分享学习心得,希望我的文章能成为你成长路上的垫脚石,让我们一起精进. 本节将介绍基于ASP.NET Core的整洁架构的设计理念,同时基于理论落地的代码 ...

  4. Android Notification使用

    一 Notification的类别 1.状态栏和抽屉式通知 //获取NotificationManager对象 val notificationManager = getSystemService(N ...

  5. Hack The Box( Starting Point )

    Hack The Box [Starting Point] 初始点 -- 了解渗透测试的基础知识. 这一章节对于一个渗透小白来说,可以快速的成长.以下将提供详细的解题思路,与实操步骤. TIER 0 ...

  6. 第五十三篇:Vue安装Element ui

    好家伙,之前写的一篇过时了,用不了了,更新一波 (已新建一个vue项目) 1. 在项目目录下执行:npm i element-ui -S 2. 在main.js中写入 import ElementUI ...

  7. Zookeeper+dubbo+Springboot集成总结

    1. 尽量用XML 集成,这也的Dubbo官方推荐的集成方式 自己在使用注解集成过程中发现有坑:Springmvc包扫描和dubbo包扫描冲突,导致消费端一直拿不到代理对象(null),非常蛋疼,所以 ...

  8. Spark 读 Hbase

    package com.grady import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.c ...

  9. 066_末晨曦Vue技术_过渡 & 动画之多个元素的过渡

    多个元素的过渡 点击打开视频讲解更加详细 我们之后讨论多个组件的过渡,对于原生标签可以使用 v-if/v-else.最常见的多标签过渡是一个列表和描述这个列表为空消息的元素: <transiti ...

  10. web字体浮在图像中央

    在做项目的过程中遇到了需要将图像作为背景,将字体显示在图像中央需求. 尝试了两种做法: 第一种方法为设置一个div设置属性为relative固定这个框的位置,将图片铺在div块里. 在div再设一个d ...