1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名
namekechengfenshu
张三语文81
张三数学75
李四语文76
李四数学90
王五语文81
王五数学100
王五英语90
select name from W_03 group by name having min(fenshu)>80
2. 学生表 如下:
自动编号学号姓名课程编号课程名称分数
12005001张三0001数学69
22005002李四0001数学89
32005001张三0001数学69
删除除了自动编号不同, 其他都相同的学生冗余信息
delete from W_03 t
where number in(select number from W_03
group by number having count(number )>1)
and t.rowid not in(select min(t.rowid) from W_03 t
group by number having count(number )>1)
2. 怎么把这样一个表儿
yearmonth amount
19911 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
yearm1m2m3m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
select year,
sum(decode(month,'1',amount,null)) as m1,
sum(decode(month,'2',amount,null)) as m2,
sum(decode(month,'3',amount,null)) as m3,
sum(decode(month,'4',amount,null)) as m4
from W_04
group by year
order by year
3. 说明:拷贝表( 拷贝数据, 源表名:a 目标表名:b)。两个表的字段名和字段内型都相同(F1,F2,F3,F4,F5)。
insert into table W_05 as select * from W_03
4. 有一张表GRADE,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文数学英语
及格优秀不及格
select
(case when S_CHINESE>=80 then '优秀' when S_CHINESE>=60 then '及格' else '不及格' end) as 语文,
(case when S_MARTH>=80 then '优秀' when S_MARTH>=60 then '及格' else '不及格' end) as 数学,
(case when S_ENGLISH>=80 then '优秀' when S_ENGLISH>=60 then '及格' else '不及格' end) as 英语
from W_03
思路:刚开始不知道case的用法,尝试了好久,然后去网上搜了一下,发现了这个查询需要case语句,最后看完case语句用法后慢慢写出来
6、编写SQL语句
1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话
create table W_06
(
S_number number,
S_name varchar(30),
S_age number,
S_sex varchar(10),
S_address varchar(50),
S_phone number
)
2) 修改学生表的结构,添加一列信息,学历
alter table W_06
add S_eduction varchar(20)
3) 修改学生表的结构,删除一列信息,家庭住址
alter table W_06
drop column S_ADDRESS
4) 向学生表添加如下信息:
学号姓名年龄性别联系电话学历
1A22男123456小学
2B21男119中学
3C23男110高中
4D18女114大学
insert ALL into W_06(S_NUMBER,S_NAME,S_AGE,S_SEX,S_PHONE,S_EDUCTION)
values(1,'A',22,'男',123456,'小学')
into W_06 values(2,'B',21,'男',119,'中学')
into W_06 values(3,'C',23,'男',110,'高中')
into W_06 values(4,'D',18,'女',114,'大学') SELECT 1 FROM DUAL;
//Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中
4) 修改学生表的数据,将电话号码以11开头的学员的学历改为“大专”
update W_06 set S_EDUCTION='大专' where S_PHONE like '11%';
5) 删除学生表的数据,姓名以C开头,性别为‘男’的记录删除
delete from W_06
where S_NAME like 'C%' and S_SEX='男'
6) 查询学生表的数据,将所有年龄小于22岁的,学历为“大专”的,学生的姓名和学号示出来
select S_NUMBER,S_NAME from W_06 where S_EDUCTION='大专' and S_AGE<22
7) 查询学生表的数据,查询所有信息,列出前25%的记录
select * from W_06 where rownum<=(select count(*)*0.25 from W_06);
8) 查询出所有学生的姓名,性别,年龄降序排列
select S_NAME,S_SEX,S_AGE from W_06 order by S_NAME,S_SEX,S_AGE DESC;
9) 按照性别分组查询所有的平均年龄
select S_SEX,AVG(S_AGE)as 平均年龄 from W_06 group by S_SEX
7、查询A(ID,Name)表中第31至40条记录,ID作为主键,可能是不是连续增长的列,完整的查询语句如下:
select ID from(select ID ,rownum rn from W_05) where rn>=31and rn<=40
8、查询表A中存在ID重复三次以上的记录,完整的查询语句如下:
select * from W_05 where ID in(
select ID from W_05 group by ID having count(ID)>3)
9、说明:随机取出表A中10条数据
select top 10 * from tablename order by newid()
SELECT * FROM (SELECT * FROM A ORDER BY DBMS_RANDOM.RANDOM()) WHERE rownum <= 10
10、查询表(Scores)中平均成绩大于60分的同学的学号(STU_ID)和平均成绩(SCORE);
select STU_ID,avg(fenshu) from W_05 group by STU_ID having avg(fenshu)>60