面试中常遇的数据库笔试题及解答方法和思路

/*
Navicat MySQL Data Transfer

Source Server         : msd-local
Source Server Version : 50619
Source Host           : 192.168.1.180:3306
Source Database       : mytest

Target Server Type    : MYSQL
Target Server Version : 50619
File Encoding         : 65001

Date: 2019-09-09 17:51:21
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for books
-- ----------------------------
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `author` varchar(20) DEFAULT NULL,
  `price` decimal(11,0) DEFAULT NULL,
  `quantity` int(11) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of books
-- ----------------------------
INSERT INTO `books` VALUES ('1', '水浒', '施耐庵', '188', '3');
INSERT INTO `books` VALUES ('2', '计算机网络', '谢希仁', '49', '3');
INSERT INTO `books` VALUES ('3', '计算方法', '严蔚敏', '58', '3');
INSERT INTO `books` VALUES ('4', '计算方法习题集', '殷人昆', '188', '3');
INSERT INTO `books` VALUES ('5', '数据库技术及应用', '王珊', '38', '3');
INSERT INTO `books` VALUES ('6', '组合数学', '周伟', '28', '3');
INSERT INTO `books` VALUES ('7', 'Redis初探', '周旭龙', '25', '3');

-- ----------------------------
-- Table structure for borrow
-- ----------------------------
DROP TABLE IF EXISTS `borrow`;
CREATE TABLE `borrow` (
  `pk` int(11) NOT NULL AUTO_INCREMENT,
  `cardId` int(11) DEFAULT NULL,
  `bookId` int(11) DEFAULT NULL,
  `returnDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`pk`),
  UNIQUE KEY `pk` (`pk`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of borrow
-- ----------------------------
INSERT INTO `borrow` VALUES ('4', '1', '1', '2017-02-16 00:00:00');
INSERT INTO `borrow` VALUES ('5', '2', '1', '2017-02-16 00:00:00');
INSERT INTO `borrow` VALUES ('6', '3', '1', '2016-12-14 11:00:44');
INSERT INTO `borrow` VALUES ('7', '4', '3', '2016-12-14 11:00:48');
INSERT INTO `borrow` VALUES ('8', '4', '6', '2016-12-14 11:00:53');
INSERT INTO `borrow` VALUES ('9', '5', '6', '2016-12-14 11:00:57');
INSERT INTO `borrow` VALUES ('10', '7', '7', '2016-12-14 11:01:01');

-- ----------------------------
-- Table structure for cards
-- ----------------------------
DROP TABLE IF EXISTS `cards`;
CREATE TABLE `cards` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `class` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of cards
-- ----------------------------
INSERT INTO `cards` VALUES ('1', '张三', '计科一班');
INSERT INTO `cards` VALUES ('2', '李四', '计科一班');
INSERT INTO `cards` VALUES ('3', '王五', '计科二班');
INSERT INTO `cards` VALUES ('4', '六四', '计科二班');
INSERT INTO `cards` VALUES ('5', '七七', '软工一班');
INSERT INTO `cards` VALUES ('6', '粑粑', '软工二班');

-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `courseNo` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `teacherNo` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('1', '语文', '1');
INSERT INTO `course` VALUES ('2', '数学', '2');
INSERT INTO `course` VALUES ('3', '英语', '3');
INSERT INTO `course` VALUES ('4', '物理', '4');

-- ----------------------------
-- Table structure for fruits
-- ----------------------------
DROP TABLE IF EXISTS `fruits`;
CREATE TABLE `fruits` (
  `type` varchar(20) DEFAULT NULL,
  `variety` varchar(20) DEFAULT NULL,
  `price` double(15,3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of fruits
-- ----------------------------
INSERT INTO `fruits` VALUES ('apple', 'gala', '2.790');
INSERT INTO `fruits` VALUES ('apple', 'fuji', '0.240');
INSERT INTO `fruits` VALUES ('apple', 'limbertwig', '2.870');
INSERT INTO `fruits` VALUES ('orange', 'valencia', '3.590');
INSERT INTO `fruits` VALUES ('orange', 'navel', '9.360');
INSERT INTO `fruits` VALUES ('pear', 'bradford', '6.050');
INSERT INTO `fruits` VALUES ('pear', 'bartlett', '2.140');
INSERT INTO `fruits` VALUES ('cherry', 'bing', '2.550');
INSERT INTO `fruits` VALUES ('cherry', 'chelan', '6.330');

-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
  `StudentNo` int(11) DEFAULT NULL,
  `CourseNo` int(11) DEFAULT NULL,
  `score` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES ('1', '2', '78');
INSERT INTO `score` VALUES ('1', '3', '67');
INSERT INTO `score` VALUES ('1', '4', '67');
INSERT INTO `score` VALUES ('2', '1', '52');
INSERT INTO `score` VALUES ('2', '2', '81');
INSERT INTO `score` VALUES ('2', '3', '92');
INSERT INTO `score` VALUES ('2', '4', '67');
INSERT INTO `score` VALUES ('3', '1', '52');
INSERT INTO `score` VALUES ('3', '2', '47');
INSERT INTO `score` VALUES ('3', '3', '88');
INSERT INTO `score` VALUES ('3', '4', '67');
INSERT INTO `score` VALUES ('4', '2', '88');
INSERT INTO `score` VALUES ('4', '3', '90');
INSERT INTO `score` VALUES ('4', '4', '67');
INSERT INTO `score` VALUES ('5', '1', '52');
INSERT INTO `score` VALUES ('5', '3', '78');
INSERT INTO `score` VALUES ('5', '4', '67');
INSERT INTO `score` VALUES ('6', '1', '52');
INSERT INTO `score` VALUES ('6', '2', '68');
INSERT INTO `score` VALUES ('6', '4', '67');
INSERT INTO `score` VALUES ('1', '1', '52');
INSERT INTO `score` VALUES ('5', '2', '72');
INSERT INTO `score` VALUES ('7', '2', '72');

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `name` varchar(32) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` varchar(8) DEFAULT NULL,
  `studentNo` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('钱二', '19', '女', '2');
INSERT INTO `student` VALUES ('刘一', '18', '男', '1');
INSERT INTO `student` VALUES ('张三', '17', '男', '3');
INSERT INTO `student` VALUES ('李四', '18', '女', '4');
INSERT INTO `student` VALUES ('王五', '17', '男', '5');
INSERT INTO `student` VALUES ('赵6', '19', '女', '6');
INSERT INTO `student` VALUES ('钱二', '25', '男', '7');

-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `teacherNo` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('1', '叶平');
INSERT INTO `teacher` VALUES ('2', '贺高');
INSERT INTO `teacher` VALUES ('3', '杨艳');
INSERT INTO `teacher` VALUES ('4', '叶平');

以上是表和数据,直接拷贝去连接工具运行即可

以下是题目:


-- 查询课程编号为“001”的课程比“002”的课程成绩高的所有学生的学号 
##(解答:将查询出来的两份表数据分组,在对比)
select x.StudentNo from score x,score y where x.StudentNo=y.StudentNo and x.CourseNo=001 and y.CourseNO=002 and x.score>y.score group by x.StudentNo

-- 查询平均成绩大于60分的学生的学号和平均成绩
##(解答:先分组,在用having添加条件)
select StudentNo,CourseNo,avg(score) from score group by StudentNo having  AVG(score)>60;

-- 查询所有学生的学号、姓名、选课数、总成绩
##(解答:找出中间表成绩表,包含课程编号与学生编号,将学生表连接通过学生分组查询出平均成绩等)
select x.studentNo,y.name,count(x.CourseNo),sum(x.score) from score x,student y where x.studentNo=y.studentNo  group by x.studentNo;

-- 查询姓“叶”的老师的个数
select count(*) from teacher where name like '叶%';
 
-- 查询没学过“叶平”老师课的学生的学号、姓名
## (解答:先找出叶平老师的编号,在通过成绩表找到那些学生学习过叶平老师的课程,在拿到这些学生的studentNo 使用not in 就可以拿到)
select studentNo,name from Student where studentNo not in(select StudentNo from score where courseNo in(select teacherNo from teacher where name='叶平'));

-- 查询学过“叶平”老师所教的所有课的同学的学号、姓名
select studentNo,name from student where studentNo in (select studentNo from score where courseNo in(select teacherNo from teacher where name='叶平'));

-- 查询学过编号为“001”的课程并且也学过编号为“002”的课程的学生的学号、姓名
select studentNo,name from student where studentNo in (select studentNo from score where courseNo in(select courseNo from course where courseNo=001 or courseNo=002));

-- 查询课程编号为“002”的总成绩
select sum(score) from score where courseNo=002;

-- 查询所有课程成绩小于60分的学生的学号、姓名
## (解答:先查询所有成绩小于60分的数据,这些数据都是<60 ,然后看看成绩表那些符合 用 in 包含小于60的学生编号)
select studentNo,name from student where studentNo in (select studentNo from score where score<60);

-- 查询没有学全所有课的学生的学号、姓名
## (解答:这里不要陷入误区,不要用课程分类,直接查询一共有多少课程,在用学生分组查询已学了几种课程,如果学生已学课程 < 用总的课程数,那么拿出这个学生编号便可)
select studentNo,name from student where studentNo in(select studentNo from score group by studentNo HAVING count(courseNo)<(select count(*) from course));

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章