【SQL练习】创建表格结构,执行查询语句(DDL和DML查询部分)

本文展示了SQL在数据库操作中的应用,包括查询学生、教师和课程信息,如姓名、性别、班级、成绩等。同时,进行了成绩范围、特定值筛选、分组统计、排序及聚合函数的使用,例如查询最高分、平均分、课程选修人数等。此外,还涉及了子查询和条件组合查询的操作。
摘要由CSDN通过智能技术生成

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aigo-2021

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值