뭉지(moonz)
작은 발자국들의 위대한 여정
뭉지(moonz)
  • All (202)
    • Test Code (4)
    • 백엔드 개발하며 작성한 (27)
      • Spring (17)
      • 데이터베이스 (7)
      • 기억할 내용 (3)
    • 언어 (53)
      • Java (25)
      • node.js (7)
      • Python (21)
    • 클라우드 (6)
    • Algorithm & Data Structure (51)
      • 개념 (15)
      • 문제 풀이 (36)
    • 유용한 모든 것 (16)
    • monologue (7)
      • 업무 노트 (1)
      • 관리 로그 (0)
      • 내 이야기 공책 (6)
    • Project (2)
    • TroubleShooting (8)
    • 지식 (18)
      • Machine Learning (6)
      • Review (7)
      • Web (5)
    • Computer Science (5)

블로그 메뉴

  • 홈
  • 태그

인기 글

최근 글

최근 댓글

전체 방문자
오늘
어제

티스토리

hELLO · Designed By 정상우.
뭉지(moonz)

작은 발자국들의 위대한 여정

[HackerRank] SQL 문제
Algorithm & Data Structure/문제 풀이

[HackerRank] SQL 문제

2022. 6. 26. 00:54
반응형

The STATION table is described as follows:

문제 1. weather-observation-station-3 link

Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.
where LAT_N is the northern latitude and LONG_W is the western longitude.

 

풀이

중복되지 않는 결과를 output하기 위해서는 SELECT 뒤에 DISTINCT를 붙입니다.

SELECT DISTINCT CITY 
FROM STATION
WHERE ID % 2 = 0;

 

 

 

문제2. weather-observation-station-5  link

Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
The STATION table is described as follows:

 

풀이

CITY 길이의 최댓값과 최댓값은 ORDER BY로 내림차순, 오름차순 정렬하여 가장 위에있는 1개(LIMIT)를 고르도록 했고, 이때 정렬이 되는 기준은 1. CITY 길이 2. CITY 알파벳 순이었습니다.

 

먼저 위 방식으로 최댓값(혹은 최솟값) 1개를 조회되도록 select를 하여 테이블을 만들고,

그 테이블에서 문제의 output을 select하도록 했습니다.

SELECT a.CITY, LENGTH(a.CITY) 
FROM (
    SELECT CITY
    FROM STATION
    ORDER BY LENGTH(CITY), CITY
) a
LIMIT 1;

SELECT a.CITY, LENGTH(a.CITY) 
FROM (
    SELECT CITY
    FROM STATION
    ORDER BY LENGTH(CITY) DESC, CITY
) a
LIMIT 1;
반응형
저작자표시 (새창열림)

'Algorithm & Data Structure > 문제 풀이' 카테고리의 다른 글

[SQL, 프로그래머스] 상품 별 오프라인 매출 구하기  (1) 2024.06.09
[SQL, 프로그래머스] 조건에 맞는 도서와 저자 리스트 출력하기  (1) 2024.06.09
[JAVA] baekjoon 2447 별 찍기-10  (0) 2022.06.15
[JAVA] baekjoon 1891 사분면  (0) 2022.06.13
[JAVA, 백준] 16947. 서울 지하철 2호선  (0) 2022.04.18
    'Algorithm & Data Structure/문제 풀이' 카테고리의 다른 글
    • [SQL, 프로그래머스] 상품 별 오프라인 매출 구하기
    • [SQL, 프로그래머스] 조건에 맞는 도서와 저자 리스트 출력하기
    • [JAVA] baekjoon 2447 별 찍기-10
    • [JAVA] baekjoon 1891 사분면
    뭉지(moonz)
    뭉지(moonz)
    제가 깨달은 것을 정리하는 공간입니다. 🧡

    티스토리툴바