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 ...
随机推荐
- C++ char数组和string类简单使用总结
使用char数组,进行字符串的操作,是c风格的操作方式. string是C++的风格,感觉string本质上就是一个vector<char> 以下代码详细展示了字符串的常见操作 #incl ...
- linux下c++sleep函数
#include <iostream> #include <unistd.h> using namespace std; int main(){ int i; ]; memse ...
- element-ul InputNumber 空白
if(this.days == undefined){ this.$nextTick(function(){ this.days = 1; }); }
- bzoj1225 [HNOI2001] 求正整数
1225: [HNOI2001] 求正整数 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 762 Solved: 313[Submit][Statu ...
- essential c++ 随笔
编写一个C++程序: vector初始化两种方法: vector<int>elem_seq(seq_size); elem_seq[0]=1 elem_seq[1]=2; 另一种方法则是利 ...
- LinkedList的构造函数有哪些
LinkedList构造函数有(两种): public LinkedList() public LinkedList(Collection<? extends E> c) /** * Co ...
- hdu4336 Card Collector(概率DP,状态压缩)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- 济南学习 Day 5 T1 晚
取模(mod) [题目描述] 有一个整数a和n个整数b_1, …, b_n.在这些数中选出若干个数并重新排列,得到c_1,…, c_r.我们想保证a mod c_1 mod c_2 mod … mod ...
- 无记录时显示gridview表头,并增加一行显示“没有记录”【绑定SqlDataSource控件时】
原文发布时间为:2008-08-04 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- Codeforces 271D - Good Substrings [字典树]
传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...