select *
from users
where user_id = 1
;
select ~: ~를 조회
*: 전체
from: 불러올 db
where: 무엇을 불러올지
where registration_date between '2022-01-01' and '2022-12-31'
where user_id in (1, 9, 21)
where username like 'p%'
where username is null
and,or,not=을 하나만 사용in 사용null을 찾을때는 = null대신 is null사용like '~%'
: ~로 시작하는 것을 찾음
select *
from EMPLOYEES
cross join DEPARTMENTS
;
cross join: 그대로 매칭시켜서 싹다 갖다붙임. 쓸데없이 중복되는 내용 다수. 찾기도 힘들어짐
SELECT E.id, E.name, D.name
FROM EMPLOYEES E
JOIN DEPARTMENTS D
ON E.dept_id = D.id
;
코드가 길어지니 별칭 사용!
on으로 필터링 해서 매칭
SELECT U.user_id, U.username, UP.bio
FROM USERS U
LEFT OUTER JOIN USER_PROFILES UP
ON U.user_id = UP.user_id
ORDER BY U.user_id
;
LEFT OUTER JOIN: 왼쪽 전부 보여줌, outer 생략 가능
ORDER BY U.user_id: 유저 id를 기준으로 정렬, asc를 붙여서 오름차순으로 정렬