以下兩題屬於easy類型題目,基本上是使用基本語法做資料查詢。
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
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