iT邦幫忙

2024 iThome 鐵人賽

DAY 18
0

以下兩題屬於easy類型題目,基本上是使用基本語法做資料查詢。

題目一 Select By ID

Query all columns for a city in CITY with the ID 1661. The CITY table is described as follows:

Field Type
ID Number
Name VARCHAR2(17)
COUNTRYCODE VACHAR2(3)
DISTRICT VARCHAR2(20)
POPULATION NUMBER

【解題邏輯】
只需要使用where指定條件來作答

    -- Oracle
    select * from CITY C
    where C.ID=1661
    ;  -- 在HackerRank的撰寫環境中, Oracle結尾必須存在';', MySQL則不用
    -- MySQL
    select * from CITY C
    where C.ID=1661
    
    >>> 輸出: 1661 Sayama JPN Saitama 162472

題目二 Higher Than 75 Marks

Query the Name of any student in STUDENTS who scored higher than Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.

Column Type
ID Interger
Name String
Marks Interger

【解題邏輯】
使用order by字串處理substr取倒數三個字元 來作答

    -- Oracle
    select s.name
    from students s
    where 1=1
    and s.marks>75
    order by substr(s.name, -3, 3), ID
    ;
    -- MySQL
    select s.name
    from students s
    where 1=1
    and s.marks>75
    order by substr(s.name, -3, 3), ID    

Referece

如何重後面取字元


上一篇
Day 17 基礎-樞紐分析
下一篇
Day 19 實戰-Hackerrank Medium篇
系列文
不居功的系統隱士 - 30天由淺入深學SQL26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言