SQL練習題:2.4

建表

# 學生表
create table t_student
(stu_id   varchar(10),stu_name varchar(10),stu_age  datetime,stu_sex  varchar(10)
);# 課程表
create table t_t_course
(c_id    varchar(10),c_name  varchar(10),c_teaid varchar(10)
);# 教師表
create table t_t_teacher
(tea_id   varchar(10),tea_name varchar(10)
);# 成績表
create table t_t_score
(s_stuid varchar(10),s_cid   varchar(10),s_score decimal(18, 1)
);-- 向t_student表插入數據
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('01', '趙雷', '1990-01-01', '男');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('02', '錢電', '1990-12-21', '男');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('03', '孫風', '1990-12-20', '男');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('04', '李云', '1990-12-06', '男');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('05', '周梅', '1991-12-01', '女');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('06', '吳蘭', '1992-01-01', '女');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('07', '鄭竹', '1989-01-01', '女');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('09', '張三', '2017-12-20', '女');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('10', '李四', '2017-12-25', '女');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('11', '李四', '2012-06-06', '女');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('12', '趙六', '2013-06-13', '女');
insert into t_student(stu_id, stu_name, stu_age, stu_sex)
values ('13', '孫七', '2014-06-01', '女');-- 向t_t_course表插入數據
insert into t_t_course(c_id, c_name, c_teaid)
values ('01', '語文', '02');
insert into t_course(c_id, c_name, c_teaid)
values ('02', '數學', '01');
insert into t_course(c_id, c_name, c_teaid)
values ('03', '英語', '03');-- 向t_t_teacher表插入數據
insert into t_teacher(tea_id, tea_name)
values ('01', '張三');
insert into t_teacher(tea_id, tea_name)
values ('02', '李四');
insert into t_teacher(tea_id, tea_name)
values ('03', '王五');-- 向t_t_score表插入數據
insert into t_score(s_stuid, s_cid, s_score)
values ('01', '01', 80);
insert into t_score(s_stuid, s_cid, s_score)
values ('01', '02', 90);
insert into t_score(s_stuid, s_cid, s_score)
values ('01', '03', 99);
insert into t_score(s_stuid, s_cid, s_score)
values ('02', '01', 70);
insert into t_score(s_stuid, s_cid, s_score)
values ('02', '02', 60);
insert into t_score(s_stuid, s_cid, s_score)
values ('02', '03', 80);
insert into t_score(s_stuid, s_cid, s_score)
values ('03', '01', 80);
insert into t_score(s_stuid, s_cid, s_score)
values ('03', '02', 80);
insert into t_score(s_stuid, s_cid, s_score)
values ('03', '03', 80);
insert into t_score(s_stuid, s_cid, s_score)
values ('04', '01', 50);
insert into t_score(s_stuid, s_cid, s_score)
values ('04', '02', 30);
insert into t_score(s_stuid, s_cid, s_score)
values ('04', '03', 20);
insert into t_score(s_stuid, s_cid, s_score)
values ('05', '01', 76);
insert into t_score(s_stuid, s_cid, s_score)
values ('05', '02', 87);
insert into t_score(s_stuid, s_cid, s_score)
values ('06', '01', 31);
insert into t_score(s_stuid, s_cid, s_score)
values ('06', '03', 34);
insert into t_score(s_stuid, s_cid, s_score)
values ('07', '02', 89);
insert into t_score(s_stuid, s_cid, s_score)
values ('07', '03', 98);
練習
#**25. 查詢和學號"01"同學學習的課程完全相同的其他同學的信息**
select t.stu_id, count(*)
from t_student tinner join t_score sc on t.stu_id = sc.s_stuidinner join t_course co on sc.s_cid = co.c_id
where co.c_id in (select co.c_idfrom t_student t1inner join t_score sc1 on t1.stu_id = sc1.s_stuidinner join t_course co1 on sc1.s_cid = co1.c_idwhere t1.stu_id = 01)
group by t.stu_id
having count(*) = (select count(*)from t_student t1inner join t_score sc1 on t1.stu_id = sc1.s_stuidinner join t_course co1 on sc1.s_cid = co1.c_idwhere t1.stu_id = 01);#26. 查詢沒學過"張三"老師講授的任一門課程的學生姓名
select s1.stu_name, s1.stu_id
from t_student s1
where s1.stu_id not in (select s.stu_idfrom t_student sinner join t_score sc on s.stu_id = sc.s_stuidinner join t_course co on sc.s_cid = co.c_idwhere co.c_id in (select co1.c_idfrom t_teacher teinner join t_course co1 on te.tea_id = co1.c_teaidwhere te.tea_name = '張三'));#**27. 查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績** (成績保留兩位小數)
select s.stu_id, s.stu_name, AVG(sc.s_score)
from t_student sinner join t_score sc on s.stu_id = sc.s_stuid
where sc.s_score < 60
group by s.stu_id, s.stu_name
having count(*) >= 2;#**28. 檢索"01"課程分數小于 60,按分數降序排列的學生信息**
select *
from t_student sinner join t_score sc on s.stu_id = sc.s_stuidinner join t_course co on sc.s_cid = co.c_id
where sc.s_score < 60and co.c_id = 01
order by sc.s_score DESC;#**29. 按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績** (平均成績保留兩位小數)
select s.*, sc.s_score, avg.a
from t_student sinner join t_score sc on s.stu_id = sc.s_stuidinner join (Select Avg(sc1.s_score) as 'a', sc1.s_stuid from t_score sc1 group by sc1.s_stuid) avgon s.stu_id = avg.s_stuid
order by a DESC;#**30. 查詢各科成績最高分、最低分、平均分、選修人數、及格率** (及格率以百分比格式顯示)
select MAX(sc.s_score),MIN(sc.s_score),AVG(sc.s_score),COUNT(sc.s_score),concat(FORMAT(((select Count(*) from t_score sc1 where sc1.s_cid = sc.s_cid and sc1.s_score >= 60) /(select Count(*) from t_score sc2 where sc2.s_cid = sc.s_cid)) * 100, 2), '%')
from t_score scinner join t_student s on s.stu_id = sc.s_stuid
group by sc.s_cid;#31. 按各科成績進行排序,并顯示排名, 成績重復時不保留名次空缺
select s.*, sc.s_cid, row_number() over (PARTITION BY sc.s_cid ORDER BY sc.s_score)
from t_student sinner join t_score sc on s.stu_id = sc.s_stuid;#**32. 按各科成績進行排序,并顯示排名,成績重復時保留名次空缺**
select s.*, sc.s_cid, RANK() over (PARTITION BY sc.s_cid ORDER BY sc.s_score)
from t_student sinner join t_score sc on s.stu_id = sc.s_stuid;

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/15725.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/15725.shtml
英文地址,請注明出處:http://en.pswp.cn/web/15725.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

光速入門python的OpenCV

前言 歡迎來到我的博客 個人主頁:北嶺敲鍵盤的荒漠貓-CSDN博客 本文整理python的OpenCV模塊的關鍵知識點 爭取用最短的時間入門OpenCV 并且做到筆記功能直接復制使用 OpenCV簡介 不浪費時間的介紹: 就是類似于ps操作圖片。 至于為什么不直接用ps&#xff0c;因為只有程序能…

【找出滿足差值條件的下標 I】python

目錄 暴力題解 優化&#xff1a;滑動窗口維護大小值 暴力題解 class Solution:def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:nlen(nums)for i in range(n):for j in range(n-1,-1,-1):if abs(i-j)>indexDiffere…

海康威視NVR通過ehome協議接入視頻監控平臺,視頻瀏覽顯示3011超時錯誤的問題解決,即:The request timeout! 【3011】

目錄 一、問題描述 二、問題分析 2.1 初步分析 2.2 查看日志 2.3 問題驗證 1、查看防火墻 2、查看安全組 3、問題原因 三、問題解決 3.1 防火墻開放相關端口 3.2 安全組增加規則 3.3 測試 1、TCP端口能夠聯通的情況 2、TCP端口不能夠聯通的情況 四、驗證 五、云…

「51媒體」如何與媒體建立良好關系?

傳媒如春雨&#xff0c;潤物細無聲&#xff0c;大家好&#xff0c;我是51媒體網胡老師。 與媒體建立良好關系對于企業或個人來說都是一項重要的公關活動。 了解媒體&#xff1a;研究媒體和記者的興趣&#xff0c;提供相關且有價值的信息。 建立聯系&#xff1a;通過專業的方式…

牛客NC324 下一個更大的數(三)【中等 雙指針 Java/Go/PHP/C++】參考lintcode 52 · 下一個排列

題目 題目鏈接&#xff1a; https://www.nowcoder.com/practice/475da0d4e37a481bacf9a09b5a059199 思路 第一步&#xff1a;獲取數字上每一個數&#xff0c;組成數組arr 第二步&#xff1a;利用“下一個排列” 問題解題方法來繼續作答&#xff0c;步驟&#xff1a;利用lintc…

C++進階之路:何為拷貝構造函數,深入理解淺拷貝與深拷貝(類與對象_中篇)

?? 歡迎大家來訪Srlua的博文&#xff08;づ&#xffe3;3&#xffe3;&#xff09;づ╭?&#xff5e;?? &#x1f31f;&#x1f31f; 歡迎各位親愛的讀者&#xff0c;感謝你們抽出寶貴的時間來閱讀我的文章。 我是Srlua小謝&#xff0c;在這里我會分享我的知識和經驗。&am…

PostgreSQL基礎(三):PostgreSQL的基礎操作

文章目錄 PostgreSQL的基礎操作 一、用戶操作 二、權限操作 三、操作任務

DRM驅動(五)之drm_atomic_state

上節講到《DRM驅動&#xff08;四&#xff09;之ADD_FB》調用drmModeAddFB創建drm_framebuffer。然后通過 drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map); vaddr mmap(0, create.size, PROT_READ | PROT_WRITE,MAP_SHARED, fd, map.offset); 將物理地址map到用戶空間后…

Python中list遍歷的幾種方式之沒有好與不好,只有合適不合適

Python中list遍歷的幾種方式 引言 Python是一種動態、解釋型的高級編程語言&#xff0c;以其簡潔、易讀的語法而廣受歡迎。在Python中&#xff0c;list是一種非常重要的數據結構&#xff0c;它允許存儲一系列的元素&#xff0c;這些元素可以是任何類型。遍歷list是處理數據的…

nginx的Connection refused

問題描述 nginx的錯誤日志中突然出現大量的的Connection refused問題&#xff0c;日志如下&#xff1a; 2020/03/19 09:52:53 [error] 20117#20117: *7403411764 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server:…

解決CLion調試時無法顯示變量值的問題

1 問題描述 使用CLion的時候&#xff0c;調試時無法顯示變量的值&#xff0c;例如&#xff1a; 圖來自StackOverflow。 2 解決辦法 可以嘗試切換調試器解決&#xff0c;在Linux下&#xff0c;CLion支持GDB和LLDB&#xff0c;如果GDB不行&#xff0c;可以切換到LLDB。 切換方…

醫院信息化IT監控一體化運維實踐

作者: 曉風 在醫療信息化日益發展的今天&#xff0c;醫院數據中心的運維工作顯得尤為重要。為了確保醫療系統的穩定運行&#xff0c;保障患者數據的安全與完整&#xff0c;我院在信息化IT監控一體化運維方面進行了深入的探索和實踐。 一、背景與挑戰 我院的機房設備規模已有50…

主動歸檔存儲的策略研討

在媒體與娛樂&#xff08;M&E&#xff09;行業中&#xff0c;主動存檔策略對于應對內容的持續需求增長、控制存儲成本膨脹以及實現檔案內容的貨幣化至關重要。以下是對此策略的深入分析&#xff1a; ### 持續的內容需求帶來的挑戰 M&E企業面臨著巨大的挑戰&#xff1…

【Spring】SSM整合_入門代碼實現

1. Maven依賴 在pom.xml中添加SSM框架的依賴 <!-- Spring Core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.x</version> </dependency>…

軟件杯 題目: 基于深度學習的疲勞駕駛檢測 深度學習

文章目錄 0 前言1 課題背景2 實現目標3 當前市面上疲勞駕駛檢測的方法4 相關數據集5 基于頭部姿態的駕駛疲勞檢測5.1 如何確定疲勞狀態5.2 算法步驟5.3 打瞌睡判斷 6 基于CNN與SVM的疲勞檢測方法6.1 網絡結構6.2 疲勞圖像分類訓練6.3 訓練結果 7 最后 0 前言 &#x1f525; 優…

為什么單片機不能直接驅動繼電器和電磁閥

文章是瑞生網轉載&#xff0c;PDF格式文章下載&#xff1a; 為什么單片機不能直接驅動繼電器和電磁閥.pdf: https://url83.ctfile.com/f/45573183-1247189072-10b6d1?p7526 (訪問密碼: 7526)

java-數組內存分配

在 Java 中&#xff0c;數組是一種基本數據結構&#xff0c;用于存儲一系列相同類型的數據。在內存中&#xff0c;數組分配是一塊連續的內存空間&#xff0c;用于存儲數組中的所有元素。本篇文章將詳細解釋 Java 中數組的內存分配&#xff0c;包括數組的聲明、創建、內存模型以…

memcpy的使?和模擬實現

目錄 一&#xff1a;memcpy的使? memcpy的使?的代碼 二&#xff1a;memcpy函數的模擬實現: memcpy和strcpy的區別 用途&#xff1a; 安全性&#xff1a; 數據類型&#xff1a; 性能&#xff1a; 在字符串中的用法示例&#xff1a; memcpy: strcpy 一&#xff1a;…

Ajax面試題精選及參考答案(3萬字長文)

目錄 什么是Ajax,它的核心原理是什么? Ajax應用程序的優勢有哪些? Ajax最大的特點是什么?

Science 基于尖峰時序編碼的模擬神經觸覺系統,可實現動態對象分類

快速處理和有效利用手與物體交互過程中產生的動態觸覺信號&#xff08;例如觸摸和抓握&#xff09;對于觸覺探索和靈巧的物體操作至關重要。將電子皮膚&#xff08;e-skins&#xff09;推進到模仿自然觸覺的水平&#xff0c;是恢復截肢者和癱瘓患者喪失的功能的可行解決方案&am…