sql

[SQL][HackerRank] Occupations
https://www.hackerrank.com/challenges/occupations/problem?isFullScreen=true Occupations | HackerRank Pivot the Occupation column so the Name of each person in OCCUPATIONS is displayed underneath their respective Occupation. www.hackerrank.com Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output col..
[SQL] ROW_NUMBER / RANK / DENSE_RANK
ROW_NUMBER, RANK, 그리고 DENSE_RANK는 모두 각 객체의 순위를 표현하는데 나타내는 함수이다. 예를들어, 아래와 같이 학생과 학생의 성적이 담긴 테이블이 있다고 하자. 우리는 여기서 학생들의 순위를 알고 싶다. NAME SCORE 일동준 97 이동준 76 삼동준 37 사동준 99 오동준 88 그럴때는 아래와 같이 쿼리를 작성한다. 각 함수를 써주고, OVER(ORDER BY 정렬기준)을 입력하면 기준에 맞게 정렬된 결과를 출력해준다 (정렬기준 뒤에 DESC를 기입하면 내림차순 기준으로 순위를 매긴다). SELECT name, score, ROW_NUMBER() OVER(ORDER BY score DESC) seq FROM students SELECT name, score, RANK()..

[SQL] NESTED CASE
어떤 조건에 따라 특정 값을 출력하고 싶을때 CASE WHEN을 이용한다. 예를 들어, employee 테이블이 존재한다고 하자. 테이블에는 사원의 ID, 이름, 그리고 부서 필드가 존재한다. 이때, 회사에서 영업부가 더이상 필요없어져서, 영업부 사람들을 해고하고자 한다. ID NAME DEPARTMENT 1 일동준 영업부 2 이동준 인사부 3 삼동준 영업부 4 사동준 총무부 이때, 누가 해고당하는지 파악하기 위해서 CASE WHEN을 이용할 수 있다. 문법은 아래와 같다. CASE WHEN 조건 THEN 출력 ELSE 조건이 아닐때 출력 END 이를 이용해, 다음과 같이 쿼리를 작성하면 아래와 같은 결과를 얻게 된다. SELECT ID, CASE WHEN department = '영업부' THEN '해..
[SQL] COUNT()와 NULL
https://www.hackerrank.com/challenges/weather-observation-station-4/problem?isFullScreen=true Weather Observation Station 4 | HackerRank Find the number of duplicate CITY names in STATION. www.hackerrank.com Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table. 문제에서는 테이블을 구성하는 총 city(중복허용)의 갯수와 고유 city의 갯수의 차를 출력하길 원한다. 쿼리..
[SQL] 조건에 맞는 숫자 탐색
https://www.hackerrank.com/challenges/weather-observation-station-3/problem Weather Observation Station 3 | HackerRank Query a list of unique CITY names with even ID numbers. www.hackerrank.com 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. 문제에서는 짝수 ID를 가지는 도시의 이름을 출력하길 원한다. 이때, 출력 결과는 중복을 허용..
[SQL] REGEXP
https://indistract.tistory.com/13 [SQL] 조건에 맞는 문자열 검색 https://www.hackerrank.com/challenges/weather-observation-station-8/problem?isFullScreen=true Weather Observation Station 8 | HackerRank Query CITY names that start AND end with vowels. www.hackerrank.com Query the list of CITY names from STATION which hav indistract.tistory.com 위 게시글에서는 조건에 맞는 문자열을 검색하는 방법을 다뤘다. LEFT(), RIGHT() 함수를 이용할수도 있지만..
[SQL] 조건에 맞는 문자열 검색
https://www.hackerrank.com/challenges/weather-observation-station-8/problem?isFullScreen=true Weather Observation Station 8 | HackerRank Query CITY names that start AND end with vowels. www.hackerrank.com Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates. 먼저, 문제에서는 모음으로 시작하고, 모음으로 끝..