SQL問題(面試題)

2021-09-07 09:37:53 字數 1088 閱讀 7438

面試完後在本地mysql資料庫中重現了該問題

資料表stuscore資訊如下:

1、計算每個人的總成績,並且排名(要求顯示字段 學號 姓名 總成績)

select stuid as 學號,name as 姓名, sum(score) as 總成績

from stuscore group by stuid order by 總成績 desc

2、計算每個人單科的最高成績(要求顯示字段 學號、姓名、課程、最高成績)

select stuid as 學號,name as 姓名,subject as 課程,score as 最高成績

from stuscore a where score=(select max(score) from stuscore where subject=a.subject)

3、統計如下:課程 不及格(0-59)個 良(60-80)個 優(81-100)個

select subject as 課程,count(case when score<60 then 1 end) as "不及格(0-59)個",

count(case when score between 60 and 80 then 1 end) as "良(81-100)個",

count(case when score between 81 and 100 then 1 end) as "優(81-100)個"

from stuscore group by `subject`

4、統計如下:課程 張三分數 李四分數 王五分數

select subject as 課程,sum(case when `name` = "張三" then score else 0 end) as "張三分數",

sum(case when `name` = "李四" then score end) as "李四分數",

sum(case when `name` = "王五" then score end) as "王五分數"

from stuscore group by `subject`

SQL面試題 (二)

sql面試題 二 有一張工資表,包含三列 員工編號 id 部門編號 groups 工資 salary 1.找到每個部門工資最高的人 包括並列第一 2.找到每個部門工資最高的人 只選乙個 sql語句如下 declare g table id int,groups nvarchar 20 salary ...

sql 查詢面試題

表中有a b c三列,用sql語句實現 當a列大於b列時選擇a列否則選擇b列,當b列大於c列時選擇b列否則選擇c列 if object id testtb is not null drop table testtb gocreate table testtb a int b int c int in...

SQL面試題(八)

問題 用一條sql語句查詢 出每門課 都大於80 分的學生姓名 name kecheng fenshu 張三 語文81 張三數學 75 李四語文 76 李四 數學90 王五語 文81 王五數學 100 王五英 語90 建表 drop table if exists studentinfo creat...