SQL面试练习(MySql)
创建测试数据库:
/*如果已经存在此数据库,先删除*/
drop database if exists sqltest ;
/*创建并设置编码为UTF-8*/
create database sqltest default character set utf8 ;
一、单表查询
| id | sno | name | cno | course | score |
| 2 | 20170101001 | 张三 | 1001 | 语文 | 86 |
| 3 | 20170101001 | 张三 | 1002 | 数学 | 56 |
| 4 | 20170101001 | 张三 | 1003 | 英语 | 48 |
| 5 | 20170101001 | 张三 | 1004 | 化学 | 90 |
| 6 | 20170101001 | 张三 | 1005 | 物理 | 57 |
| 7 | 20170101002 | 李四 | 1001 | 语文 | 90 |
| 8 | 20170101002 | 李四 | 1002 | 数学 | 68 |
| 9 | 20170101002 | 李四 | 1003 | 英语 | 87 |
| 10 | 20170101002 | 李四 | 1004 | 化学 | 68 |
| 11 | 20170101002 | 李四 | 1005 | 物理 | 45 |
| 12 | 20170101003 | 王五 | 1001 | 语文 | 86 |
| 13 | 20170101003 | 王五 | 1002 | 数学 | 60 |
| 14 | 20170101003 | 王五 | 1003 | 英语 | 48 |
| 15 | 20170101003 | 王五 | 1004 | 化学 | 90 |
| 16 | 20170101003 | 王五 | 1005 | 物理 | 70 |
| 17 | 20170101004 | 赵六 | 1001 | 语文 | 82 |
| 18 | 20170101004 | 赵六 | 1002 | 数学 | 80 |
| 19 | 20170101004 | 赵六 | 1003 | 英语 | 30 |
| 20 | 20170101004 | 赵六 | 1004 | 化学 | 70 |
| 21 | 20170101004 | 赵六 | 1005 | 物理 | 88 |
/*切换至测试数据库*/
use sqltest; /*如果表存在,则删除*/
drop table if exists tb_score ; /*创建学生表ST*/
CREATE TABLE `tb_score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Sno` varchar(50) not null ,
`name` varchar(50) DEFAULT NULL,
`Cno` varchar(50) not null ,
`course` varchar(50) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; /*插入数据*/
insert into tb_score (sno,name,cno,course,score) value('','张三','','语文','') ;
insert into tb_score (sno,name,cno,course,score) value('','张三','','数学','') ;
insert into tb_score (sno,name,cno,course,score) value('','张三','','英语','') ;
insert into tb_score (sno,name,cno,course,score) value('','张三','','化学','') ;
insert into tb_score (sno,name,cno,course,score) value('','张三','','物理','') ; insert into tb_score (sno,name,cno,course,score) value('','李四','','语文','') ;
insert into tb_score (sno,name,cno,course,score) value('','李四','','数学','') ;
insert into tb_score (sno,name,cno,course,score) value('','李四','','英语','') ;
insert into tb_score (sno,name,cno,course,score) value('','李四','','化学','') ;
insert into tb_score (sno,name,cno,course,score) value('','李四','','物理','') ; insert into tb_score (sno,name,cno,course,score) value('','王五','','语文','') ;
insert into tb_score (sno,name,cno,course,score) value('','王五','','数学','') ;
insert into tb_score (sno,name,cno,course,score) value('','王五','','英语','') ;
insert into tb_score (sno,name,cno,course,score) value('','王五','','化学','') ;
insert into tb_score (sno,name,cno,course,score) value('','王五','','物理','') ; insert into tb_score (sno,name,cno,course,score) value('','赵六','','语文','') ;
insert into tb_score (sno,name,cno,course,score) value('','赵六','','数学','') ;
insert into tb_score (sno,name,cno,course,score) value('','赵六','','英语','') ;
insert into tb_score (sno,name,cno,course,score) value('','赵六','','化学','') ;
insert into tb_score (sno,name,cno,course,score) value('','赵六','','物理','') ;
1.1查询不及格科目数大于等于2的学生学号、姓名和不及格科目数量:
/*不加having条件筛选*/
select sno as '学号' , name as '姓名' , count(score) as '不及格科目数' from tb_score where score < 60 group by sno ;

select sno as '学号' , name as '姓名' , count(score) as '不及格科目数' from tb_score where score < 60 group by sno having count(score) > 2 ;

1.2查询不及格科目数大于等于2的学生学号、姓名:
select sno as '学号' , name as '姓名' from tb_score where score < 60 group by sno having count(score) > 2 ;

1.3、查询不及格科目数大于等于2的学生学号、学生姓名、科目号、科目名称和分数,并按学号降序、科目号升序排序:
中间步骤探索
select * from tb_score where score < 60 group by sno having count(score) >= 2 order by sno desc , cno asc ;

最终结果
select * from tb_score where score < 60 and sno in (
select sno from tb_score where score < 60 group by sno having count(score) >= 2
)
order by sno desc , cno asc ;

二、多表查询
/*选择数据库*/
use sqltest ; /*删除已有的学生表*/
drop table if exists tb_student ; /*创建学生表*/
CREATE TABLE `tb_student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stuId` varchar(45) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`clsId` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*插入学生的数据*/
insert into tb_student (stuId,name,clsId) value ('','张三','') ;
insert into tb_student (stuId,name,clsId) value ('','李四','') ;
insert into tb_student (stuId,name,clsId) value ('','王五','') ;
insert into tb_student (stuId,name,clsId) value ('','赵六','') ;
insert into tb_student (stuId,name,clsId) value ('','巩发财','') ;
insert into tb_student (stuId,name,clsId) value ('','柏拉图','') ;
insert into tb_student (stuId,name,clsId) value ('','钱老虎','') ;
insert into tb_student (stuId,name,clsId) value ('','杨伟','') ;
insert into tb_student (stuId,name,clsId) value ('','陈海','') ;
/*删除已有的班级表*/
drop table if exists tb_class ; /*创建班级表*/
CREATE TABLE `tb_class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clsId` varchar(45) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*插入班级的数据*/
insert into tb_class (clsId,name) value ('','高二(1)班') ;
insert into tb_class (clsId,name) value ('','高二(2)班') ;
insert into tb_class (clsId,name) value ('','高二(3)班') ;
/*删除已有的课程表*/
drop table if exists tb_course ; /*创建课程表*/
CREATE TABLE `tb_course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`couId` varchar(45) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*插入课程数据*/
insert into tb_course (couId,name) value ('A01','语文') ;
insert into tb_course (couId,name) value ('A02','数学') ;
insert into tb_course (couId,name) value ('A03','英语') ;
/*删除已有的分数表*/
drop table if exists tb_score ; /*创建分数表*/
CREATE TABLE `tb_score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stuId` varchar(45) DEFAULT NULL,
`couId` varchar(45) DEFAULT NULL,
`score` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*插入分数数据*/
insert into tb_score (stuId,couId,score) value ('','A01',81) ;
insert into tb_score (stuId,couId,score) value ('','A02',73) ;
insert into tb_score (stuId,couId,score) value ('','A03',79) ; insert into tb_score (stuId,couId,score) value ('','A01',79) ;
insert into tb_score (stuId,couId,score) value ('','A02',83) ;
insert into tb_score (stuId,couId,score) value ('','A03',87) ; insert into tb_score (stuId,couId,score) value ('','A01',65) ;
insert into tb_score (stuId,couId,score) value ('','A02',97) ;
insert into tb_score (stuId,couId,score) value ('','A03',65) ; insert into tb_score (stuId,couId,score) value ('','A01',78) ;
insert into tb_score (stuId,couId,score) value ('','A02',86) ;
insert into tb_score (stuId,couId,score) value ('','A03',78) ; insert into tb_score (stuId,couId,score) value ('','A01',67) ;
insert into tb_score (stuId,couId,score) value ('','A02',89) ;
insert into tb_score (stuId,couId,score) value ('','A03',88) ; insert into tb_score (stuId,couId,score) value ('','A01',98) ;
insert into tb_score (stuId,couId,score) value ('','A02',90) ;
insert into tb_score (stuId,couId,score) value ('','A03',92) ; insert into tb_score (stuId,couId,score) value ('','A01',85) ;
insert into tb_score (stuId,couId,score) value ('','A02',78) ;
insert into tb_score (stuId,couId,score) value ('','A03',72) ; insert into tb_score (stuId,couId,score) value ('','A01',78) ;
insert into tb_score (stuId,couId,score) value ('','A02',85) ;
insert into tb_score (stuId,couId,score) value ('','A03',77) ; insert into tb_score (stuId,couId,score) value ('','A01',68) ;
insert into tb_score (stuId,couId,score) value ('','A02',91) ;
insert into tb_score (stuId,couId,score) value ('','A03',94) ;
|
学生表
|
分数表
|
|
班级表
|
|
|
课程表
|
2.1查询各班各科最高分数:
SELECT
cls.name AS '班级',
cou.name AS '课程',
MAX(temp.score) AS '最高分'
FROM
(SELECT
st.clsId, sc.couId, sc.score
FROM
tb_score AS sc
INNER JOIN tb_student AS st ON sc.stuId = st.stuId) as temp
INNER JOIN tb_class AS cls ON temp.clsId = cls.clsId
INNER JOIN tb_course AS cou ON temp.couId = cou.couId
GROUP BY temp.clsId , temp.couId

SQL面试练习(MySql)的更多相关文章
- php面试专题---MySQL常用SQL语句优化
php面试专题---MySQL常用SQL语句优化 一.总结 一句话总结: 原理,万变不离其宗:其实SQL语句优化的过程中,无非就是对mysql的执行计划理解,以及B+树索引的理解,其实只要我们理解执行 ...
- php面试专题---Mysql索引原理及SQL优化
php面试专题---Mysql索引原理及SQL优化 一.总结 一句话总结: 注意:只写精品 1.为表设置索引要付出代价 是什么? 存储空间:一是增加了数据库的存储空间 修改插入变动索引时间:二是在插入 ...
- php面试专题---MYSQL查询语句优化
php面试专题---MYSQL查询语句优化 一.总结 一句话总结: mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存) ...
- How to Disable Strict SQL Mode in MySQL 5.7
If your app was written for older versions of MySQL and is not compatible with strict SQL mode in My ...
- 通过sql server 连接mysql
图文:通过sql server 连接mysql 1.在SQL SERVER服务器上安装MYSQL ODBC驱动; 驱动下载地址:http://dev.mysql.com/downloads/con ...
- 如何用SQL语句实现Mysql数据库的备份与还原
以前一直做android客户端的项目,根本没有开发asp.net mvc的开发,现阶段做了一个模块,参数设置,以及数据库的备份与还原.其需求如下: 参数设置 本项参数设置为对自动数据备份进行设置,管理 ...
- 数据库 SQL Server 到 MySQL 迁移方法总结
最近接手一起老项目数据库 SQL Server 到 MySQL 的迁移.因此迁移前进行了一些调查和总结.下面是一些 SQL Server 到 MySQL 的迁移方法. 1. 使用 SQLyog 迁移 ...
- 一道SQL面试例题 if...else 与聚集函数
晚上回来,同学说面试遇到了一个SQL面试题目,自己做了一下,总结总结. 题目如下: 下面是产品数据表(产品id,颜色col,数量num),其中每种产品有1~2种颜色. 求每种产品各颜色的数量差值(对于 ...
- Monitor All SQL Queries in MySQL (alias mysql profiler)
video from youtube: http://www.youtube.com/watch?v=79NWqv3aPRI one blog post: Monitor All SQL Querie ...
随机推荐
- 4,list,list的列表嵌套,range
list 索引,切片+步长 # li = [, True, (, , , , , , '小明',], {'name':'alex'}] #索引,切片,步长 # print(li[]) # print( ...
- Java-改变Class
改变一个Class对象的类型 package com.tj; public class MyClass2 { public static void main(String[] args) { Obje ...
- CSS 如何让 height:100%; 起作用
当你设置一个页面元素的高度(height)为100%时,期望这样元素能撑满整个浏览器窗口的高度,但大多数情况下,这样的做法没有任何效果.你知道为什么height:100%不起作用吗? 按常理,当我们用 ...
- [automator学习篇]android 接口-- bluetooth
http://www.android-doc.com/guide/topics/connectivity/bluetooth.html 本地下载的sdk 目录: D:\AndroidSdk\s ...
- system sys,sysoper sysdba 的区别
--===================================== -- system sys,sysoper sysdba 的区别 --========================= ...
- 【Luogu】P1312Mayan游戏(暴搜)
题目链接 由于是暴搜题,所以这篇博客只讲怎么优化剪枝,以及一些细节. 模拟消除思路:因为消除可以拆分成小的横条或竖条,而这些条的长度至少为三,所以一块可消除的区域至少会有一个中心点.这里的中心点可以不 ...
- BZOJ 1191: [HNOI2006]超级英雄Hero【二分图匹配】
裸的匹配题,一眼就能看出来二分图的模型,是某个经典题的改编.貌似某本图论书上讲过的,有N个人以及M个职位,每个职位只能提供给一个人,而每个人由于能力有限只能胜任有限个职位,问是否有办法使得每个人都有工 ...
- 刷题总结——魔术球问题(ssoj最小路径覆盖+网络流)
题目: 题目描述 假设有 n 根柱子,现要按下述规则在这 n 根柱子中依次放入编号为 1,2 ,3,… 的球.(1)每次只能在某根柱子的最上面放球.(2)在同一根柱子中,任何 2 个相邻球的编号之和为 ...
- LightOJ1125 Divisible Group Sums
Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you c ...
- jQuery事件委托之Safari失效的解决办法--摘抄
什么是事件委托 事件委托是Jquery中一种事件绑定的方式,不同于常见的事件绑定方式将事件绑定在目标元素上,而是将事件绑定在父级元素上通过事件冒泡来执行绑定函数. //常见的事件绑定(Jquery) ...



