Create table student (
Sno char(15) primary key ,
Sname varchar(10) not null,
Sage tinyint ,
Special varchar(20),
Birth date,
Sex char(2)
);
insert into stu(sno,sname,sage,special,birth,sex) values
('st002','lisi',19,'CS','1999-1-2','M'),
('st003','wangwu',20,'CS','1998-1-2','M'),
('st004','zhaoliu',21,'art','1997-1-2','M'),
('st005','sunqi',22,'math','1996-1-2','F'),
('st006','lisi',35,'en','1983-1-2','F'),
('st007','jia',18,'CS','2000-1-2','M'),
('st008','peter',16,'edu','2002-1-2','F'),
('st009','hanmeimei',19,'art','1999-1-2','F'),
('st0010','poly',8,'CS','2008-1-2','F'),
('st0011','zhangliu',21,'art','1997-7-2','M'),
('st0012','wangliu',27,'art','1991-1-2','M'),
('st0013','liliu',26,'art','1992-1-2','F'),
('st0014','sunliu',20,'art','1998-1-2','F'),
('st0015','lisi',18,'CS','2000-1-2','F'),
('st0016','wangwu',20,'CS','1998-1-2','F'),
('st0017','lisi',17,'CS','2001-1-2','M'),
('st0018','wangwu',26,'CS','1992-1-2','M');
课前复习:
1查询表中学生的学号,姓名,年龄信息。
select sno,sname,sage from student;
2查询edu系所有女生的信息。
select * from student where Special ='edu' and Sex ='f';
3查询所有在1999年以后出生的学生的所有信息。
select * from student where birth>'1999-1-1';
select * from student where year(birth)>1999;
4.查询所有在2000年至2010年出生的所有学生的信息。
select * from student where Birth between '2000-1-1' and '2010-12-31';
select * from student where Birth>='2000-1-1' and Birth <='2010-12-31';
作业:
1.查询所有学生的最大,最小,平均年龄。
2.查询art系学生的最大,最小,平均年龄,。
3.查询各个系学生的最大,最小,平均年龄。
4.查询人数超过5的专业名称及人数。
5.按年龄从小到大查询所有人的信息。
6.按年龄从大到小查询所有人的信息。
7.按学号递减,年龄递增查询所有人的信息。
Create table student1 (
Sno char(15) primary key ,
Sname varchar(10) not null,
Sage tinyint ,
Special varchar(20),
Birth date,
Sex char(2)
);
select * from student1
insert into student1(sno,sname,sage,special,birth,sex) values
('st002','lisi',19,'CS','1999-1-2','M'),
('st003','wangwu',20,'CS','1998-1-2','M'),
('st004','zhaoliu',21,'art','1997-1-2','M'),
('st005','sunqi',22,'math','1996-1-2','F'),
('st006','lisi',35,'en','1983-1-2','F'),
('st007','jia',18,'CS','2000-1-2','M'),
('st008','peter',16,'edu','2002-1-2','F'),
('st009','hanmeimei',19,'art','1999-1-2','F'),
('st010','poly',8,'CS','2008-1-2','F'),
('st011','zhangliu',21,'art','1997-7-2','M'),
('st012','wangliu',27,'art','1991-1-2','M'),
('st013','liliu',26,'art','1992-1-2','F'),
('st014','sunliu',20,'art','1998-1-2','F'),
('st015','lisi',18,'CS','2000-1-2','F'),
('st016','wangwu',20,'CS','1998-1-2','F'),
('st017','lisi',17,'CS','2001-1-2','M'),
('st018','wangwu',26,'CS','1992-1-2','M');
/1.查询所有学生的最大,最小,平均年龄/
select AVG(sage) as AVG, MAX(sage) as MAX,MIN(sage) as MIN from student1;
/2. 查询art系学生的最大,最小,平均年龄/
select MAX(sage) as MAXage,MIN(sage) as MINage,AVG(sage) as AVGage from student1 where Special='art';
/* 3.查询各个系学生的最大,最小,平均年龄。 /
select MAX(sage) as MAXage,MIN(sage) as MINage,AVG(sage) as AVGage from student1 group by Special ;
/4.查询人数超过5的专业名称及人数。/
select Special ,count() as [专业人数]from student1 group by Special HAVING COUNT() > 5;
/5.按年龄从小到大查询所有人的信息。/
select sno,sname,sage,special,birth,sex from student1 order by sage asc;
/6.按年龄从大到小查询所有人的信息。 /
select sno,sname,sage,special,birth,sex from student1 order by sage desc;
/7.按学号递减,年龄递增查询所有人的信息。/
select sno,sname,sage,special,birth,sex from student1 order by sno desc ,Sage asc;
/表名自行修改为了student1,学号应该是三位 将原数据 如st0018改为st018否则按原来的学号无法递减排序*/