본문 바로가기

SQL

SQL 실전! 실제 DB에서 연습해요(1)

Lv1. 데이터 속 김서방 찾기

select count(1)
from users
where substr(name, 1, 1) = '김'

 

Lv2. 날짜별 획득포인트 조회하기

select created_at, round(avg(point)) average_points
from point_users
group by created_at

 

Lv3. 이용자의 포인트 조회하기

select u.user_id,
		u.email,
		coalesce(p.point, 0) point
from users u
left join point_users p on u.user_id = p.user_id
order by point desc

select u.user_id,
		u.email,
		if(p.point is null, 0, p.point) point
from users u
left join point_users p on u.user_id = p.user_id
order by point desc