SQL 별칭 기능 alias

2022. 12. 22. 21:17SQL

alias는 쿼리가 길어지고 헷갈리는 것을 방지하기 위해 사용된다. 

 

select * from orders o
inner join users u
on o.user_id = u.user_id

-> orders에는 별칭 ' o ' 가 생기고 users에는 별칭 ' u ' 가 생겨서 order.user_id 이렇게 하지 않아도 o.user_id로 간편하게 지칭할 수 있다. 

->별칭은 한 글자에서 두글 자 내지로 보통 지정한다고 한다. 

 

select payment_method , count(*) as cnt from orders o
inner join users u
on o.user_id = u.user_id

group by payment_method

-> count(*) as cnt  'as'를 이용하여 별칭을 지정할 수 있다. 출력물의 제목이 바뀐다. count(*)로 출력되던 것이 cnt로 출력된다.