本文主要是介绍【SQL练习】创建表格结构,执行查询语句(DDL和DML查询部分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.创建以下表结构:
2.插入如下记录:
3.执行如下查询:
1.查询Student表中的所有记录的SNAME,SSEX和CLASS列
2.查询教师所有的单位即不重复的DEPART列
3.查询Student表的所有记录
4.查询Score表中的成绩在60-80之间的所有记录
5.查询Score表中的成绩为85,86,88的记录
6.查询Student表中的95031班或性别为女的同学记录
7.以Class降序查询Student表的所有记录
8.以CNO升序DEGREE降序查询Score表的所有记录
9.查询95031班的学生人数
10.查询Score表中的最高分的学生学号和课程号以及成绩
11.查询3-105号课程的平均分
12.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数
13.查询最低分大于70,最高分小于90的SNO列
14.查询Student表中不姓王的同学记录
15.查询Student表中每个学生的姓名和年龄 1
6.查询Student表中的最大和最小的SBIRTH日期值
17.以班号和年盼到从大到小的顺序查询Student表中的全部记录
18.查询所有学生的SNAME,CNO和DEGREE列
19.查询所有学生的SNO,CNAME和DEGREE列
20.查询所有学生的SNAME,CNAME和DEGREE列
drop table student;
drop table teacher;
drop table course;
drop table score;
create table student(sno varchar(3),sname varchar(4),ssex varchar(2),sbirth date,classes varchar(5)
);
create table teacher(tno varchar(3),tname varchar(10),tsex varchar(2),tbirth date,prof varchar(6),-- 职称depart varchar(20) -- 院系
);
create table course(cno varchar(5),cname varchar(10),tno varchar(3));
create table score(sno varchar(3),cno varchar(5),degree int(5)
);
insert into student
values
('108','曾化','男','1977/9/1','95033'),
('105','匡明','男','1975/10/2','95031'),
('107','王丽','女','1977/1/23','95033'),
('109','王芳','女','1977/2/10','95031'),
('103','李军','男','1977/6/3','95031'),
('101','张飞','男','1977/6/3','95031');
insert into teacher
values
('804','李成','男','1958/12/2','副教授','计算机系'),
('856','张九','男','1969/3/12','讲师','电子工程系'),
('825','王萍','女','19720505','助教','计算机系'),
('832','刘冰','女','1977/8/11','助教','电子工程系');
insert into course
values
('3-105','计算机导论','825'),
('3-245','操作系统','804'),
('6-166','数字电路','856'),
('9-888','高等数学','832');
insert into score
values
('103','3-245',86),
('105','3-245',75),
('109','3-245',68),
('103','3-105',92),
('105','3-105',88),
('109','3-105',76),
('101','3-105',64),
('107','3-105',91),
('108','3-105',78),
('101','6-166',85);-- 1.查询Student表中的所有记录的SNAME,SSEX和CLASS列
select sname,ssex,classes from student;
-- 2.查询教师所有的单位即不重复的DEPART列
select distinct depart from teacher;
-- 3.查询Student表的所有记录
select * from student;
-- 4.查询Score表中的成绩在60-80之间的所有记录
select * from score where degree>60 and degree<80;
-- 5.查询Score表中的成绩为85,86,88的记录
select * from score where degree=85 or degree=86 or degree=88;
-- 6.查询Student表中的95031班或性别为女的同学记录
select * from student where classes='95031' or ssex='女';
-- 7.以Class降序查询Student表的所有记录
select * from student order by classes desc;
-- 8.以CNO升序DEGREE降序查询Score表的所有记录
select * from score order by cno,degree desc;
-- 9.查询95031班的学生人数
select count(0) from student where classes='95031';
-- 10.查询Score表中的最高分的学生学号和课程号以及成绩
select sno,cno,degree,max(degree) from score;
-- 11.查询3-105号课程的平均分
select avg(degree) from score where cno='3-105';
-- 12.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数
select avg(degree) from score
where cno in (select cno from score group by cno having count(sno)>=5) and cno like '3%';
-- 13.查询最低分大于70,最高分小于90的SNO列
select sno from score group by sno having min(degree)>70 and max(degree)<90 ;
-- 14.查询Student表中不姓王的同学记录
select * from student where sname not like '王%';
-- 15.查询Student表中每个学生的姓名和年龄
select sname,datediff(current_date,sbirth)/365 as age from student;
-- 16.查询Student表中的最大和最小的SBIRTH日期值
select max(sbirth) as min,min(sbirth) as max from student;
-- 17.以班号和年盼到从大到小的顺序查询Student表中的全部记录
select * from student order by sbirth,classes desc;
-- 18.查询所有学生的SNAME,CNO和DEGREE列
select sname,cno,degree from student s,score c where s.sno=c.sno;
-- 19.查询所有学生的SNO,CNAME和DEGREE列
select s.sno,c.cname,degree from student s,course c,score o where s.sno=o.sno and c.cno=o.cno;
-- 20.查询所有学生的SNAME,CNAME和DEGREE列
select s.sname,cname,degree from student s,course c,score o where s.sno=o.sno and c.cno=o.cno;
这篇关于【SQL练习】创建表格结构,执行查询语句(DDL和DML查询部分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!