题目来源:https://blog.csdn.net/flycat296/article/details/63681089

teradata实现:

drop table student;
create table student(
s_id varchar(10),
sname varchar(20),
sage date,
sex varchar(20)
); insert into Student values('01' , '赵雷' ,'1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
select * from student; create table course(
c_id varchar(10),
cname varchar(20),
t_id varchar(10)
); insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
select * from course;
create table teacher(
t_id varchar(10),
tname varchar(20)
); insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
select * from teacher;
create table sc(
s_id varchar(10),
c_id varchar(10),
score decimal(18,1)
);
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
select * from sc order by s_id , c_id; /*1*/
select t1.s_id,t1.c_id,t1.score,t2.c_id,t2.score
from sc t1 inner join sc t2
on t1.s_id=t2.s_id
and t1.c_id='01'
and t2.c_id='02'
and t1.score>t2.score; select a.s_id,a.c_id,b.s_id,b.c_id
from (select *from sc where c_id='01') a
left join (select * from sc where c_id='02') b
on a.s_id=b.s_id
where a.score >b.score; /*1.1*/
select t1.s_id,t1.c_id,t2.s_id,t2.c_id
from sc t1 inner join sc t2
on t1.s_id=t2.s_id
and t1.c_id='01'
and t2.c_id='02'; select *from
(select *from sc where c_id='01') a
left join (select * from sc where c_id='02') b
on a.s_id=b.s_id
where b.s_id is not null; /*1.2*/
select *from
(select *from sc where c_id='01') a
left join (select * from sc where c_id='02') b
on a.s_id=b.s_id
; /*1.3*/
select * from
sc where c_id='02'
and s_id not in
(select s_id from sc where c_id='01'); /*2*/
select a.s_id,b.sname,avg(a.score)
from sc a left join student b
on a.s_id=b.s_id
group by a.s_id,b.sname
having avg(a.score)>=60; /*3*/
select * from student where s_id in (select s_id from sc group by s_id) /*4*/
select a.s_id,a.sname,count(b.s_id),sum(b.score)
from student a left join sc b
on a.s_id=b.s_id
group by a.s_id , a.sname; /*4.1*/ select a.s_id,b.s_id, a.countclass,a.totlescore
from
(select s_id,count(s_id) countclass,sum(score) totlescore from sc group by s_id) a
left join
student b
on a.s_id=b.s_id /*5*/
select count(*) from teacher where tname like '李%';
/*6*/
select *
from student a left join sc b
on a.s_id=b.s_id
left join course c
on b.c_id= c.c_id
left join teacher d
on c.t_id =d.t_id
where d.tname='张三';
/*7*/
select a.s_id,a.sname,a.sage,a.sex from student a left join sc b
on a.s_id =b.s_id
having count(b.s_id)<3
group by a.s_id,a.sname,a.sage,a.sex
;
/*8*/
select * from student where s_id
in (select distinct s_id from sc where c_id in (select c_id from sc where s_id='01') ) /*9*/
select * from student where
s_id in(select s_id from sc where c_id in
(select c_id from sc where s_id='01' ) and s_id<>'01'
group by s_id having count(s_id)>=3) ; /*10*/
select * from student where s_id not in
(select s_id from sc where c_id in
(select c_id from course where t_id in
(select t_id from teacher where tname='张三'))); /*11*/
select a.s_id,a.sname, b.avg_score
from student a right join (select s_id ,avg(score) avg_score from sc where score<60 group by s_id having count(score)>=2) b
on a.s_id =b.s_id; /*12*/
select a.s_id,a.sname,a.sage,a.sex, b.score
from student a right join sc b
on a.s_id=b.s_id
where b.c_id='01'
and b.score<60
order by b.score desc;
/*13*难点:按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩*/
select s_id, max(case c_id when '01' then score else 0 end ) a,
max(case c_id when '02' then score else 0 end ) b,
max(case c_id when '03' then score else 0 end) c,
avg(score)
from sc
group by s_id
order by 5 desc; /*14*/
select a.c_id,a.cname,b.highest,b.lowest,b.avgscore,c.jigelv,d.middle,e.excellent,f.great,g.people_number
from course a left join
(select c_id,max(score) highest, min(score) lowest ,avg(score) avgscore
from sc group by c_id) b
on a.c_id=b.c_id
left join
(select c_id,(sum(case when score>=60 then 1 else 0 end)*1.00/count(*)*100) jigelv from sc group by c_id) c
on a.c_id=c.c_id
left join
(select c_id ,(sum(case when score>=70 and score<80 then 1 else 0 end)*1.00/count(*)*100) middle from sc group by c_id) d
on a.c_id=d.c_id
left join
(select c_id,(sum(case when score>=80 and score<90 then 1 else 0 end )*1.00/count(*)*100) excellent from sc group by c_id)e
on a.c_id=e.c_id
left join
(select c_id,(sum(case when score>=90 then 1 else 0 end )*1.00/count(*)*100) great from sc group by c_id ) f
on a.c_id=f.c_id
left join
(select c_id,count(*) people_number from sc group by c_id) g
on a.c_id=g.c_id
order by g.people_number,a.c_id;
/*15,15.1 row_number()over() rank()over() dense_rank()*/
select s_id, c_id,score,row_number()over(partition by c_id order by score desc ) rank1
from sc; select s_id,c_id,score,rank()over(partition by c_id order by score desc) rank1
from sc; select s_id,c_id,score,dense_rank() over(partition by c_id order by score desc) rank1
from sc; /*16*/
select s_id,sum(score),rank()over(order by sum(score) desc) from sc group by s_id;
/*16.1*/
select s_id,sum(score),dense_rank()over(order by sum(score) desc) from sc group by s_id; /*17*/
select c_id, sum(case when score<=60 then 1 else 0 end ) a1,
(sum(case when score<=60 then 1 else 0 end)*1.00/count(*) ) a2
from sc group by c_id
order by c_id; /*18*/
select * from
(select c_id,s_id,score,rank()over( partition by c_id order by score desc ) rank1
from sc ) b where b.rank1<=3
/*方法二:难点*/
select a.c_id,a.s_id ,a.score,b.score from sc a
left join sc b
on a.c_id=b.c_id and a.score<b.score
group by a.s_id,a.c_id,a.score
having count(b.s_id)<3
order by a.c_id,a.score desc; select * from sc a where
(select count(*) from sc where c_id=a.c_id and score>a.score)<3
order by a.c_id, a.score desc
/*19*/
select c_id ,count(*) from sc group by c_id;
/*20*/
select s_id from sc group by s_id having count(s_id)=2
/*21*/
select sex,count(sex) from student group by sex
/*22*/
select * from student where sname like'%风%';
/*23*/ select a.s_id,b.countnumber from student a
left join (select sname,sex,count(*) countnumber from student group by sname,sex)b
on a.sname=b.sname
and a.sex=b.sex
where b.countnumber>1;
/*24 to_char()的使用*/
select s_id ,sname from student where to_char(sage,'yyyy')=1990
/*25*/
select c_id,avg(score) avgscore from sc group by c_id order by avgscore desc,c_id; /*26*/
select a.s_id,a.sname,avg(b.score) avgscore
from student a left join sc b
on a.s_id=b.s_id
group by a.s_id ,a.sname
having avg(b.score)>85;
/*27*/
select a.sname,b.score from
student a left join sc b
on a.s_id=b.s_id
left join course c
on b.c_id=c.c_id
where c.cname='数学'
and b.score<60; /*28*/
select a.s_id,a.sname,b.c_id,b.score,c.cname
from student a left join sc b
on a.s_id=b.s_id
left join course c
on b.c_id=c.c_id
order by a.s_id,b.c_id;
/*29%%*/
select a.sname,c.cname,b.score
from (select s_id,c_id,score from sc where score>70) b
left join
course c on b.c_id=c.c_id
left join student a
on b.s_id=a.s_id; /*30*/
select c_id,count(*)
from sc where score<60
group by c_id
;
/*31*/
select a.c_id,count(*) from
course a left join sc b
on a.c_id=b.c_id
group by a.c_id
/*32*/
select c_id,count(*)
from
sc group by c_id;
/*33 %%成绩不重复*/
select top 1* from sc
where c_id in (select c_id from course where t_id in (select t_id from teacher where tname='张三'))
order by score desc; /*34 %%成绩重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩*/
select a.s_id,a.sname,b.score,b.c_id from
(select c_id,max(score) maxscore
from sc
group by c_id
) e
left join
sc b
on e.maxscore=b.score and e.c_id=b.c_id
left join course c
on b.c_id=c.c_id
left join teacher d
on c.t_id=d.t_id
left join student a
on a.s_id=b.s_id
where d.tname='张三'; /*dense_rank()*/
select e.s_id,e.c_id,e.score
from (select s_id ,c_id, score,dense_rank()over(partition by c_id order by s_id)rank1
from sc) e
left join course c
on e.c_id=c.c_id
left join teacher d
on c.t_id=d.t_id
where d.tname='张三' and e.rank1=1; select top 1*
from (select s_id ,c_id, score,dense_rank()over(partition by c_id order by s_id)rank1
from sc) e
left join course c
on e.c_id=c.c_id
left join teacher d
on c.t_id=d.t_id
where d.tname='张三' ; /*不同学生课程相同,分数相同*/
select a.s_id,a.c_id,a.score,b.s_id,b.c_id,b.score from sc a left join sc b
on a.score=b.score and a.s_id>b.s_id and a.c_id=b.c_id
where b.score is not null; /*35,查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩*/
select c.s_id,max(c.c_id) c_id,max(c.score) score from sc c
left join (select s_id ,avg(score)a from sc group by s_id)b
on c.s_id=b.s_id
where c.score=b.a
group by c.s_id
having count(0)=(select count(0) from sc where s_id=c.s_id) /*存在三行,如何归并成一行*/
select a.s_id,a.c_id,b.s_id,b.c_id,a.score,b.score from
(select s_id,c_id, score,rank()over(partition by s_id order by score) rank1 from sc) a
inner join
(select s_id,c_id, score,rank()over(partition by s_id order by score) rank1 from sc)b
on a.rank1=b.rank1
and a.s_id=b.s_id
and a.c_id<b.c_id;
/*36*/
select * from
(select s_id,c_id,score,rank()over(partition by c_id order by score) rank1
from sc )a where a.rank1<3; /*37*/
select c_id,count(*)from sc
group by c_id
having count(*)>5; /*38*/
select s_id from sc
group by s_id
having count(c_id)>=2 /*39*/
select s_id from sc
group by s_id
having count(c_id)=3;
/*40*/
select s_id,sname,extract(year from date)-extract(year from sage) age from student;

  

超经典sql练习题,在teradata上实现的更多相关文章

  1. 超经典SQL练习题,做完这些你的SQL就过关了

    使用方法:我用的数据库是 Ms SQL Server 2008 ,练习时应当自己建数据,自己先思考,切勿急躁翻答案!否则效果减半,做完这些,恭喜你,你的 SQL 就算过关了. 测试表格 --1.学生表 ...

  2. j接近50道经典SQL练习题,附建表SQL解题SQL

    说明 本文章整理了47道常见sql联系题,包括建表语句,表结构,习题列表,解题答案都涵盖在本文章内.文末提供了所用SQL脚本下载链接.所有解题答案都是本人自己写的,广大读者如果在阅读使用中,有任何问题 ...

  3. [转] 经典SQL练习题

    原题目来自qaz13177_58_CSDN博客 http://blog.csdn.net/qaz13177_58_/article/details/5575711/#sql 只是更新个人答案供参考 表 ...

  4. 经典SQL练习题

    题目地址:http://blog.csdn.net/qaz13177_58_/article/details/5575711 1. 查询Student表中的所有记录的Sname.Ssex和Class列 ...

  5. 【转载】【SQL练习】经典SQL练习题

    出处 https://blog.csdn.net/mrbcy/article/details/68965271 准备数据 建表语句 CREATE TABLE students (sno VARCHAR ...

  6. SQL练习题汇总(Sqlserver和Mysql版本)

    所需表及数据执行脚本: CREATE TABLE STUDENT (SNO ) NOT NULL, SNAME ) NOT NULL, SSEX ) NOT NULL, SBIRTHDAY DATET ...

  7. 面试题: 数据库 sql优化 sql练习题 有用 学生表,课程表,成绩表,教师表 练习

    什么是存储过程?有哪些优缺点? 什么是存储过程?有哪些优缺点? 存储过程就像我们编程语言中的函数一样,封装了我们的代码(PLSQL.T-SQL). 存储过程的优点: 能够将代码封装起来 保存在数据库之 ...

  8. Oracle经典SQL

    最近本人整理了一些Oracle sql,现分享给大家,后续还会更新.如果有错误的地方,请指正,共同学习.贴上去的sql都是我测试过的,大家可以粘贴在自己的电脑上试试. 1.查询部门的名称,及最低收入雇 ...

  9. 经典SQL语句大全以及50个常用的sql语句

    经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...

随机推荐

  1. 复刻smartbits的国产网络测试工具minismb-操作技巧

    复刻smartbits的国产网络性能测试工具smartbits,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此工具测试任何ip网络设备的端口吞吐率,带宽,并发连 ...

  2. 【SpringBoot系列2】SpringBoot整合Redis

    前言: 真的越来越喜欢SpringBoot了,这是SpringBoot学习系列之一. 正文: 1:首先在pom文件中添加依赖,记得是spring-boot-starter-data-redis,不是s ...

  3. CruiseControl 安装配置

    https://sourceforge.net/projects/ccnet/files/CruiseControl.NET%20Releases/ 一个完整的配置文件(VS2010的解决方案)其他版 ...

  4. .net Core 部署到 Linux

    1.环境说明 服务器系统:CentOS 7.4  64位 相关工具:Xshel.Xftp .net Core版本:2.2 VS版本:2017 服务器软件软件:.netcore.nginx.superv ...

  5. Java基础之多态性

    class A { public void fun1(){ System.out.println("A--->public fun1()"); } public void f ...

  6. LDA(线性判别分析,Python实现)

    源代码: #-*- coding: UTF-8 -*- from numpy import * import numpy def lda(c1,c2): #c1 第一类样本,每行是一个样本 #c2 第 ...

  7. Java-函数式编程(二)Lambda表达式

    本文首发:Java-函数式编程(二)Lambda表达式 “Lambda 表达式”(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lamb ...

  8. html 如何访问 jar 包里面的静态资源(js、css、字体等)

    前言:最近两天在尝试写一个工具 jar 包,里面包含后台处理的 java 代码,包含前端 html.js.css.字体文件等,过程中解决了访问 jar 包里的静态资源问题,所以记录下来. 附:自己的一 ...

  9. Singleton(单例)模式和Double-Checked Locking(双重检查锁定)模式

    问题描述 现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能:在实际开发过程中,会专门有一个日志模块,负责写日志,由于在系统的任何地方,我们都有可能要调用日志模块中的函数,进 ...

  10. agc007D - Shik and Game(dp 单调性)

    题意 题目链接 Sol 主人公的最优决策一定是经过熊->返回到某个位置->收集经过的钻石 那么可以直接设\(f[i]\)表示收集完了前\(i\)个位置的钻石的最小时间,转移的时候枚举下最后 ...