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