MySQL查询语句练习

 
 

-- MySQL dump 10.13 Distrib 8.0.13, for Win64 (x86_64)
--
-- Host: localhost Database: school
-- ------------------------------------------------------
-- Server version 8.0.13

 
 

/*!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 */;
SET NAMES utf8mb4 ;
/*!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 */;
SET character_set_client = utf8mb4 ;
CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=22 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,'一年一班'),(3,'一年三班'),(2,'一年二班'),(16,'七年一班'),(17,'七年二班'),(6,'三年一班'),(8,'三年三班'),(7,'三年二班'),(19,'九年一班'),(4,'二年一班'),(5,'二年二班'),(20,'二年十一班'),(12,'五年一班'),(13,'五年二班'),(18,'八年一班'),(14,'六年一班'),(15,'六年二班'),(9,'四年一班'),(11,'四年三班'),(10,'四年二班');
/*!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 */;
SET character_set_client = utf8mb4 ;
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=9 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,'物理',4),(5,'化学',5),(6,'政治',6),(7,'地理',4),(8,'历史',3);
/*!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 */;
SET character_set_client = utf8mb4 ;
CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`number` float NOT NULL DEFAULT '0',
`exam_date` date DEFAULT NULL,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 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,13,'2001-10-11',1,1),(2,14,'2002-10-11',2,2),(3,80,'2001-10-11',3,3),(4,13,'2001-10-11',4,4),(5,13,'2001-10-11',5,5),(6,13,'2001-10-11',6,6),(7,90,'2001-10-11',7,7),(8,50,'2001-10-11',8,8),(9,18,'2001-10-11',9,8),(10,49,'2001-10-11',10,7),(11,13,'2001-10-11',11,6),(12,100,'2013-09-10',8,6);
/*!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 */;
SET character_set_client = utf8mb4 ;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`age` tinyint(3) NOT NULL,
`sex` char(1) DEFAULT '女',
`birthday` date DEFAULT NULL,
`tel` char(11) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`class_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 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,'王三',13,'男','2001-10-11','15012341234','西安市',1),(2,'王二狗',14,'女','2002-10-11','15012341234','西安市',3),(3,'李铁蛋儿',12,'男','2001-10-11','15012341234','西安市',5),(4,'王二麻子',13,'女','2001-10-11','15012341234','西安市',16),(5,'张铁柱',13,'女','2001-10-11','15012341234','西安市',12),(6,'孙铁锤',13,'女','2001-10-11','15012341234','西安市',13),(7,'张四',23,'男','2001-10-11','15012341234','西安市',2),(8,'张五',13,'女','2001-10-11','15012341234','西安市',1),(9,'张六',18,'女','2001-10-11','15012341234','西安市',7),(10,'张七',13,'男','2001-10-11','15012341234','西安市',4),(11,'张八',13,'男','2001-10-11','15012341234','西安市',5);
/*!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 */;
SET character_set_client = utf8mb4 ;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`sex` char(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 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,'孙老师','男'),(6,'周老师','女'),(7,'张老师','女');
/*!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-02-25 15:32:25


上面是导出的school数据库,导入即可练习

1
1、查询Student表中的所有记录的sname、gender和class_id列。 2 2、查询教师所有的姓名即不重复的tname列。 3 select distinct name from teacher; 4 select name from teacher group by name; 5 3、 查询Student表的所有记录。 6 4、 查询Score表中成绩在60到80之间的所有记录。 7 select * from score where number between 60 and 80; 8 select * from score where number>=60 and number <=80; 9 5、 查询Score表中成绩为86或88的记录。 10 select * from score where number in (86,88); 11 select * from score where number=86 or number=88; 12 6、 查询Student表中性别为“女”的同学记录。 13 select * from student where sex=''; 14 7、 以age降序查询Student表的所有记录。 15 select * from student order by age desc; 16 8、 以Cid升序、sid降序查询Score表的所有记录。 17 select * from student order by class_id asc,id desc; #class_id升序,如果遇到相同的class_id,则比较他们的id,降序 18 9、 查询“一年级一班”班的学生人数。 19 select count(id) as 人数 from student where class_id=(select id from class where name='一年一班'); 20 select count(student.id) as 人数 from student inner join class on student.class_id=class.id where class.name='一年一班'; 21 select count(stu.id) as 人数 from student as stu inner join class as cls on stu.class_id=cls.id where cls.name='一年一班'; 22 10、查询Score表中的最高分的学生学号和课程号。 23 select number,student_id,course_id from score where number in (select max(number) from score); 24 11、查询‘3’号课程的平均分。 25 select avg(number) from score where course_id=3; 26 12、查询Score表中至少有5名学生选修的课程id。 27 select course_id, count(student_id) as n from score group by course_id having n>5; 28 select course_id from score group by course_id having count(student_id)>5; 29 13、查询最低分大于70,最高分小于90的student_id列。 30 select student_id from score group by student_id having max(number)<90 and min(number)>70; 31 14、查询“一年级一班”所选课程的平均分。 32 select avg(number) from score where student_id in (select id from student where class_id=(select id from class where name='一年一班')); 33 select avg(number) from score inner join student on score.student_id=student.id inner join class on class.id=student.class_id where class.name='一年一班'; 34 15、查询选修“2”课程的成绩高于“10”号同学成绩的所有同学的记录。 35 select * from student where id in (select student_id from score where course_id=2 and number>(select number from score where course_id=2 and student_id=10)); 36 select * from student inner join score on student.id=score.student_id where course_id=2 and number>(select number from score where course_id=2 and student_id=10); 37 17、查询成绩高于学号为“5”最高分、课程号为“2”的成绩的所有记录。 38 select number from score where course_id=2 and number>(select max(number) from score where student_id=5); 39 18、查询和学号为6号同学相同年龄的所有学生的id、name和age列。 40 select id,name,age from student where age=(select age from student where id=6); 41 19、查询“马杰“教师任课的学生成绩。 42 select number,student_id from score where course_id in (select id from course where teacher_id=(select id from teacher where name='马杰')); 43 20、查询选修某课程的同学人数多于5人的教师姓名。 44 select name from teacher where id in (select teacher_id from course where id in (select course_id from score group by course_id having count(student_id)>5)); 45 21、查询一年级二班和一年级一班全体学生的记录。 46 select * from student where class_id in (select id from class where name in ('一年二班','一年一班')); 47 22、查询存在有85分以上成绩的课程. 48 select name from course where id in (select course_id from score where number>60); 49 23、查询出“数学“教师所教课程的成绩表。 50 select number from score where course_id in (select id from course where teacher_id=(select teacher_id from course where name='数学')); 51 24、查询所有教师和同学的所有信息. 52 select * from student inner join score on score.student_id=student.id inner join course on course.id=score.course_id inner join teacher on teacher.id=course.teacher_id; 53 25、查询所有“女”教师和“女”同学的name、gender. 54 55 26、查询学生平均成绩比总平均成绩低的同学的学生名。 56 select student.name from student where id in (select student_id from score group by student_id having avg(number)<(select avg(number) from score)); 57 27、查询所有任课教师的Tname. 58 select teacher.id,teacher.name from teacher where id in (select teacher_id from course); 59 28 查询所有未讲课的教师的Tname. 60 select teacher.id,teacher.name from teacher where id not in (select teacher_id from course); 61 29、查询至少有2名男生的班号。************************************************ 62 select class_id from student where sex='' group by class_id having count(id)>1 ; 63 30、查询Student表中不姓“王”的同学记录。 64 select * from student where name not like '王%'; 65 31、查询Student表中最大和最小的年龄。 66 select max(age),min(age) from student; 67 32、以班号和年龄从大到小的顺序查询Student表中的全部记录。 68 select * from student order by class_id desc,age desc; 69 34、查询最高分同学的student_id、class_id和成绩列。 70 select student_id,class_id,number from score inner join student on student.id=score.student_id where number=(select max(number) from score); 71 35、查询和“李军”同性别的所有同学的Sname. 72 select name from student where sex=(select sex from student where name='李军'); 73 36、查询和“李军”同性别并同班的同学Sname. 74 select name from student where sex=(select sex from student where name='张五') and class_id=(select class_id from student where name='张五'); 75 37、查询所有选修“语文”课程的“男”同学的成绩表 76 select number from score where student_id in (select id from student where sex='') and course_id in (select id from course where course.name='语文');

猜你喜欢

转载自www.cnblogs.com/zpdbkshangshanluoshuo/p/10431203.html