728x90

https://leetcode.com/problems/game-play-analysis-i/

 

[problem]

[analysis]

1. eventdate가 여러 데이터가 있으므로 min을 이용

2. id group by

 

[solution]

728x90
728x90

https://leetcode.com/problems/customers-who-never-order/

 

Customers Who Never Order - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

[problem]

Write an SQL query to report all customers who never order anything.

Return the result table in any order.

 

[analysis]

1. 두 테이블 left join

2. cutomnerID가 null인 경우 where을 사용해서 리스트 출력해야함

join 과 where은 같이 사용할 수 있따!!

 

[soultion]

728x90
728x90

https://www.hackerrank.com/challenges/weather-observation-station-2/problem

 

Weather Observation Station 2 | HackerRank

Write a query to print the sum of LAT_N and the sum of LONG_W separated by space, rounded to 2 decimal places.

www.hackerrank.com

 

[problem]

[analysis]

1. SUM 이용

2. ROUND(컬럼명/값,n) 값을 소수점이하 n자리수로 반올림

 

[solution]

728x90

'MS SQL > MS SQL : HackerRank' 카테고리의 다른 글

MS SQL- HackerRank Japan Population(sum, where)  (0) 2022.04.25
728x90

https://www.hackerrank.com/challenges/japan-population/problem

 

Japan Population | HackerRank

Query to the sum of the populations of all Japanese cities in CITY.

www.hackerrank.com

[problem]

[analysis]

population 기준으로 sum 하고 countycode 조건 1개가 존재함

 

[solution]

728x90
728x90

https://leetcode.com/problems/not-boring-movies/

 

Not Boring Movies - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

[problem]

1. 홀수인경우

2. description 에 boring이 들어간 경우 제외

3. order by DESC 적용

[analysis]

1.where 절을 통한 조건 적용

--> 홀수인경우 id % 2 !=0 으로 나타낼 수 있음 / boring문자 제외도 !를 이용함

[solution]

728x90
728x90

https://leetcode.com/problems/combine-two-tables/

 

Combine Two Tables - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

[problem]

두 테이블간의 join 필요

Write an SQL query to report the first name, last name, city, and state of each person in the Person table. If the address of a personId is not present in the Address table, report null instead.

Return the result table in any order.

[analysis]

1. address가 null인 경우도 정보를 보여줘야 하므로 person table 기준으로 left join 실행

2. 두 테이블에 공통 컬럼인 personId 기준으로 join

 

[solution]

728x90
728x90

https://leetcode.com/problems/duplicate-emails/submissions/

 

Duplicate Emails - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

[problem]

email데이터 중복없이 정보 출력하기

[analysis]

1. 중복된 이메일 제외 위해서 email로 group by 해줌

2. having count email >1 (2이상이면 중복임)

* having은 group by에서 연산한 결과물에 조건을 줄 때 사용 where과는 다르다.

 

[solution]

728x90
728x90

CASEWHEN
>문법

CASE WHEN 조건절 THEN 참일때 값 ELSE 거짓일 때 값 END 컬럼명

>예제

테이블에서 user값이 0이면 정회원, 그 외엔 준회원일 때

SELECT DISTINCT 
USER,
CASE WHEN USER='0' THEN '정회원'  ELSE '준회원' END 회원상태
FROM USER_TABLE

>다중으로도 사용 가능함

SELECT *,
   (CASE WHEN SCORE>= '90' THEN 'A학점'
        WHEN (SCORE>= '80' AND SCORE < '90') THEN 'B학점'
        WHEN (SCORE>= '70' AND SCORE < '80') THEN 'C학점' 
        WHEN (SCORE>= '60' AND SCORE < '70') THEN 'D학점'
        ELSE 'F학점'
    END) AS '학점'

FROM MY_TABLE

728x90
728x90

GETDATE()
DB에서 현재 시간을 가져오는 함수

> 형식

SELECT GETDATE()

> SELECT 절 뿐 아니라 WEHRE절 등 현재시간 필요한 곳에서 사용 가능함

SELECT *
FROM TABLE A
WHERE TIME_STAMP < GETDATE()

> YEAR MONTH DAY 따로 출력도 가능함

SELECT 
YEAR(GETDATE()) AS 년도,
MONTH(GETDATE()) AS 월
DAY(GETDATE()) AS 일,
CONVERT(TIME,GETDATE()) AS 시간

 

DATEADD
특정 날짜와 숫자값 계산해주는 함수

>형식

SELECT DATEADD(날짜형식, 값, 날짜)

 

> D, MONT, YEAR
1) SELECT DATEADD(D,5, '2022-01-1') AS '+5일'
 -> SELECT DATEADD(D,5, GETDATE()) AS '+5일'  getdate도 함께 사용가능

2) SELECT DATEADD(MONTH,5, '2022-01-1') AS '+5달'

SELECT DATEADD(YEAR ,5, '2022-01-1') AS '+5년'

 

728x90
728x90

TRIM은 쿼리에서 공백을 제거해주는 함수이다. 데이터베이스에 따라 함수가 다르다고 함

- MySQL: TRIM( ), RTRIM( ), LTRIM( )
- Oracle: RTRIM( ), LTRIM( )
- SQL Server: RTRIM( ), LTRIM( )

 

MS SQL에 적용되는 함수

 

TRIM : 문자열 앞쪽, 뒷 공백을 전부 제거

예제 > TRIM('       ABCDEF          ')

결과 >'ABCDEF'

 

LTRIM : 문자열 앞쪽 공백을 전부 제거

예제 >TRIM('       ABCDEF          ')

결과 >'ABCDEF     '

 

RTRIM : 문자열 뒷쪽 공백을 전부 제거

예제 >TRIM('       ABCDEF          ')

결과 >'     ABCDEF'

 

REPLACE('문자열', '치환할 문자열', '치환될 문자열')

예제 > SELECT REPLACE('ABCDEF','ABC','AAA');
결과 > 'AAADEF'

728x90

+ Recent posts