Kodewick
学びはじめる →
基礎←→0 / 70

成績を文字で

#056基礎CASE WHEN … WHEN約 4 分

先生が数学の点数を文字で表したいそうです。90 以上は 'A'、75 以上は 'B'、60 以上は 'C'、それ以外は 'D' です。scores から数学の行(subject が 'math')を選び、student_id、score、文字の 3 列を出してください。

使うテーブル
students
  • idPK
  • name
  • class
  • age
  • city
scores
  • idPK
  • student_idFK
  • subject
  • score

scores.student_id → students.id

students
idnameclassagecity
1Amy1A12Leeds
2Ben1A13York
3Cara1B12NULL
4Dan1B14Leeds
5Eva1A12Hull
6Finn1C13NULL
7Gus1C12York
7 行
scores
idstudent_idsubjectscore
11math92
21art78
32math65
42art88
53math74
64math55
74art90
85math81
95artNULL
107math99
10 行
query.sql
SELECT student_id, score,
  CASE
    -- ここにコードを書く:高い順に WHEN を 3 つ(90、75、60)、最後に ELSE 'D'
  END
FROM scores
WHERE subject = 'math';
Ctrl + Enter でも実行できます
結果

実行するとクエリの結果が表示されます。よさそうなら提出しましょう。