mysql sql语句面试经典50题_常见的SQL面试题:经典50题(简单)
目錄
一、簡單查詢
1. 查詢姓“猴”的學生名單
2.查詢姓“孟”老師的個數
二、匯總分析
1.查詢課程編號為“0002”的總成績
2.查詢選了課程的學生人數
3.查詢各科成績最高和最低的分, 以如下的形式顯示:課程號,最高分,最低分
4.查詢每門課程被選修的學生數
5.查詢男生、女生人數
6.查詢平均成績大于60分學生的學號和平均成績
7.查詢至少選修兩門課程的學生學號
8.查詢同名同姓學生名單并統計同名人數
9.查詢不及格的課程并按課程號從大到小排列
10.查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列
三、復雜查詢
1.查詢所有課程成績小于60分學生的學號、姓名
2.查詢沒有學全所有課的學生的學號、姓名
3.查詢出只選修了兩門課程的全部學生的學號和姓名
4.日期函數
5.查詢各科成績前兩名的記錄(重要)
6.【行轉列、列轉行】問題(重要)
(1)行轉列
(2)列轉行
(3)單列拆分轉行
題目來源:知乎猴子 常見的SQL面試題:經典50題
一、簡單查詢
1. 查詢姓“猴”的學生名單
select *
from student
where sname like "猴%"
2.查詢姓“孟”老師的個數
select count(*)
from teacher
where tname like "孟%"
二、匯總分析
1.查詢課程編號為“0002”的總成績
select sum(score)
from score
where cid="0002"
2.查詢選了課程的學生人數
select count(distinct sid)
from score
where cid is not null and score is not null
3.查詢各科成績最高和最低的分, 以如下的形式顯示:課程號,最高分,最低分
select cid, max(score),min(score)
from score
group by cid
4.查詢每門課程被選修的學生數
select cid,count(distinct sid)
from score
group by cid
5.查詢男生、女生人數
select ssex,count(sid)
from student
group by ssex
6.查詢平均成績大于60分學生的學號和平均成績
select sid, avg(score) as average
from score
group by sid
having average>60
7.查詢至少選修兩門課程的學生學號
select sid,count(distinct cid)
from score
group by sid
having count(distinct cid)>=2
8.查詢同名同姓學生名單并統計同名人數
select sname, count(sid)
from student
group by sname
having count(sid)>=2
9.查詢不及格的課程并按課程號從大到小排列
select distinct cid
from score
where score<60
order by cid desc
10.查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列
select cid, avg(score) as average
from score
group by cid
order by average asc, cid desc
后面幾題太簡單了...不寫了
三、復雜查詢
1.查詢所有課程成績小于60分學生的學號、姓名
select s.sid, s.sname
from student s
where s.sid in(
select distinct sid
from score
where score<60)
2.查詢沒有學全所有課的學生的學號、姓名
select sid,sname
from student
where sid in(
select sid
from score
group by sid
having count(distinct cid)
3.查詢出只選修了兩門課程的全部學生的學號和姓名
select sid,sname
from student
where sid in(
select sid
from score
group by sid
having count(distinct cid)=2)
4.日期函數
5.查詢各科成績前兩名的記錄(重要)
詳細見mysql分組取TOP N個的問題
6.【行轉列、列轉行】問題(重要)
(1)行轉列
下面是學生的成績表(表名score,列名:學號、課程號、成績)
使用sql實現將該表行轉列為下面的表結構
思路:使用case when,具體思路參考文章sql面試題:行列如何互換?
select sid,
max(case when cid='0001' then score else 0 end) as '課程號0001',
max(case when cid='0002' then score else 0 end) as '課程號0002',
max(case when cid='0003' then score else 0 end) as '課程號0003'
from score
group by sid
不要忘記最后要group by
(2)列轉行
原數據參考文章?MySQL行轉列與列轉行
要求轉化成:
select user_name, '語文' as course, CN_SCORE as score from GRADE
union all
select user_name, '數學' as course, MATH_SCORE as score from GRADE
union all
select user_name, '英語' as course, EN_SCORE as score from GRADE
(3)單列拆分轉行
思路:(1)先建立一個序列表 tb_sequence
--創建自動遞增的序列表
create table tb_sequence if not exists(id int auto_increment not null, primary key(id));
--插入數值,這里插入的個數=列拆分后的行數
insert into table tb_sequence values(),(),(),(),(),(),(),();
(2) 計算每一條記錄將會拆分成多少行,得出size。計算方法:逗號的個數+1,逗號個數可以用(length(mobile)-length(replace(mobile, ',' , '')))/length(',') 得出。
再將得出size的原表與tb_sequence做cross join,選取id小于size取值的行,其實就是占坑。
select *
from tb_sequence a
cross join
(select b.*,((length(mobile)-length(replace(mobile, ',' , '')))/length(',')+1) as size
from user1 b) b on a.id<=b.size
--備注:原文不知道為什么mobile要重新concat,附上原文的代碼:
select *
from tb_sequence a
cross join
(select user_name,concat(mobile,','),((length(mobile)-length(replace(mobile, ',' , '')))/length(',')+1) as size
from user1 b) b on a.id<=b.size
上面這一段代碼輸出結果:
(3)最后就是進行字符串的處理
總結
以上是生活随笔為你收集整理的mysql sql语句面试经典50题_常见的SQL面试题:经典50题(简单)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL经典面试50题 | 附答案
- 下一篇: oracle面试上机题,Oracle面试