数据库小练习2:

新建一张exam表,字段数据如下图:

 

CREATE TABLE exam(
id INT,
NAME VARCHAR(10),
english VARCHAR(10),
chinese VARCHAR(10),
math VARCHAR(10)
)

INSERT INTO exam (id,NAME,english,chinese,math) VALUES (1,'张三',85,74,91);
INSERT INTO exam (id,NAME,english,chinese,math) VALUES (2,'李四',98,90,83);
INSERT INTO exam (id,NAME,english,chinese,math) VALUES (3,'王五',85,84,59);
INSERT INTO exam (id,NAME,english,chinese,math) VALUES (4,'赵六',75,79,76);
INSERT INTO exam (id,NAME,english,chinese,math) VALUES (5,'田七',69,63,98);
INSERT INTO exam (id,NAME,english,chinese,math) VALUES (6,'李老八',89,90,83);

 

1.查询所有学生考试成绩信息

SELECT * FROM exam;

2.查询所有学生的姓名和英语成绩

 SELECT NAME,english FROM exam;

3.查询英语成绩信息(不显示重复的值)

 SELECT DISTINCT english FROM exam;

4.查看学生姓名和学生的总成绩

 SELECT NAME,english+chinese+math FROM exam;

5.查询学生的姓名和平均分,平均分用avg别名展示

 SELECT NAME,(english+chinese+math)/3 AS AVG FROM exam;

6.查询李四学生的成绩:

 SELECT * FROM exam WHERE NAME = '李四';

7.查询名称叫李四学生并且英文大于90分

 SELECT * FROM exam WHERE NAME = '李四' AND english > 90;

8.查询姓李的学生的信息

 SELECT * FROM exam WHERE NAME LIKE '李%';

9.查询英语成绩是69,75,89学生的信息

 SELECT * FROM exam WHERE english IN (69,75,89);    (重点记忆:in固定的用法)

10.查询数学成绩在80-90之间的学生信息

 SELECT * FROM exam WHERE math>=80 AND math<=90;

11.只要有一门不及格,就找出来

SELECT * FROM exam WHERE english<60 OR chinese<60 OR math<60;

12.查询学生信息,并且按照语文成绩进行排序:

 SELECT * FROM exam ORDER BY chinese ASC;

13.查询学生信息,并且按照语文成绩倒序排序:

 SELECT * FROM exam ORDER BY chinese DESC;

14.查询学生信息,先按照语文成绩进行倒序排序,如果成绩相同再按照英语成绩升序排序

 SELECT * FROM exam ORDER BY chinese ASC,english DESC;

15.查询姓李的学生的信息,按照英语成绩降序排序

 SELECT * FROM exam WHERE NAME LIKE '李%' ORDER BY english DESC;

16.查询学生信息,按照总成绩排序,只展示学生的姓名和总分(SUM)

 SELECT NAME,english+math+chinese AS '总分' FROM exam ORDER BY '总分' ASC;

17.获取所有学生的英语成绩的总和:

 SELECT SUM(english) FROM exam ;

18.获取所有学生的英语成绩和数学成绩总和:

 SELECT SUM(english),SUM(math) FROM exam;

19.查询姓李的学生的英语成绩的总和

 SELECT SUM(english) FROM exam WHERE NAME LIKE '李%';

20.查询所有学生各科的总成绩:

 SELECT SUM(english),SUM(chinese),SUM(math) FROM exam;

21.获得姓李的学生的个数

 SELECT COUNT(*) FROM exam WHERE NAME LIKE '李%';

22.获得数学成绩的最高分:

 SELECT MAX(math) AS '最高分' FROM exam ;

23.获得语文成绩的最小值

 SELECT MIN(chinese) AS '最小值' FROM exam;

24.获取语文成绩的平均值

SELECT AVG(chinese) AS '平均值' FROM exam;

posted on 2022-04-10 15:59  博塬  阅读(240)  评论(0编辑  收藏  举报