oracle查询练习题(从易到难)

本文通过具体的例子介绍了使用SQL进行数据查询的方法,包括基本的SELECT语句、条件筛选、联表查询、聚合函数等,并展示了如何解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CREATE TABLE student (

id INTEGER,

name VARCHAR2(20) NOT NULL ,

sex VARCHAR2(4) ,

birth VARCHAR2(10),

department VARCHAR2(20) ,

address VARCHAR2(50)

);

 

CREATE TABLE score (

id INTEGER,

stu_id INTEGER NOT NULL ,

c_name VARCHAR2(20) ,

grade INTEGER

);

 

INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');

INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');

INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');

INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');

INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');

INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');

 

INSERT INTO score VALUES(1,901, '计算机',98);

INSERT INTO score VALUES(2,901, '英语', 80);

INSERT INTO score VALUES(3,902, '计算机',65);

INSERT INTO score VALUES(4,902, '中文',88);

INSERT INTO score VALUES(5,903, '中文',95);

INSERT INTO score VALUES(6,904, '计算机',70);

INSERT INTO score VALUES(7,904, '英语',92);

INSERT INTO score VALUES(8,905, '英语',94);

INSERT INTO score VALUES(9,906, '计算机',90);

INSERT INTO score VALUES(10,906, '英语',85);

 

 

1、从student表中查询计算机系和英语系的学生的信息

 

2、从student表中查询年龄18~22岁的学生信息

 

3、从student表中查询每个院系有多少人

 

4、从score表中查询每个科目的最高分

 

5、查询李四的考试科目(c_name)和考试成绩(grade)

 

6、查询所有学生的信息和考试信息

 

7、计算每个学生的总成绩

 

8、查询计算机成绩低于95的学生信息

 

9、查询同时参加计算机和英语考试的学生的信息

 

10、将计算机考试成绩按从高到低进行排序

 

11、查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

 

 

-- 1、从student表中查询计算机系和英语系的学生的信息

 

select * from student where department in ('计算机系','英语系');

 

 

-- 2、从student表中查询年龄18~22岁的学生信息

 

select * from student where (to_char(sysdate,'yyyy')-to_number(birth)) between 25 and 35;

select * from student where datediff(year,birth,getdate) between 25 and 35;

 

 

-- 3、从student表中查询每个院系有多少人

 

select department,count(*) from student group by department;

 

select * from score;

-- 4、从score表中查询每个科目的最高分

 

select c_name,max(grade) from score group by c_name;

 

 

-- 5、查询李四的考试科目(c_name)和考试成绩(grade)

 

select a.name,b.c_name,b.grade from student a inner join score b on a.id=b.stu_id and a.name='李四';

select a.name,b.c_name,b.grade from student a,score b where a.id=b.stu_id and a.name='李四';

 

 

-- 6、查询所有学生的信息和考试信息

 

select a.*,b.* from student a inner join score b on a.id=b.stu_id; --内连接

select a.*,b.* from student a full join score b on a.id=b.stu_id; --全连接

 

 

-- 7、计算每个学生的总成绩

 

select stu_id,sum(grade) from score group by stu_id;

 

select stu_id,name,sum(grade) from (select a.*,b.* from student a inner join score b on a.id=b.stu_id) group by stu_id,name;

 

 

-- 8、查询计算机成绩低于95的学生信息

 

select * from student where id in (select stu_id from score where grade<95 and c_name='计算机'); 【用了子查询,二次查询】

 

select t.* from student t inner join score s on t.id=s.stu_id and s.c_name='计算机' and s.grade<95; 【效率最高,一次查询】

 

 

-- 9、查询同时参加计算机和英语考试的学生的信息

--交集intersect

select * from student where id in (select stu_id from score where c_name='计算机' intersect select stu_id from score where c_name='英语');

 

 

 

select t.* from student t inner join score s on t.id=s.stu_id and s.c_name='计算机' and t.id=s.stu_id and s.c_name='英语';

 

 

-- 10、将计算机考试成绩按从高到低进行排序

 

select grade from score where c_name='计算机' order by grade desc;

 

 

-- 11、查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

 

select name,department,c_name,grade from (select a.*,b.* from student a inner join score b on a.id=b.stu_id) where name like '张%' or name like '王%';

 

select t.name,s.department,c_name,s.grade from student t inner join score s on t.id=s.stu_id and name like '张%' or name like '王%';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值