본문 바로가기

SQL 공부

HackerRank로 SQL 공부하기 Basic select

728x90
  1. Weather Observation Station 3

 

ID번호가 짝수인 도시 추출 (even number가 짝수구나 영어공부부터 다시 해야될듯 ㅎㅎ...)

도시 이름 조회 중복된 항목은 답변에서 제외

 

select distinct(city)
from station
where id%2 = 0

 

영어 해석을 못해서 틀림 ㅠ

 

  1. Weather Observation Station 4

표의 전체 도시 항목 수와 표의 중복된 도시 항목 수 사이의 차이를 구하여라.

 

select count(city) - count(distinct(City))
from station

 

0

 

이름이 가장 긴 도시와 가장 짧은 도시 추출

도시명, 도시명 길이 조회

도시명 기준 오름차순 정렬

 

select city, length(city)
from station
where length(city) = (select min(length(city))from station)
order by city asc
limit 1;

select city, length(city)
from station
where length(city) = (select max(length(city))from station)
order by city asc
limit 1;

 

다시

 

STATION의 모음(예: a, e, i, o, u)으로 시작하는 CITY 이름 목록을 조회.

결과에 중복된 이름을 포함할 수 없다.

select distinct(city)
from station
where left(city,1) in ('i','e','a','o','u')

 

 

STATION에서 모음으로 시작하지 않는 CITY 이름 목록을 조회.

결과에 중복된 이름을 포함할 수 없다.

select distinct(city) from station
where left(city,1) not in ('i','a','e','o','u')

 

 

75점 초과한 학생 추출

이름의 끝에서 3개의 철자를 기준으로 오름차순, 같다면 id 기준 오름차순

select name
from students
where marks > 75
order by right(name,3)asc, id asc