Mysql練習題目【7月10日更新】

七、Mysql練習題目

https://zhuanlan.zhihu.com/p/38354000

1. 創建表

創建學生表
mysql> create table if not exists student(-> student_id varchar(255) not null,-> student_name varchar(255) not null,-> birthday date not null,-> gender varchar(255) not null,-> primary key(student_id)-> )default charset utf8;
創建成績表
mysql> create table score(-> student_id varchar(255) not null,-> course_id varchar(255) not null,-> score float(3) not null,-> primary key(student_id,course_id)-> )default charset utf8;
創建課程表
mysql> create table course(-> course_id varchar(255) not null,-> course_name varchar(255) not null,-> teacher_id varchar(255) not null,-> primary key(course_id)-> )default charset utf8;
創建教師表
mysql> create table teacher(-> teacher_id varchar(255) not null, -> teacher_name varchar(255) null, -> primary key(teacher_id) -> ) default charset utf8;

2. 插入數據

學生表插入數據
insert into student
values
('0001','猴子','1989-01-01','男'),
('0002','猴子','1990-12-21','女'),
('0003','馬云','1991-12-21','男'),
('0004','王思聰','1990-05-20','男');
成績表插入數據
insert into score
values
('0001','0001',80),
('0001','0002',90),
('0001','0003',99),
('0002','0002',60),
('0002','0003',80),
('0003','0001',80),
('0003','0002',80),
('0003','0003',80);
課程表插入數據
insert into course
values
('0001','語文','0002'),
('0002','數學','0001'),
('0003','英語','0003');
教師表插入數據
insert into teacher 
values
('0001','孟扎扎'),
('0002','馬化騰'),
('0003',null),
('0004','');

3. 題目

1.查詢姓“猴”的學生名單
select * from student where student_name like "猴%";
2.查詢名字中最后一個字是猴的學生名單
select * from student where student_name like "%猴";
3.查詢名字中帶猴的學生名單
select * from student where student_name like "%猴%";
4.查詢名字中第二個字是猴的學生名單
select * from student where student_name like "_猴%";
5.查詢姓“孟”老師的個數

關鍵字:個數-count

select count(*) from teacher where teacher_name like "孟%";
6.查詢課程編號為“0002”的總成績

關鍵字:總成績-sum

select sum(score) from score where course_id='0002';
7.查詢選了課程的學生人數*

關鍵字:人數-count

select 學號,成績表里學號有重復值需要去掉

select count(distinct student_id) as 學生人數 from score;
8.查詢各科成績最高和最低的分

關鍵字:各科-分組;最高分-max ;最低分-min

select course_id,min(score) as 最低分,max(score) as 最高分 from score group by course_id;
9.查詢每門課程被選修的學生數

關鍵字:每門-分組;學生數-count

select course_id as 課程,count(student_id) as 學生個數 from score group by course_id;
10.查詢男生、女生人數

關鍵字:男生、女生(相當于每,因為性別只有男、女)-分組;人數-count

select gender,count(*) from student group by gender;
11.查詢平均成績大于60分學生的學號和平均成績

關鍵字:平均成績大于-having avg()>

select student_id,avg(score) from score group by student_id having avg(score)>60;
12.查詢至少選修兩門課程的學生學號

關鍵字:至少-count

select student_id from score group by student_id having count(course_id)>=2;
13.查詢同名同姓學生名單并統計同名人數*

關鍵字:人數-count

select student_name,count(student_id) from student group by student_name having count(student_id) >=2; 
14.查詢不及格的課程并按課程號從大到小排列
select * from score where score<60 order by course_id desc;
15.查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列

關鍵字:每門-group by;平均成績-avg

select course_id,avg(score) as 平均成績 from score group by course_id order by 平均成績,course_id desc;
16.檢索課程編號為“0004”且分數小于60的學生學號,結果按按分數降序排列
select student_id student from score where course_id='0004' and score<60 order by score desc;
17.統計每門課程的學生選修人數(超過2人的課程才統計)

關鍵字:每門-group by;人數-count

select course_id,count(student_id) from score group by course_id having count(student_id)>2;
18.查詢兩門以上不及格課程的同學的學號及其平均成績*

19.查詢每個學生的總成績并進行排名

關鍵字:每個-group by;總成績-sum

select student_id,sum(score) as 總成績 from score group by student_id order by 總成績;
20.查詢平均成績大于60分的學生的學號和平均成績

關鍵字:平均成績大于-having avg()>60

select student_id,avg(score) as 平均成績 from score group by student_id having avg(score)>60;
21.查詢所有課程成績小于60分學生的學號、姓名

關鍵字:所有成績小于60-where score < 60

因為姓名在student表中,成績在score表中,所以要用到子查詢。

注意:如果(值1,值2,……)存在重復值時,in (值1,值2,……) 會從(值1,值2,……)中的重復值中選擇一個。即in會過濾掉重復數據

select student_id,student_name from student where student_id in (select student_id from score where score<60);
22.查詢沒有學全所有課的學生的學號、姓名*
select student_id,student_name from student where student_id in (select student_id from score group by student_id having count(course_id) < (select count(*) from course));
23.查詢出只選修了兩門課程的全部學生的學號和姓名
select student_id,student_name from student where student_id in (select student_id from score group by student_id having count(course_id)=2);
24.查詢各科成績前兩名的記錄*

關鍵字:各科-group by


https://mp.weixin.qq.com/s/MuxjlFV0gi1GydOrYfiSeQ

img
25.查找1990年出生的學生名單*
select * from student where year(birthday) = 1990;select * from student where birthday like '1990-%';
26.查詢各學生的年齡(精確到年)*
select student_id, year(current_time)-year(birthday)+1 from student;
27.查詢各學生的年齡(精確到月份)*
select student_id,timestampdiff(month ,birthday ,now())/12 from student;
28.查詢本月過生日的學生*
select * from student where month(birthday)=month(current_date);
29.查詢所有學生的學號、姓名、選課數、總成績*

要顯示的列:學號,姓名,選課數,總成績,分布在兩個表中,所以應該用連表查詢,將兩個表連接起來

關鍵字:所有學生-左連接或右連接:左連接的話join左邊的表應該為student,右連接的話join右邊應該為student表。

注意:分組時,如果要用外連接的條件分組,則選擇的那個表中的列中的數據應該是唯一的,及s.student_id

select s.student_id as 學號,s.student_name as 姓名,count(c.course_id) as 選課數,sum(c.score) as 總成績 from student as s left join score as c on s.student_id=c.student_id group by s.student_id;
30.查詢平均成績大于85的所有學生的學號、姓名和平均成績*
select t1.student_id as '學號',t1.student_name as '姓名',avg(score) as '平均成績' from student as t1 left join score as t2 on t1.student_id = t2.student_id group by  t1.student_id having avg(t2.score)>85;
31.查詢學生的選課情況:學號,姓名,課程號,課程名稱*
select t1.student_id,t1.student_name,t3.course_id,t3.course_name from student as t1 left join score as t2 on t1.student_id=t2.student_id left join course as t3 on t2.course_id=t3.course_id;
32.查詢出每門課程的及格人數和不及格人數*

關鍵字:及格和不及格-分類:case語句

select course_id, sum(case when score>=60 then 1 else 0 end) as 及格人數, sum(case when score<60 then 1 else 0 end) as 不及格人數 from score group by course_id;
33.使用分段[100-85],[85-70],[70-60],[<60]來統計各科成績,分別統計:各分數段人數,課程號和課程名稱
select a.課程號,b.課程名稱,
sum(case when 成績 between 85 and 100 then 1 else 0 end) as '[100-85]',
sum(case when 成績 >=70 and 成績<85 then 1 else 0 end) as '[85-70]',
sum(case when 成績>=60 and 成績<70  then 1 else 0 end) as '[70-60]',
sum(case when 成績<60 then 1 else 0 end) as '[<60]'
from score as a right join course as b 
on a.課程號=b.課程號
group by a.課程號,b.課程名稱;
34.查詢課程編號為0003且課程成績在80分以上的學生的學號和姓名
select student_id,student_name from student where student_id in (select student_id from score where course_id='0003' and score>80);
35.檢索"0001"課程分數小于60,按分數降序排列的學生信息
select t1.*,t2.score from student as t1 left join score as t2 on t1.student_id=t2.student_id where t2.course_id = 0001 and t2.score>60 order by t2.score desc;
36.查詢不同老師所教不同課程平均分從高到低顯示**

注意:select只能是聚合函數或group by后面的字段,否則會報SELECT list is not in GROUP BY clause and contains nonaggregated column的錯誤。原因是sql_mode模式的限制。可以修改這個模式,修改方法:https://blog.csdn.net/weixin_42085125/article/details/115335503

因為我們要查詢t2.course_id,所以要在group by后面加上t2.course_id

select t1.teacher_name,t2.course_id,avg(t3.score) from teacher as t1 inner join course as t2 on t1.teacher_id=t2.teacher_id inner join score as t3 on t2.course_id=t3.course_id group by t1.teacher_id,t2.course_id order by avg(score) desc; 
37.查詢課程名稱為"數學",且分數低于60的學生姓名和分數
行列如何互換

sql面試題:行列如何互換?

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

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

相關文章

前端面試題33(實時消息傳輸)

前端實時傳輸協議主要用于實現實時數據交換&#xff0c;特別是在Web應用中&#xff0c;它們讓開發者能夠構建具有實時功能的應用&#xff0c;如聊天、在線協作、游戲等。以下是幾種常見的前端實時傳輸協議的講解&#xff1a; 1. Short Polling (短輪詢) 原理&#xff1a;客戶…

【1】A-Frame整體介紹

1.A-Frame是什么&#xff1f; A-Frame 是一個用于構建虛擬現實 (VR) 體驗的 Web 框架。 A-Frame 基于 HTML 之上&#xff0c;因此上手簡單。但 A-Frame 不僅僅是 3D 場景圖或標記語言&#xff1b;它還是一種標記語言。其核心是一個強大的實體組件框架&#xff0c;為 Three.js …

Golang | Leetcode Golang題解之第226題翻轉二叉樹

題目&#xff1a; 題解&#xff1a; func invertTree(root *TreeNode) *TreeNode {if root nil {return nil}left : invertTree(root.Left)right : invertTree(root.Right)root.Left rightroot.Right leftreturn root }

AI機器人在未來的應用場景預測:是否會取代人類?華為、百度、特斯拉他們在AI領域都在做什么?

引言 隨著人工智能&#xff08;AI&#xff09;技術的飛速發展&#xff0c;AI機器人在各個領域的應用變得越來越普遍。從工業自動化到日常生活&#xff0c;AI機器人已經開始展現出強大的潛力和實際應用價值。本文將深入探討AI機器人在未來的應用場景&#xff0c;并分析它們是否…

uniapp+vue3嵌入Markdown格式

使用的庫是towxml 第一步&#xff1a;下載源文件&#xff0c;那么可以git clone&#xff0c;也可以直接下載壓縮包 git clone https://github.com/sbfkcel/towxml.git 第二步&#xff1a;設置文件夾內的config.js&#xff0c;可以選擇自己需要的格式 第三步&#xff1a;安裝…

大模型/NLP/算法面試題總結3——BERT和T5的區別?

1、BERT和T5的區別&#xff1f; BERT和T5是兩種著名的自然語言處理&#xff08;NLP&#xff09;模型&#xff0c;它們在架構、訓練方法和應用場景上有一些顯著的區別。以下是對這兩種模型的詳細比較&#xff1a; 架構 BERT&#xff08;Bidirectional Encoder Representation…

【Web前端】JWT(JSON Web Tokens)概述

1、簡介 JWT&#xff08;JSON Web Tokens&#xff09;是一種用于雙方之間安全傳輸信息的簡潔的、URL安全的令牌標準。 它基于JSON對象&#xff0c;并通過數字簽名確保其完整性和真實性。 JWT因其小巧、自包含以及易于在客戶端和服務器之間傳輸的特性而被廣泛使用于身份驗證和…

python字符串驗證從基礎到進階的總結

引言 在數據處理和文本挖掘中&#xff0c;對字符串的驗證是確保數據符合特定要求的關鍵步驟之一。其中一個常見的驗證需求是確認字符串是否只包含字母。Python為此提供了多種實現的方法&#xff0c;我們將逐一討論它們。 方法1&#xff1a;使用 isalpha() 方法 def is_all_l…

UML 2.5圖的分類

新書速覽|《UML 2.5基礎、建模與設計實踐》新書速覽|《UML 2.5基礎、建模與設計實踐 UML 2.5在UML 2.4.1的基礎上進行了結構性的調整&#xff0c;簡化和重新組織了 UML規范文檔。UML規范被重新編寫&#xff0c;使其“更易于閱讀”&#xff0c;并且“盡可能減少前向引用”。 U…

php簡單實現利用飛書群里機器人推送消息的方法

這是一篇利用的飛書的自定義機器人&#xff0c;將系統中的錯誤信息推送給技術群的功能代碼示例。 飛書文檔地址&#xff1a;開發文檔 - 飛書開放平臺 自定義機器人只能在群聊中使用的機器人&#xff0c;在當前的群聊中通過調用webhook地址來實現消息的推送。 配置群邏輯可以看…

LLM應用構建前的非結構化數據處理(三)文檔表格的提取

1.學習內容 本節次學習內容來自于吳恩達老師的Preprocessing Unstructured Data for LLM Applications課程&#xff0c;因涉及到非結構化數據的相關處理&#xff0c;遂做學習整理。 本節主要學習pdf中的表格數據處理 2.環境準備 和之前一樣&#xff0c;可以參考LLM應用構建前…

金蝶部署常見問題解決

金蝶部署常見問題解決 金蝶版本&#xff1a; Apusic Application Server Enterprise Edition 9.0 SP8 kbc build 202312041121 報錯信息&#xff1a; 與金蝶官方人員溝通&#xff0c;發現lib包版本太低&#xff0c;升級后可正常使用。替換lib包后重啟服務。 下載lib: 鏈接: …

西瓜杯CTF(1)

#下班之前寫了兩個題&#xff0c;后面繼續發 Codeinject <?php#Author: h1xaerror_reporting(0); show_source(__FILE__);eval("var_dump((Object)$_POST[1]);"); payload 閉合后面的括號來拼接 POST / HTTP/1.1 Host: 1dc86f1a-cccc-4298-955d-e9179f026d54…

公司內部配置GitLab,通過SSH密鑰來實現免密clone、push等操作

公司內部配置GitLab&#xff0c;通過SSH密鑰來實現免密clone、push等操作。以下是配置SSH密鑰以實現免密更新的步驟&#xff1a; 1.生成SSH密鑰 在本地計算機上打開終端或命令提示符。輸入以下命令以生成一個新的SSH密鑰&#xff1a;ssh-keygen -t rsa -b 4096 -C "your…

VBA實現Excel數據排序功能

前言 本節會介紹使用VBA如何實現Excel工作表中數據的排序功能。 本節會通過下表數據內容為例進行實操&#xff1a; 1. Sort 單列排序 語法&#xff1a;Sort key1,Order1 說明&#xff1a; Key1&#xff1a;表示需要按照哪列進行排序 Order1&#xff1a;用來指定是升序xlAsce…

D2D用戶的功率優化算法研究

D2D通信技術是指兩個對等的用戶節點之間直接進行通信的一種通信方式。在由D2D通信用戶組成的分布式網絡中&#xff0c;每個用戶節點都能發送和接收信號&#xff0c;并具有自動路由(轉發消息)的功能。網絡的參與者共享它們所擁有的一部分硬件資源&#xff0c;包括信息處理、存儲…

短視頻矩陣搭建,用云微客獲客更方便

你的同行都爆單了&#xff0c;你還在問什么是矩陣&#xff1f;讓我來告訴你。短視頻矩陣是短視頻獲客的一種全新玩法&#xff0c;是以品牌宣傳、產品推廣為核心的一個高端布局手段&#xff0c;也是非常省錢的一種方式。 1.0時代&#xff0c;一部手機一個賬號&#xff1b;2.0時代…

demon drone 200無人機標定流程

demon drone 200無人機標定流程 一、飛控固件更新1.1 固件更新1.2 參數更新 二、imu標定2.1 安裝imu標定工具&#xff08;在你自己的電腦上&#xff09;2.2 錄制rosbag(在對應飛機上)2.3 運行標定程序&#xff08;在你自己的電腦上&#xff09; 三、雙目及imu聯合標定3.1 安裝標…

mysql索引筆記

這里想整理一下性能優化中用到的東西&#xff0c;先整理一下優化mysql索引中所查閱到的資料吧。 目錄 MySQL索引類型詳解存儲方式區分1.B樹索引2.哈希索引 邏輯區分1.普通索引2. 唯一索引3. 主鍵索引4. 空間索引5. 全文索引 實際使用區分1. 單列索引2. 多列索引 多表聯查如何建…

GD 32中斷系統實現

1.0 中斷的概念 中斷&#xff1a;簡單來說就是打斷的意思&#xff0c;在計算機系統中CPU在執行一個操作的時候&#xff0c;有一個比當前任務更為緊急的任務需要執行,cpu暫停當前任務轉而去執行更為緊急任務的操作&#xff0c;執行完更為緊急任務之后再返回來執行原來未執行完的…