這是老男孩的練習題,作一作增長熟練度。python
數據庫文件數據庫
-- MySQL dump 10.13 Distrib 5.7.16, for Win64 (x86_64) -- -- Host: localhost Database: db -- ------------------------------------------------------ -- Server version 5.7.16 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `class` -- DROP TABLE IF EXISTS `class`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `class` ( `cid` int(11) NOT NULL AUTO_INCREMENT, `caption` varchar(32) NOT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `class` -- LOCK TABLES `class` WRITE; /*!40000 ALTER TABLE `class` DISABLE KEYS */; INSERT INTO `class` VALUES (1,'三年二班'),(2,'三年三班'),(3,'一年二班'),(4,'二年九班'); /*!40000 ALTER TABLE `class` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `course` -- DROP TABLE IF EXISTS `course`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `course` ( `cid` int(11) NOT NULL AUTO_INCREMENT, `cname` varchar(32) NOT NULL, `teacher_id` int(11) NOT NULL, PRIMARY KEY (`cid`), KEY `fk_course_teacher` (`teacher_id`), CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `course` -- LOCK TABLES `course` WRITE; /*!40000 ALTER TABLE `course` DISABLE KEYS */; INSERT INTO `course` VALUES (1,'生物',1),(2,'物理',2),(3,'體育',3),(4,'美術',2); /*!40000 ALTER TABLE `course` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `score` -- DROP TABLE IF EXISTS `score`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `score` ( `sid` int(11) NOT NULL AUTO_INCREMENT, `student_id` int(11) NOT NULL, `course_id` int(11) NOT NULL, `num` int(11) NOT NULL, PRIMARY KEY (`sid`), KEY `fk_score_student` (`student_id`), KEY `fk_score_course` (`course_id`), CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`), CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`) ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `score` -- LOCK TABLES `score` WRITE; /*!40000 ALTER TABLE `score` DISABLE KEYS */; INSERT INTO `score` VALUES (1,1,1,10),(2,1,2,9),(5,1,4,66),(6,2,1,8),(8,2,3,68),(9,2,4,99),(10,3,1,77),(11,3,2,66),(12,3,3,87),(13,3,4,99),(14,4,1,79),(15,4,2,11),(16,4,3,67),(17,4,4,100),(18,5,1,79),(19,5,2,11),(20,5,3,67),(21,5,4,100),(22,6,1,9),(23,6,2,100),(24,6,3,67),(25,6,4,100),(26,7,1,9),(27,7,2,100),(28,7,3,67),(29,7,4,88),(30,8,1,9),(31,8,2,100),(32,8,3,67),(33,8,4,88),(34,9,1,91),(35,9,2,88),(36,9,3,67),(37,9,4,22),(38,10,1,90),(39,10,2,77),(40,10,3,43),(41,10,4,87),(42,11,1,90),(43,11,2,77),(44,11,3,43),(45,11,4,87),(46,12,1,90),(47,12,2,77),(48,12,3,43),(49,12,4,87),(52,13,3,87); /*!40000 ALTER TABLE `score` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `student` -- DROP TABLE IF EXISTS `student`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `student` ( `sid` int(11) NOT NULL AUTO_INCREMENT, `gender` char(1) NOT NULL, `class_id` int(11) NOT NULL, `sname` varchar(32) NOT NULL, PRIMARY KEY (`sid`), KEY `fk_class` (`class_id`), CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `student` -- LOCK TABLES `student` WRITE; /*!40000 ALTER TABLE `student` DISABLE KEYS */; INSERT INTO `student` VALUES (1,'男',1,'理解'),(2,'女',1,'鋼蛋'),(3,'男',1,'張三'),(4,'男',1,'張一'),(5,'女',1,'張二'),(6,'男',1,'張四'),(7,'女',2,'鐵錘'),(8,'男',2,'李三'),(9,'男',2,'李一'),(10,'女',2,'李二'),(11,'男',2,'李四'),(12,'女',3,'如花'),(13,'男',3,'劉三'),(14,'男',3,'劉一'),(15,'女',3,'劉二'),(16,'男',3,'劉四'); /*!40000 ALTER TABLE `student` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `teacher` -- DROP TABLE IF EXISTS `teacher`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `teacher` ( `tid` int(11) NOT NULL AUTO_INCREMENT, `tname` varchar(32) NOT NULL, PRIMARY KEY (`tid`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `teacher` -- LOCK TABLES `teacher` WRITE; /*!40000 ALTER TABLE `teacher` DISABLE KEYS */; INSERT INTO `teacher` VALUES (1,'張磊老師'),(2,'李平老師'),(3,'劉海燕老師'),(4,'朱雲海老師'),(5,'李傑老師'); /*!40000 ALTER TABLE `teacher` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2019-05-15 20:47:52
1.查詢學過「葉平」老師所教的全部課的同窗的學號、姓名;學習
select student.sname,B.student_id from (select student_id from score where course_id in (select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname="李平老師") group by student_id having count(student_id)=(select count(cid) from course left join teacher on course.teacher_id = teacher.tid where teacher.tname="李平老師"))as B left join student on student.sid=B.student_id;
2.查詢學過「001」而且也學過編號「002」課程的同窗的學號、姓名;blog
select student_id,sname from (select student_id,course_id from score where course_id = 1 or course_id = 2) as B left join student on B.student_id = student.sid group by student_id HAVING count(student_id) > 1
3.查詢沒有學全全部課的同窗的學號、姓名;ci
select student.sname,B.student_id from(select student_id,count(sid) from score group by student_id having count(sid)< (select count(cid) from course)) as B left join student on student.sid=B.student_id;
4.查詢有課程成績小於60分的同窗的學號、姓名;io
查詢至少有一門課與學號爲「001」的同窗所學相同的同窗的學號和姓名;table
查詢至少學過學號爲「001」同窗全部課的其餘同窗學號和姓名;class
查詢和「002」號的同窗學習的課程徹底相同的其餘同窗學號和姓名;cli
select student_id from score where student_id in (select student_id from score where student_id !=1 group by student_id having count(course_id)=(select count(course_id) from score where student_id =1)) and course_id in (select course_id from score where student_id =1) group by student_id having count(course_id)=(select count(course_id) from score where student_id =1);
按各科平均成績從低到高和及格率的百分數從高到低順序select
查詢沒學過「葉平」老師講授的任一門課程的學生姓名;
查詢所有學生都選修的課程的課程號和課程名;
檢索至少選修兩門課程的學生學號
查詢每門課程成績最好的前兩名;
思路:新建一個虛擬表,表中有sid列,course_id 列以及對應的course_id的課程成績第一名和第二名,而後將表與score表連起來,查詢score表中num大於等於第二名的行
查詢選修「劉海燕」老師所授課程的學生中,成績最高的學生姓名及其成績;
查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名;
查詢平均成績大於85的全部學生的學號、姓名和平均成績;
查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
查詢出只選修了一門課程的所有學生的學號和姓名;
課程平均分從高到低顯示(顯示任課老師)