sql面试50题------(1-10)
文章目录
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)的更多相关文章
- SQL面试50题------(初始化工作、建立表格)
文章目录 1.建表 1.1 学生表和插入数据 1.2 教师表和数据 1.3 课程表和数据 1.4 成绩表和数据 2.数据库数据 2.1 学生表 2.2 教师表 2.3 课程表 2.4 得分表 1.建表 ...
- SQL面试50题
1.查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(重点) SELECT a.s_id,a.s_score FROM (') as a INNER JOIN (') as b on ...
- sql面试50题------(11-20)
文章目录 11.查询至少有一门课与学号为'01'的学生所学课程相同的学生的学号和姓名 12.查询和'01'号同学所学课程完全相同的其他同学的学号 13.查询两门及其以上不及格课程的同学的学号,姓名及其 ...
- sql面试50题------(21-30)
文章目录 21.查询不同老师所教不同课程平均分从高到低显示 23.使用分段[100,85),[85,70),[70,60),[<60] 来统计各科成绩,分别统计各分数段人数:课程ID和课程名称 ...
- 剑指offer 面试50题
面试50题: 题目:第一个只出现一次的字符 题:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 解题思路一:利用Python特 ...
- 转:sql 经典50题--可能是你见过的最全解析
题记:从知乎上看到的一篇文章,刚好最近工作中发现遇到的题目与这个几乎一样,可能就是从这里来的吧.^_^ 里面的答案没有细看,SQL求解重在思路,很多时候同一种结果可能有多种写法,比如题中的各科成绩取前 ...
- SQL语句50题
-- 一.创建教学系统的数据库,表,以及数据 --student(sno,sname,sage,ssex) 学生表--course(cno,cname,tno) 课程表--sc(sno,cno,sco ...
- sql查询50题
一个项目涉及到的50个Sql语句问题及描述:--1.学生表Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2 ...
- Linux系统管理员面试50题
命令nslookup是做什么的? Nslookup 是一个 监测网络中 DNS 服务器是否能正确实现域名解析的命令行工具. 你如何把CPU占用率最高的进程显示出来? top -c 按照cpu排序 如果 ...
随机推荐
- 一文搞懂什么是kubernetes Service
1.什么是Service? 在kubernets中,Pod是应用程序的载体,Pod你可以想象成就是容器,为动态的一组Pod提供一个固定的访问入口,它是以一种叫ClusterIP地址来进行标识,而Clu ...
- 技术分享 | Update更新慢、死锁等问题的排查思路分享
欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 一.简介 在开始排错之前我们需要知道 Update 在 MySQL 中的生命周期 ...
- 一般处理程序ashx接入微信服务器配置
首先在威信后台填写服务器相关配置,这里按照说明直接填写就好了.配置提交前在需要在我们服务端先准备号接受微信请求的url,对请求内容做验证. 1.准备接口配置信息 A.服务器URL 该URL用于开发者接 ...
- 【原创】JDK 9-17新功能30分钟详解-语法篇-var
JDK 9-17新功能30分钟详解-语法篇-var 介绍 JDK 10 JDK 10新增了新的关键字--var,官方文档说作用是: Enhance the Java Language to exten ...
- C语言【10部分】
输出整数 #include <stdio.h> int main() { int number; // printf() 输出字符串 printf("输入一个整数: " ...
- 解决eclipse中的Java文件,使用idea打开的乱码问题
吐槽: 在克隆一些Github上面资源的时候,使用idea打开,会出现乱码的情况 而使用eclipse打开,这种情况就会消失.「是因为eclipse使用的是GBK编码,idea使用的是utf-8」 这 ...
- 【java】学习路线10-权限修饰符详解
/*关于修饰符:类:public default public protected default privatesame class √ ...
- Linux虚拟机破解密码步骤
Linux破解密码 重启系统 到达logo界面快速 按 e 编辑当前条目 将光标移至以 linux 开头的行,此为内核命令行 在UTF-8(RHEL7):ro(RHEL8)后添加 rd.break ( ...
- KingbaseES V8R6单实例外部备份案例
案例说明: 本案例采用sys_backup.sh执行物理备份,备份使用如下逻辑架构:数据库主机采用CentOS 7系统,repo采用kylin V10 Server. 单实例+外部备份服务器 备份逻辑 ...
- 【读书笔记】C#高级编程 第五章 泛型
(一)泛型概述 泛型不仅是C#编程语言的一部分,而且与程序集中的IL代码紧密地集成.泛型不仅是C#语言的一种结构,而且是CLR定义的.有了泛型就可以创建独立于被包含类型的类和方法了. 1.性能 泛型的 ...