SELECT练习题 SQL强化训练

在这里插入图片描述
建表代码
create table login_info(
uno number(3),
area varchar2(20)
);
insert into login_info values(‘101’, ‘BJ’);
insert into login_info values(‘101’, ‘BJ’);
insert into login_info values(‘102’, ‘SH’);
insert into login_info values(‘102’, ‘BJ’);
insert into login_info values(‘103’, ‘TJ’);
insert into login_info values(‘104’, ‘BJ’);
insert into login_info values(‘104’, ‘TJ’);
insert into login_info values(‘105’, ‘CQ’);
insert into login_info values(‘105’, ‘CQ’);
insert into login_info values(‘105’, ‘CQ’);
commit;

--存在一张银行用户操作日志表,请找出异动信息即用户登录不在一个地区的信息
SELECT * FROM login_info a ,login_info b where a.uno = b.uno and a.area <> b.area; 

--存在一张银行用户操作日志表,请找出异动信息即用户登录不在一个地区的信息
select * from login_info info1, login_info info2 where info1.uno=info2.uno and info1.area<>info2.area;

在这里插入图片描述
create table tb_student(
id number(4) ,
name varchar2(20),
course varchar2(20),
score number(5,2)
);
insert into tb_student values(1,‘张三’,‘语文’,81);
insert into tb_student values(2,‘张三’,‘数学’,75);
insert into tb_student values(3,‘李四’,‘语文’,86);
insert into tb_student values(4,‘李四’,‘数学’,90);
insert into tb_student values(5,‘王五’,‘语文’,81);
insert into tb_student values(6,‘王五’,‘数学’,100);
insert into tb_student values(7,‘王五’,‘英语’,90);
commit;
查询
select * from tb_student;

--使用一条sql语句,查询每门课都大于80分的学生姓名。
select distinct b.name
  from tb_student a,
       (select name, min(score) mi from tb_student group by name) b
 where a.name = b.name
   and mi > 80;

--行转列

select name 姓名 ,sum(case when course='语文' then score end)语文,
sum(case when course='数学' then score end )数学,
sum(case when course='英语' then score end )英语
from tb_student group by name;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

建表语句:
CREATE TABLE COURSE
(
CNO VARCHAR2 (5) NOT NULL,
CNAME VARCHAR2 (15) NOT NULL,
TNO VARCHAR2 (10) NOT NULL,
CONSTRAINT PK_COURSE PRIMARY KEY (CNO)
);

COMMENT ON COLUMN COURSE.CNO IS ‘学科编号’;
COMMENT ON COLUMN COURSE.CNAME IS ‘学科名称’;
COMMENT ON COLUMN COURSE.TNO IS ‘教师编号’;

select * from course;

CREATE TABLE GRADE
(
LOW NUMBER (3) NOT NULL,
UPP NUMBER (3) NOT NULL,
RANK CHAR (1) NOT NULL,
CONSTRAINT PK_GRADE PRIMARY KEY (LOW, UPP)
);

COMMENT ON COLUMN GRADE.LOW IS ‘分数范围下限’;
COMMENT ON COLUMN GRADE.UPP IS ‘分数范围上限’;
COMMENT ON COLUMN GRADE.RANK IS ‘评级’;

CREATE TABLE SCORE
(
SNO VARCHAR2 (3) NOT NULL,
CNO VARCHAR2 (5) NOT NULL,
DEGREE NUMBER (10,1) NOT NULL,
CONSTRAINT PK_SCORE PRIMARY KEY (SNO, CNO)
);

COMMENT ON COLUMN SCORE.SNO IS ‘学生编号’;
COMMENT ON COLUMN SCORE.CNO IS ‘学科编号’;
COMMENT ON COLUMN SCORE.DEGREE IS ‘成绩’;

CREATE TABLE STUDENT
(
SNO VARCHAR2 (3) NOT NULL,
SNAME VARCHAR2 (6) NOT NULL,
SSEX VARCHAR2 (3) NOT NULL,
SBIRTHDAY DATE NOT NULL,
CLASS VARCHAR2 (5) NOT NULL
);

扫描二维码关注公众号,回复: 6766185 查看本文章

COMMENT ON COLUMN STUDENT.SNO IS ‘学生编号’;
COMMENT ON COLUMN STUDENT.SNAME IS ‘学生姓名’;
COMMENT ON COLUMN STUDENT.SSEX IS ‘学生性别’;
COMMENT ON COLUMN STUDENT.SBIRTHDAY IS ‘生日’;
COMMENT ON COLUMN STUDENT.CLASS IS ‘班级’;

CREATE TABLE TEACHER
(
TNO VARCHAR2 (3) NOT NULL,
TNAME VARCHAR2 (6) NOT NULL,
TSEX VARCHAR2 (3) NOT NULL,
TBIRTHDAY DATE NOT NULL,
PROF VARCHAR2 (9) NOT NULL,
DEPART VARCHAR2 (15) NOT NULL,
CONSTRAINT PK_TEACHER PRIMARY KEY (TNO)
);

COMMENT ON COLUMN TEACHER.TNO IS ‘教师编号’;
COMMENT ON COLUMN TEACHER.TNAME IS ‘教师姓名’;
COMMENT ON COLUMN TEACHER.TSEX IS ‘性别’;
COMMENT ON COLUMN TEACHER.TBIRTHDAY IS ‘生日’;
COMMENT ON COLUMN TEACHER.PROF IS ‘职称’;
COMMENT ON COLUMN TEACHER.DEPART IS ‘部门’;

INSERT INTO COURSE (CNO, CNAME, TNO)
VALUES (‘3-105’, ‘计算机导论’, ‘825’);
INSERT INTO COURSE (CNO, CNAME, TNO)
VALUES (‘3-245’, ‘操作系统’, ‘804’);
INSERT INTO COURSE (CNO, CNAME, TNO)
VALUES (‘6-166’, ‘数据电路’, ‘856’);
INSERT INTO COURSE (CNO, CNAME, TNO)
VALUES (‘9-888’, ‘高等数学’, ‘100’);

INSERT INTO GRADE (LOW, UPP, RANK)
VALUES (90, 100, ‘A’);
INSERT INTO GRADE (LOW, UPP, RANK)
VALUES (80, 89, ‘B’);
INSERT INTO GRADE (LOW, UPP, RANK)
VALUES (70, 79, ‘C’);
INSERT INTO GRADE (LOW, UPP, RANK)
VALUES (60, 69, ‘D’);
INSERT INTO GRADE (LOW, UPP, RANK)
VALUES (0, 59, ‘E’);

INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘103’, ‘3-245’, 86);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘105’, ‘3-245’, 75);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘109’, ‘3-245’, 68);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘103’, ‘3-105’, 92);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘105’, ‘3-105’, 88);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘109’, ‘3-105’, 76);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘101’, ‘3-105’, 64);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘107’, ‘3-105’, 91);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘108’, ‘3-105’, 78);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘101’, ‘6-166’, 85);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘107’, ‘6-166’, 79);
INSERT INTO SCORE (SNO, CNO, DEGREE)
VALUES (‘108’, ‘6-166’, 81);

INSERT INTO STUDENT (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
VALUES (‘108’, ‘曾华’, ‘男’, TO_DATE (‘1977-09-01’, ‘YYYY-MM-DD’), ‘95033’);
INSERT INTO STUDENT (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
VALUES (‘105’, ‘匡明’, ‘男’, TO_DATE (‘1975-10-02’, ‘YYYY-MM-DD’), ‘95031’);
INSERT INTO STUDENT (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
VALUES (‘107’, ‘王丽’, ‘女’, TO_DATE (‘1976-01-23’, ‘YYYY-MM-DD’), ‘95033’);
INSERT INTO STUDENT (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
VALUES (‘101’, ‘李军’, ‘男’, TO_DATE (‘1976-02-20’, ‘YYYY-MM-DD’), ‘95033’);
INSERT INTO STUDENT (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
VALUES (‘109’, ‘王芳’, ‘女’, TO_DATE (‘1975-02-10’, ‘YYYY-MM-DD’), ‘95031’);
INSERT INTO STUDENT (SNO, SNAME, SSEX, SBIRTHDAY, CLASS)
VALUES (‘103’, ‘陆君’, ‘男’, TO_DATE (‘1974-06-03’, ‘YYYY-MM-DD’), ‘95031’);

INSERT INTO TEACHER (TNO, TNAME, TSEX, TBIRTHDAY, PROF, DEPART)
VALUES (‘804’, ‘李诚’, ‘男’, TO_DATE (‘1958-12-02’, ‘YYYY-MM-DD’), ‘副教授’, ‘计算机系’);
INSERT INTO TEACHER (TNO, TNAME, TSEX, TBIRTHDAY, PROF, DEPART)
VALUES (‘856’, ‘张旭’, ‘男’, TO_DATE (‘1969-03-12’, ‘YYYY-MM-DD’), ‘讲师’, ‘电子工程系’);
INSERT INTO TEACHER (TNO, TNAME, TSEX, TBIRTHDAY, PROF, DEPART)
VALUES (‘825’, ‘王萍’, ‘女’, TO_DATE (‘1972-05-05’, ‘YYYY-MM-DD’), ‘助教’, ‘计算机系’);
INSERT INTO TEACHER (TNO, TNAME, TSEX, TBIRTHDAY, PROF, DEPART)
VALUES (‘831’, ‘刘冰’, ‘女’, TO_DATE (‘1977-08-14’, ‘YYYY-MM-DD’), ‘助教’, ‘电子工程系’);

–COURSE 学科表
–SCORE 成绩表
–STUDENT 学生表
–GRADE 评级表
–TEACHER 教师表

查询
select * from course;
select * from grade;
select * from score;
select * from student;
select * from teacher;

--1.1 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT sname , ssex ,class from student;
--1.2查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher;
--select depart from teacher group by depart;

--1.3查询Student表的所有记录。
select * from student;
 
--1.4查询Score表中成绩在6080之间的所有记录。
select * from score where degree>=60 and degree<=80;
select * from score where degree between 60 and 80;

--1.5查询Score表中成绩为858688的记录。
select * from score where degree>=85 and degree<=88 and degree!=87;
select * from score where degree in(85 , 86 ,88);

--1.6查询Student表中“95031”班或性别为“女”的同学记录.
select * from Student where CLASS = 95031 or ssex = '女';


--1.7以Class降序查询Student表的所有记录。
select * from Student  order by class desc;

--1.8以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno ,degree desc;

--1.9查询“95031”班的学生人数。
select count(*) from student where class = 95031; 

--1.10查询Score表中的最高分的学生学号和课程号。
select sno,cno  from score,(select max(degree) m from score) where degree =m;

--1.11查询‘3-105’号课程的平均分。
select avg(degree) from score where cno = '3-105';

--1.12查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select cno,avg(degree) from score group by cno having count(1)>=5 and cno like '3%';

select cno, avg(degree) 
  from score
 group by cno
having count(*) >= 5 and cno like '3%'


--1.13查询最低分大于70,最高分小于90的Sno列。
select sno ,degree from score where degree between 70 and 90;

--1.14查询所有学生的Sname、Cno和Degree列。
select sname , cno , degree from score a , student b where a.sno=b.sno;

--1.15查询所有学生的Sno、Cname和Degree列。
select sno , cname , degree from score a , course b where a.cno=b.cno;

--1.16查询所有学生的Sname、Cname和Degree列。
select Sname,Cname,Degree from score a , course b ,student c where a.cno = b.cno and a.sno = c.sno;

--1.17查询“95033”班所选课程的平均分
select sno from student where class = 95033; 
select sno,degree,cno from score sc,(select sno s from student where class = 95033)ss where s=sc.sno;

select avg(degree) from
 (select sno,degree,cno from score sc,(select sno s from student where class = 95033)ss where s=sc.sno)
  group by cno;
  
  
--2.1现查询所有同学的Sno、Cno和rank列。

select Sno,Cno,rank from (select Sno,Cno,degree d from score) s,grade g where d between g.low and g.upp;

--2.2查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select * from student s ,
(select distinct sno from score where degree>(select degree from score where sno=109 and cno='3-105'))sssno 
where sssno.sno=s.sno;

--2.3查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
SELECT sno from score group by sno having count(1)>1;
select *
  from score
 where sno in (SELECT sno from score group by sno having count(1) > 1)
   and degree != (select max(degree) from score) order by sno;

--2.4查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from score where degree > (select degree from score where  cno = '3-105' and sno = 109);

--2.5查询和学号为109的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno ,sname,sbirthday from student where sbirthday like '%75%';
--或
select sno, sname, sbirthday
  from student
 where to_char(sbirthday, 'yyyy') =
       (select to_char(sbirthday, 'yyyy') from student where sno = 109);


--2.6查询“张旭“教师任课的学生成绩。
select tno from teacher where tname ='张旭';
SELECT cno FROM course where tno = (select tno from teacher where tname ='张旭');

select *
  from score
 where cno =
       (SELECT cno
          FROM course
         where tno = (select tno from teacher where tname = '张旭'));

--2.7查询选修某课程的同学人数多于5人的教师姓名。
select cno from score group by cno having count(1)>5;

select tname
  from teacher
 where tno =
       (select tno
          from course
         where cno =
               (select cno from score group by cno having count(1) > 5));
--2.8查询95033班和95031班全体学生的记录
select * from student where class in (95033,95031) order by class;

--2.9查询存在有85分以上成绩的课程Cno.
select distinct cno from score where degree>85;

--2.10查询出“计算机系“教师所教课程的成绩表。
select tno from teacher where depart ='计算机系';
select cno from course where tno in (select tno from teacher where depart ='计算机系');

select *
  from score
 where cno in
       (select cno
          from course
         where tno in (select tno from teacher where depart = '计算机系'));


--2.11查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
select tname , prof from teacher where depart = '计算机系';
select tname , prof from teacher where depart = '电子工程系';

select tname ,prof from teacher
where prof in (((select prof from teacher where depart = '计算机系')union
(select prof from teacher where depart = '电子工程系'))minus
((select prof from teacher where depart = '电子工程系')intersect
(select prof from teacher where depart = '计算机系')));
--union minus intersect 全用 秀!!!

--2.12查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学 的Cno、Sno和Degree,并按Degree从高到低次序排序。
--题目写的莫名其妙
--其实并不对 学3-105的有6人 学3-245的有3人 答案不严谨
select cno, sno, degree
  from score
 where cno = '3-105'
   and degree > (select min(degree) from score where cno = '3-245')
 order by degree desc;
 
 
--3.1查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select Cno,Sno,Degree from score where cno='3-105'
select Cno,Sno,Degree from score where cno='3-245'
select a.cno,a.Sno,a.Degree from (select Cno,Sno,Degree from score where cno='3-105')a,
 (select cno,Sno,Degree from score where cno='3-245')b where a.sno=b.sno and a.degree>b.degree;

--3.2查询所有教师和同学的name、sex和birthday.
select tname ,tsex,tbirthday from teacher
union all
select sname ,ssex ,sbirthday from student;

--3.3查询所有“女”教师和“女”同学的name、sex和birthday.
select tname ,tsex,tbirthday from teacher where tsex='女'
union all 
select sname ,ssex ,sbirthday from student where ssex='女';



--3.4成绩比该课程平均成绩低的同学的成绩表。
select * from score a where  degree <(select avg(degree) from score b group by cno having b.cno=a.cno);
--select degree from score group by cno
--select avg(degree) from score group by cno

--3.5查询所有任课教师的Tname和Depart.
select Tname,Depart from teacher;

--3.6查询所有未讲课的教师的Tname和Depart. 
select tno from teacher minus select tno from course;
select tname,depart from teacher where tno=(select tno from teacher minus select tno from course);

--3.7查询至少有2名男生的班号。
select class from student group by class having count('男')>1;

--3.8查询Student表中不姓“王”的同学记录。
select * from Student
minus
select * from student ss where sname like '王%';

--3.9查询Student表中每个学生的姓名和年龄。
select sname ,2019-to_number(to_char(Sbirthday,'yyyy')) "年龄" from student;

--3.10查询Student表中最大和最小的Sbirthday日期值。
select to_number(to_char(Sbirthday,'yyyy')) "年" ,
to_number(to_char(Sbirthday,'mm')) "月" from student
to_number(to_char(Sbirthday,'dd')) "日" from student;--取出年月日

--震惊
select max(Sbirthday) from student
union
select min(Sbirthday) from student;


--3.11以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from student order by class desc ,Sbirthday asc;

--3.12查询“男”教师及其所上的课程。
select tno from teacher where tsex = '男';
select cname from course where tno in (select tno from teacher where tsex = '男');

--3.13查询最高分同学的Sno、Cno和Degree列。
 
select Sno,Cno,Degree from score where sno in (select sno from score where degree = (select max(degree)from score));

--3.14查询和“李军”同性别的所有同学的Sname.

select sname from student where ssex = (select ssex from student where sname='李军');

--3.15查询和“李军”同性别并同班的同学Sname.
select sname from student where ssex = (select ssex from student where sname='李军')
intersect
select sname from student where class = (select class from student where sname='李军');


--3.16查询所有选修“计算机导论”课程的“男”同学的成绩表

select sno from student where ssex = '男';
select cno from course where cname = '计算机导论';

select * from score where cno=(
select cno from course where cname = '计算机导论') and sno in(
select sno from student where ssex = '男');

猜你喜欢

转载自blog.csdn.net/digua930126/article/details/92997598