面試中常遇的數據庫筆試題及解答方法和思路

/*
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));

 

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