SQL 查询语句基本练习

这是一些基本的查询语句练习,所用编辑器为Vscode,配置的是postgreSQL,这些习题是看B站视频的时候写的,比较简单的没有注释语句,加了注释的是一些在入门时感觉稍微麻烦的语句。

首先建立一个test数据库,然后选定test数据库建立4个数据表:student、course、score、teacher。

\l   --查看数据库(MySQL:show databases;)
create database test;  --创建数据库
\c test --选定数据库(MySQL:use test)

 postgreSQL中查看数据库、选定数据库等少量命令与MySQL会有所不同。

-------------------------查询练习
----学生表 Student : 学号 姓名 性别 出生年月日 所在班级
----课程表 Course : 课程号 课程名称 教师编号
----成绩表 Score : 学号 课程号 成绩
----教师表 Teacher : 教师编号 教师名字 教师性别 出生年月日 职称 所在部门
create table student(

    sno varchar(20) primary key,
    sname varchar(20) not null,
    ssex varchar(10) not null,
    sbirthday date,
    class varchar(20)
);
create table teacher(
    tno varchar(20) primary key,
    tname varchar(20) not null,
    tsex varchar(10) not null,
    tbirthday date,
    prof varchar(20) not null,
    depart varchar(20) not null
);
create table course(
    cno varchar(20) primary key,
    cname varchar(20) not null,
    tno varchar(20) not null,
    foreign key(tno) references teacher(tno)
);
create table score(
    sno varchar(20) not null,
    cno varchar(20) not null,
    degree decimal,
    foreign key(sno) references student(sno),
    foreign key(cno) references course(cno)
);
\dt ---查看数据库中的数据表
\d student; ---查看数据表结构
\d course;
\d teacher;
\d score;

由于course表和score表需要连接student 表和teacher表中的外键,所以不能先建course表和score表,必须先创建student 表和teacher表。

创建好数据表后,往各个数据表中增添具体数据:

insert into student values('101','曾华','男','1977-09-01','95033');
insert into student values('102','匡明','男','1975-10-02','95031');
insert into student values('103','王丽','女','1976-01-23','95033');
insert into student values('104','李军','男','1976-02-20','95033');
insert into student values('105','王芳','女','1975-02-10','95031');
insert into student values('106','陆君','男','1974-06-03','95031');
insert into student values('107','王尼玛','男','1976-02-10','95033');
insert into student values('108','张全蛋','男','1975-02-10','95031');
insert into student values('109','赵铁柱','男','1974-06-03','95031');
select * from student;
insert into teacher values('804','李诚','男','1958-12-02','副教授','计算机系');
insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','计算机系');
insert into teacher values('831','刘冰','女','1977-08-14','助教','电子工程系');
select * from teacher;
insert into course values('3-105','计算机导论','825');
insert into course values('3-245','操作系统','804');
insert into course values('6-166','数字电路','856');
insert into course values('9-888','高等数学','831');
select * from course;
insert into score values('103','3-245','86');
insert into score values('105','3-245','75');
insert into score values('109','3-245','68');
insert into score values('103','3-105','92');
insert into score values('105','3-105','88');
insert into score values('109','3-105','76');
insert into score values('103','3-105','64');
insert into score values('105','3-105','91');
insert into score values('109','3-105','78');
insert into score values('103','6-166','85');
insert into score values('105','6-166','79');
insert into score values('109','6-166','81');
select * from score;

下面是查询语句的一些基本练习:

------select练习

select * from student;
select sname,ssex,class from student;
select distinct depart from teacher;   ---distinct:排重
select * from score where degree between 60 and 80;
---或者 select * from score where degree > 60 and degree < 80;
select * from score where degree in (85,86,88);
select * from student where class='95031' or ssex='女';
select * from student order by class desc;   ---降序排列(默认升序asc)
select * from score order by cno asc,degree desc;
select count(*) from student where class='95031';   ----count计数
--select max(degree) from score;
select sno,cno,degree from score where degree=(select max(degree) from score);
select avg(degree) ,cno from score group by cno; --查询每门课的平均成绩, avg:取平均数
---score表中至少有2名学生选修的并以3开头的课程平均数
select avg(degree),cno from score group by cno having count(cno)>=2 and cno like '3%'; 
select sno from score where degree between 70 and 90;
---多表查询同时输出:查询所有学生的sname,cno和degree列
--select sname from student;
--select cno,degree from score;
select sname,cno,degree from student,score where student.sno=score.sno;
select sno,cname,degree from course,score where course.cno=score.cno;
select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno;
---95031班学生每门课的平均分
select cno,avg(degree) from score where sno in(select sno from student where class='95031') group by cno;
---选修3-245课程高于109号同学3-245成绩的所有同学记录
--select degree from score where cno='3-245' and sno='109';
select score from score where cno='3-245' and degree > (select degree from score where cno='3-245' and sno='109');
---查询和学号为108,101的同学同年出生的所有同学的sno,sname,sbirthday
--select year(sbirthday) from student where sno in (108,101) ; ---year:提取年份
select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));
---张旭教师任课的学生成绩
select degree from score where cno in (select cno from course where tno = (select tno from teacher where tname='张旭'));
---查询选修某课程的同学多于5人的教师姓名
select tname from teacher where tno in (select tno from course where cno=(select cno from score group by cno having count(*)>5));
select * from student where class in ('95033','95031');
select cno from score where degree > 85;
---计算机系教师所教课程的成绩表
select * from score where cno in (select cno from course where tno in (select tno from teacher where depart='计算机系'));
---计算机系与电子工程系不同职称教师的tname和prof(不要相同职称的记录) --union的用法
select tname,prof from teacher where depart ='计算机系' and prof not in (select prof from teacher where depart='电子工程系') union select tname,prof from teacher where depart ='电子工程系' and prof not in (select prof from teacher where depart='计算机系');
---选修编号为3-105课程且成绩至少高于选修编号为3-245的同学的cno,sno和degree   --any的用法
select cno,sno,degree from score where cno='3-105' and degree > any (select degree from score where cno='3-105') order by degree desc;
---选修编号为3-105课程且成绩高于选修编号为3-245的同学的cno,sno和degree   --all的用法(不加all会报错)
select cno,sno,degree from score where cno='3-105' and degree > all (select degree from score where cno='3-105') order by degree desc;
---查询所有女老师和女同学的name,sex,birthday (不加as语句会默认列表名称为sname,ssex,sbirthday)
select sname as name,ssex as sex,sbirthday as birthday from student where ssex ='女' union select tname,tsex,tbirthday from teacher where tsex ='女';
---查询所有任课教师的tname,depart(在课表中有课)
select tname,depart from teacher where tno in (select tno from course);
---查询至少有2名男生的班号
select class from student where ssex='男' group by class having count(*)>1;
---student中不姓王的同学记录
select * from student where sname not like '王%';
---查询student中每个学生的姓名和年龄 --年龄=当年年份-出生年份 
select sname,year(now)-year(sbirthday) from student;
select max(sbirthday),min(sbirthday)  from student;
select * from student order by class ,year(now)-year(sbirthday) desc;
select * from course where tno in (select tno from teacher where tsex='男'); 
---查询最高分同学的sno,cno,degree
select sno,cno,degree from score where degree = (select max(degree) from score);
select sname from student where ssex = (select ssex from student where sname='李军');
select sname from student where ssex = (select ssex from student where sname='李军') and class=(select class from student where sname='李军');
select * from score where cno=(select cno from course where cname='计算机导论') and sno in (select sno from student where ssex='男');
select degree from score where sno in (select sno from student where sname like '王%');
select tno from teacher where prof in ('讲师','助教');

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章