超經典sql練習題,在teradata上實現

来源:https://www.cnblogs.com/papio/archive/2018/08/05/9426374.html
-Advertisement-
Play Games

題目來源:https://blog.csdn.net/flycat296/article/details/63681089 teradata實現: ...


題目來源: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;

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 資料庫編程 從前面我們知道資料庫概念包含 資料庫文件、伺服器和資料庫客戶端 客戶端我們之前已經用過的有navicat/mysql-client等程式。 問題: 如何使用客戶端將100000行數據插入到資料庫? 大家會發現如果用之前客戶端幾乎是不可能完全這個任務的, 因為我們不可能去構造出那個插入10 ...
  • 該文章是基於 Hadoop2.7.6_01_部署 、 Hive-1.2.1_01_安裝部署 進行的 1. 前言 在一個完整的大數據處理系統中,除了hdfs+mapreduce+hive組成分析系統的核心之外,還需要數據採集、結果數據導出、任務調度等不可或缺的輔助系統,而這些輔助工具在hadoop生態 ...
  • 占座 ...
  • 1197多語句事務要求更大的max_binlog_cache_size報錯 binlog_cache_size:為每個session 分配的記憶體,在事務過程中用來存儲二進位日誌的緩存,提高記錄bin-log的效率。沒有什麼大事務,dml也不是很頻繁的情況下可以設置小一點,如果事務大而且多,dml操作 ...
  • sqlServer_基礎概念 SQL server的管理工具 SQL server聯機叢書 開始菜單à Microsoft SQL Server 2008 à 文檔和教程 à SQL Server聯機叢書 SQL Server 配置管理器 用於啟動和管理SQL server資料庫的服務端,以及其他相 ...
  • mysql資料庫基礎知識 什麼是資料庫 資料庫顧名思義,就是用來存儲數據的工具,用一個比例形象的例子來比喻,就是Excel,一個Excel文件就可以看成是一個資料庫。 關係型資料庫 就是以行與列構成的二維數據表的形式,用來存儲數據的,並且將多張數據表存儲在一個單元中的存儲形式,就稱為關係型資料庫。 ...
  • Oracle Drop表並未直接刪除 drop table xx purge drop表 執行drop table xx 語句 drop後的表被放在回收站(user_recyclebin)里,而不是直接刪除掉。這樣,回收站里的表信息就可以被恢復,或徹底清除。 通過查詢回收站user_recycleb ...
  • 一.背景 在上一篇《資料庫操作類SqlHelper》博文的最後,提到了一個實踐運用中遇到的問題,就是資料庫表中的自增長欄位的賦值不受人為控制。比如資料庫有一個tb_Department表,DeptNO欄位為自增長主鍵。 現在插入一行數據 啊!DeptNO欄位怎麼就是22了呢,不應該是從4開始嗎? 原 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...