Kodewick
開始學 →
小挑戰←→0 / 70

最老的兩隻貓

#022小挑戰ORDER BY DESC + LIMIT約 5 分鐘

醫生想給年紀最大的兩隻貓做體檢。從 pets 表裡只看 kind 是 'cat' 的,選出 name 和 age,按 age 從大到小排,只要前 2 行。

用到的表
owners
  • idPK
  • name
  • city
  • phone
pets
  • idPK
  • name
  • kind
  • age
  • weight
  • owner_idFK

pets.owner_id → owners.id

owners
idnamecityphone
1LenaOslo555-0101
2MarcoRomeNULL
3PriyaOslo555-0103
4OmarCairoNULL
5SofiaRome555-0105
5 行
pets
idnamekindageweightowner_id
1Biscuitdog3121
2Mochicat541
3Pepperdog8202
4Nemofish1NULL3
5Lunacat233
6Rexdog1130NULL
7Kiwibird4NULL5
8Tofucat75NULL
8 行
query.sql
-- 在這裡寫:先 WHERE 留下貓,再按 age 從大到小排,最後只要 2 行
SELECT name, age
FROM pets;
Ctrl + Enter 也能執行
結果

寫好了先點「執行」看看結果,沒問題再點「提交」。