문제
Query the Name of any student in STUDENTS who scored higher than 75 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.
Input Format
The STUDENTS table is described as follows:
The Name column only contains uppercase (A-Z) and lowercase (a-z) letters.
Sample Input
Sample Output
Ashley Julia Belvet
Explanation
Only Ashley, Julia, and Belvet have Marks > 75 . If you look at the last three characters of each of their names, there are no duplicates and 'ley' < 'lia' < 'vet'.
코드
SELECT name
FROM Students
WHERE marks > 75
ORDER BY substr(name,-3), id /*두번째 정렬 기준은 콤마(,)찍고 기재*/
풀이
- STUDENTS 테이블에서 name만 조회
- 점수가 75 초과
- name의 끝부터 세자리 글자 순으로 정렬
- 단 name의 끝부터 세자리 글자가 같을 경우 ID를 오름차순으로 정렬
더보기
SUBSTR(문자열, 시작위치, 길이)
문자열을 잘라서 추출하는 함수. 문자 단위로 자를 때 사용
-- SUBSTR(문자열, 시작위치, 길이)
SELECT name, substr(name, -3) /*끝의 3번째부터 끝까지라서 뒤에 길이 생략*/
FROM Students
-- 결과
-- Stuart | art
-- Kristeen | een
-- Christene | ene
SELECT name, substr(name, 1, 2) /*1번째 자리부터 2번째 자리까지 추출*/
FROM Students
-- 결과
-- Stuart | St
-- Kristeen | Kr
-- Christene | Ch
LEFT(문자열, 문자열의 길이)
SELECT LEFT("20210628", 4)
-- 결과
-- 2021
RIGHT(문자열, 문자열의 길이)
SELECT RIGHT("20210628", 4)
-- 결과
-- 0628
반응형
'Algorithm > MySQL' 카테고리의 다른 글
[프로그래머스] 이름이 없는 동물의 아이디 (MySQL) (0) | 2021.06.29 |
---|---|
[해커랭크(HackerRank)] Weather Observation Station 15 (MySQL) (0) | 2021.06.28 |
[해커랭크(HackerRank)] Employee Salaries (MySQL) (0) | 2021.06.28 |
[해커랭크(HackerRank)] Employee Names (MySQL) (0) | 2021.06.28 |
[해커랭크(HackerRank)] Weather Observation Station 12 (MySQL) (0) | 2021.06.28 |