Answer :
select product_id
from products
where low_fats = 'Y' and recyclable = 'Y'
SQL은 항상 (sub query는 예외)
select ...
from ...
where ...
group by ...
order by ...
having ... 순서의 형식을 가진다
Answer :
select name
from Customer
where referee_id != 2 or referee_id is null
SQL에서 not equal는 python이랑 똑같음 !=
Answer :
select name, population, area
from World
where area >= 3000000 or population >= 25000000
문제 설명에 어쩔땐 at least 가 있는데 이는 최소 이기 때문에 무조건 = 가 들어간다.
Answer :
select distinct(author_id) as id
from views
where viewer_id = author_id
order by author_id asc
SQL 문법 중에 order by가 있음, 정렬 하는 용도 임으로 기본 값이 ascending (ASC)임, 또는 내림차순 (Descending (DESC)도 가능)
Answer :
select tweet_id
from Tweets
where length(content) > 15
문제 설명에 어쩔땐 greater than 이 있는데 이는 ~ 보다 크다를 의미함으로 > 가 들어간다.
Answer :
select eu.unique_id, e.name
from employees as e
left join employeeUNI as eu
on e.id = eu.id
딱히 어려운거 없는듯, 굳히 말하면 join, left join 등 알아두기 ?
Answer :
select p.product_name, s.year, s.price
from sales as s
left join product as p
on s.product_id = p.product_id
이전 문제랑 같음
Answer :
select v.customer_id, count(*) as count_no_trans
from visits as v left join transactions as t on v.visit_id = t.visit_id
where t.transaction_id is NULL
group by v.customer_id
join 을 하면은 매치 안되는것 들은 NULL 이 생성된 다는거 잊지말기, 문제의 주요점은 방문했지만 물건을 안 산 횟수를 구하는게 문제이다.
Answer :
select w2.id
from weather as w1, weather as w2
where datediff(w2.recorddate, w1.recorddate) = 1 and w2.temperature > w1.temperature
datediff 사용하는 방법 알기 !, 그리고 SQL에서는 날짜를 그냥 '20xx-xx-xx' < '2025-03-7' 이렇게 비교 할 수 있음.
Answer :
select a1.machine_id, round(avg(a2.timestamp - a1.timestamp), 3) as processing_time
from activity as a1, activity as a2
where a2.machine_id = a1.machine_id and a2.activity_type = 'end' and a1.activity_type = 'start'
group by a1.machine_id
해당 문제는 round , avg , timestamp 사용하는 방법 알기 !
Answer :
select e.name, b.bonus
from employee as e left join bonus as b on e.empID = b.empID
where b.bonus < 1000 or b.bonus is NULL
해당 문제는 less than 은 < 를 의미함. 반대로 greater than 은 > 를 의미함
Answer :
select st.student_id, st.student_name, su.subject_name, count(ex.subject_name) as attended_exams
from students as st
cross join subjects as su
left join examinations as ex on st.student_id = ex.student_id and su.subject_name = ex.subject_name
group by st.student_id, su.subject_name
order by st.student_id, su.subject_name
해당 문제는 cross join 을 알아야함. 두가지 table인 Students table:, Subjects table:를 합치고 group by를 통해 묶어주고 계산하면 끝.
Answer :
select e.name
from employee as e
where e.id IN(
select em.managerId
from employee as em
group by em.managerId
having count(em.managerId) >= 5
)
해당 문제는 sub query 를 배우는 문제로 where 문에 또 다른 query를 사용해 조건을 추출해내는 것을 알 수 있다. group 문 다음에는 having 까먹지말기, where 아님.