SELECT
ai.animal_id
, ai.name
FROM
animal_ins ai
JOIN animal_outs ao
ON ai.animal_id = ao.animal_id
ORDER BY
DATEDIFF(ao.datetime, ai.datetime) DESC
LIMIT 2
;
WITH in_out_rank AS (
SELECT
ai.animal_id
, ai.name
, RANK() OVER (ORDER BY DATEDIFF(ao.datetime, ai.datetime) DESC) AS date_rank
FROM
animal_ins ai
JOIN animal_outs ao
USING(animal_id)
ORDER BY
date_rank
)
SELECT
animal_id
, name
FROM
in_out_rank
WHERE
date_rank < 3
LIMIT 2
;
SELECT
name
, population
, area
FROM
world
WHERE
area >= 3000000
OR population >= 25000000
;
select name,population,area
from World
where population >=25000000
UNION
select name,population,area
from World
where area >=3000000;
SELECT
DISTINCT author_id AS id
FROM
views
WHERE
author_id = viewer_id
ORDER BY
author_id
;
SELECT
author_id AS id
FROM
views
WHERE author_id = viewer_id
GROUP BY author_id
ORDER BY author_id
;
select distinct author_id as id
from Views
where author_id = viewer_id
order by id asc;
SELECT
tweet_id
FROM
tweets
WHERE
CHAR_LENGTH(content) > 15
;
SELECT
tweets.tweet_id
FROM
tweets
WHERE
LENGTH(tweets.content) > 15
LENGTH 함수는 문자의 Byte 길이를 가져옵니다.
CHAR_LENGTH(CHARACTER_LENGTH) 함수는 문자의 Byte 수를 계산하지 않고 단순히 몇 개의 문자가 있는지를 가져옵니다.
SELECT LENGTH('안녕');
→ 결과: 6
SELECT CHARACTER_LENGTH('안녕');
→ 결과: 2
def solution(arr, divisor):
answer = []
arr.sort()
for i in arr:
if i % divisor == 0:
answer.append(i)
elif i % divisor != 0:
pass
return answer or [-1]
return하려는 값이 null일 때(list로는 []일 때) or B를 쓰면 B를 return함!
꽤 유용한 코드 방식이니 꼭 기억할 것
def solution(arr, divisor):
return sorted([i for i in arr if i%divisor == 0]) or [-1]
def solution(arr, divisor):
answer = []
for i in arr:
if i % divisor == 0:
answer.append(i)
if answer == []:
return [-1]
answer.sort()
return answer
for i in range(len(arr)):
if arr[i] % divisor == 0:
answer.append(arr[i])
def solution(arr, divisor):
arr = [x for x in arr if x % divisor == 0];
arr.sort();
return arr if len(arr) != 0 else [-1];