MySQL 基础查询语句练习题

目录

  • 1、创建表
  • 2、为student表和score表增加记录
  • 3、查询student表的所有记录
  • 4、查询student表的第2条到4条记录
  • 5、从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
  • 6、从student表中查询计算机系和英语系的学生的信息
  • 7、从student表中查询年龄18~22岁的学生信息
  • 8、从student表中查询每个院系有多少人
  • 9、从score表中查询每个科目的最高分
  • 10、查询李四的考试科目(c_name)和考试成绩(grade)
  • 11、用连接的方式查询所有学生的信息和考试信息
  • 12、计算每个学生的总成绩
  • 13、计算每个考试科目的平均成绩
  • 14、查询计算机成绩低于95的学生信息
  • 15、查询同时参加计算机和英语考试的学生的信息
  • 16、将计算机考试成绩按从高到低进行排序
  • 17、从student表和score表中查询出学生的学号,然后合并查询结果
  • 18、查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
  • 19、查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
  • 20、查询student表中学生的学号、姓名、年龄、院系和籍贯并且按照年龄从小到大的顺序排列
  • 21、查询score表中学生的学号、考试科目和成绩并且按照成绩从高到低的顺序排列
  • 22、删除整张score表

1、创建表

(1)创建student表

/* 公众号:AllTests软件测试 */
CREATETABLEstudent (
idINT(10) NOTNULLUNIQUEPRIMARY KEY,
nameVARCHAR(20) NOTNULL,
sex VARCHAR(4),
birth YEAR,
department VARCHAR(20),
address VARCHAR(50)
);


(2)创建score表

/* 公众号:AllTests软件测试 */
CREATETABLEscore (
idINT(10) NOTNULLUNIQUEPRIMARY KEYAUTO_INCREMENT,
stu_id INT(10) NOTNULL,
c_name VARCHAR(20),
grade INT(10)
);

2、为student表和score表增加记录

(1)向student表插入记录的INSERT语句如下

/* 公众号:AllTests软件测试 */
INSERTINTOstudent VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERTINTOstudent VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERTINTOstudent VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERTINTOstudent VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERTINTOstudent VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERTINTOstudent VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');


null

(2)向score表插入记录的INSERT语句如下

/* 公众号:AllTests软件测试 */
INSERTINTOscore VALUES(NULL,901, '计算机',98);
INSERTINTOscore VALUES(NULL,901, '英语', 80);
INSERTINTOscore VALUES(NULL,902, '计算机',65);
INSERTINTOscore VALUES(NULL,902, '中文',88);
INSERTINTOscore VALUES(NULL,903, '中文',95);
INSERTINTOscore VALUES(NULL,904, '计算机',70);
INSERTINTOscore VALUES(NULL,904, '英语',92);
INSERTINTOscore VALUES(NULL,905, '英语',94);
INSERTINTOscore VALUES(NULL,906, '计算机',90);
INSERTINTOscore VALUES(NULL,906, '英语',85);


null

3、查询student表的所有记录

/* 公众号:AllTests软件测试 */
SELECT* FROMstudent;


null

4、查询student表的第2条到4条记录

/* 公众号:AllTests软件测试 */
SELECT* FROMstudent LIMIT1,3;


null

5、从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息

/* 公众号:AllTests软件测试 */
SELECTid, name, department FROMstudent;


null

6、从student表中查询计算机系和英语系的学生的信息

/* 公众号:AllTests软件测试 */
SELECT* FROMstudent WHEREdepartment IN('计算机系','英语系');


null

7、从student表中查询年龄18~22岁的学生信息

/* 公众号:AllTests软件测试 */
SELECTid, name, sex, 2013-birth ASage, department, address
FROMstudent
WHERE2013-birth BETWEEN18AND22;


或者


/* 公众号:AllTests软件测试 */
SELECTid, name, sex, 2013-birth ASage, department, address
FROMstudent
WHERE2013-birth>=18AND2013-birth<=22;


null

8、从student表中查询每个院系有多少人

/* 公众号:AllTests软件测试 */
SELECTdepartment, COUNT(id) FROMstudent GROUPBYdepartment;


null

9、从score表中查询每个科目的最高分

/* 公众号:AllTests软件测试 */
SELECTc_name, MAX(grade) FROMscore GROUPBYc_name;


null

10、查询李四的考试科目(c_name)和考试成绩(grade)

/* 公众号:AllTests软件测试 */
SELECTc_name, grade
FROMscore WHEREstu_id=(SELECTidFROMstudent WHEREname='李四');


null

11、用连接的方式查询所有学生的信息和考试信息

/* 公众号:AllTests软件测试 */
SELECTstudent.id, name, sex, birth, department, address, c_name, grade
FROMstudent, score
WHEREstudent.id=score.stu_id;


null

12、计算每个学生的总成绩

/* 公众号:AllTests软件测试 */
SELECTstudent.id, name, SUM(grade) FROMstudent, score
WHEREstudent.id=score.stu_id
GROUPBYid;


null

13、计算每个考试科目的平均成绩

/* 公众号:AllTests软件测试 */
SELECTc_name, AVG(grade) FROMscore GROUPBYc_name;


null

14、查询计算机成绩低于95的学生信息

/* 公众号:AllTests软件测试 */
SELECT* FROMstudent
WHEREidIN(SELECTstu_id FROMscore WHEREc_name="计算机"andgrade<95);


null

15、查询同时参加计算机和英语考试的学生的信息

/* 公众号:AllTests软件测试 */
SELECT* FROMstudent
WHEREid=ANY(SELECTstu_id FROMscore WHEREstu_id IN(SELECTstu_id FROMscore WHEREc_name='计算机') ANDc_name='英语');


或者


/* 公众号:AllTests软件测试 */
SELECTa.* FROMstudent a, score b, score c
WHEREa.id=b.stu_id ANDb.c_name='计算机'ANDa.id=c.stu_id ANDc.c_name='英语';


null

16、将计算机考试成绩按从高到低进行排序

/* 公众号:AllTests软件测试 */
SELECTstu_id, grade
FROMscore WHEREc_name='计算机'ORDERBYgrade DESC;


null

17、从student表和score表中查询出学生的学号,然后合并查询结果

/* 公众号:AllTests软件测试 */
SELECTidFROMstudent UNIONSELECTstu_id FROMscore;


null

18、查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

/* 公众号:AllTests软件测试 */
SELECTstudent.id, name, sex, birth, department, address, c_name, grade
FROMstudent, score WHERE(nameLIKE'张%'ORnameLIKE'王%') ANDstudent.id=score.stu_id;


null

19、查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

/* 公众号:AllTests软件测试 */
SELECTstudent.id, name, sex, birth, department, address, c_name, grade
FROMstudent, score WHEREaddress LIKE'湖南%'ANDstudent.id=score.stu_id;


null

20、查询student表中学生的学号、姓名、年龄、院系和籍贯并且按照年龄从小到大的顺序排列

/* 公众号:AllTests软件测试 */
selectstudent.id, name, 2017-birth, department, address fromstudent where2017-birth
ORDERBY2017-birth;


null

21、查询score表中学生的学号、考试科目和成绩并且按照成绩从高到低的顺序排列

/* 公众号:AllTests软件测试 */
selectscore.stu_id, c_name, grade fromscore ORDERBYgrade DESC;


null

22、删除整张score表

/* 公众号:AllTests软件测试 */
droptablescore;


精彩推荐

面试笔试系列

思维导图系列

接口Requests系列

测试框架pytest系列

文章为作者独立观点,不代表BOSS直聘立场。未经账号授权,禁止随意转载。