创建数据表grade:
CREATE TABLE grade(
id INT NOT NULL,
sex CHAR(1),
firstname VARCHAR(20) NOT NULL,
lastname VARCHAR(20) NOT NULL,
english FLOAT,
math FLOAT,
chinese FLOAT
);
向数据表grade中插入几条数据:
INSERT INTO grade
VALUES (1,'m','John','Smith',88.0,85.0,82.0),
(2,'f','Adam','Smith',76.0,78.0,90.0),
(3,'m','Allen','William',88.0,92.0,95.0),
(4,'m','George','William',62.0,58.0,72.0),
(5,'f','Alice','Davis',89.0,94.0,98.0),
(6,'m','Kevin','Miller',77.0,88.0,99.0),
(7,'f','Helen','Davis',79.0,83.0,91.0),
(8,'m','Andrew','Johnson',81.0,86.0,88.0);
1、查询所有字段
使用select * from grade;可以查询出所有的字段信息:
2、查询grade表中的id,firstname,lastname字段
使用select id, firstname, lastname from grade;可以查询出所有的字段信息:
3、查询grade表中id大于4的学生姓名
使用select * from grade where id>4;可以查询出所有的字段信息:
4、查询grade表中女生的记录
使用select * from grade where sex='f';可以查询出所有的字段信息:
5、查询grade表中id值为2,4,6的学生记录
使用sselect * from grade where id in(2, 4, 6);可以查询出所有的字段信息:
6、查询grade表中math成绩在85到94之间的记录
使用select * from grade where math between 85 and 94;可以查询出所有的字段信息:
7、查询grade表中firstname以A开头的记录
使用select * from grade where firstname like 'A%';可以查询出所有的字段信息:
8、查询grade表中firstname以A开头以e结尾的记录
使用select * from grade where firstname like 'A%e';可以查询出所有的字段信息:
9、查询grade表中firstname包含l的记录
使用select * from grade where firstname like '%l%';可以查询出所有的字段信息:
10、查询grade表中firstname以A开头后面有4个字符的记录
使用select * from grade where firstname like 'A____';可以查询出所有的字段信息:
11、查询grade表中english在80到90之间的记录
使用select * from grade where english between 80 and 90;可以查询出所有的字段信息:
12、查询grade表中math大于90 或者 chinese大于90的记录
使用select * from grade where math>90 or chinese>90;可以查询出所有的字段信息:
13、查询grade表中id不是1、3、5、7的记录
使用select * from grade where id not in(1, 3, 5, 7);可以查询出所有的字段信息:
14、查询grade表中的性别有哪些
使用select sex from grade group by sex;可以查询出所有的字段信息:
15、查询grade表中的lastname有哪几种
使用select lastname from grade group by lastname;可以查询出所有的字段信息:
16、求出表中所有记录的条数
使用select count(*) from grade;可以查询出所有的字段信息:
17、求出表中英语成绩的80的记录的条数
使用select count(*) from grade where english=80;可以查询出所有的字段信息:
18、计算所有学生的数学成绩的和
使用select sum(math) from grade;可以查询出所有的字段信息:
19、计算女生的数学成绩的和
使用select sum(math) from grade where sex='f';可以查询出所有的字段信息:
20、计算英语成绩平均分
使用select avg(english) from grade;可以查询出所有的字段信息:
21、计算男生的英语成绩平均分
使用select avg(english) from grade where sex='m';可以查询出所有的字段信息:
22、求出数学成绩的最高分
使用select max(math) from grade;可以查询出所有的字段信息:
23、求出男生中的数学最高分
使用select max(math) from grade where sex='m';可以查询出所有的字段信息:
24、按照math成绩的升序进行排列
使用select * from grade order by math asc;可以查询出所有的字段信息:
25、按照sex字段的升序和chinese字段的降序排列
使用select * from grade order by sex asc,chinese desc;可以查询出所有的字段信息:
26、查询数据表grade中的记录,按照sex字段进行分组
使用select sex, count(*) from grade group by sex;可以查询出所有的字段信息:
27、将grade表按照lastname字段值分组,并计算每个分组中的学生数
使用select lastname, count(lastname) from grade group by lastname;可以查询出所有的字段信息:
28、对grade表按照lastname字段分组,查出math字段和小于100的组
使用select lastname, sum(math) from grade group by lastname having sum(math)<100;可以查询出所有的字段信息:
29、查询grade表中的第3到第6条记录
使用select * from grade where id between 3 and 6;可以查询出所有的字段信息:
30、查询grade表中男生平均成绩(三科)大于85的记录
使用select *, (math + chinese + english) / 3 as average from grade where sex='m' and (math + chinese + english) / 3>85;可以查询出所有的字段信息: