You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
select [컬럼명] (","로 구분)
from [테이블명]
where [조건들] (and, or 연산자로 구분)
order by [컬럼명들] (","로 구분)
나이가 30보다 많고 부서가 'Sales'인 직원들을 나이 내림차순으로 정렬하여 조회합니다.
select name, age
from employees
where age >30and department ='Sales'order by age desc;
그룹 제어
select [컬럼명] (","로 구분)
from [테이블명]
where [조건들] (and, or 연산자로 구분)
group by [컬럼명들] (","로 구분)
having [group by절에 해당하는 조건들] (","로 구분)
나이가 30보다 많은 직원들의 부서별 평균 급여를 조회하며, 평균 급여가 50,000 이상인 부서만 반환합니다.
select department, avg(salary)
from employees
where age >30group by department
havingavg(salary) >50000;
분기문
select
(case [컬럼명] when [비교값1] then [반환값1]
when [비교값2] then [반환값2]
when [비교값3] then [반환값3]
...
else [when절 이외의 조건일 때 반환될 값]
end) as [별칭 컬럼명]
from [테이블명]
부서 이름이 'Sales'일 경우 '영업부', 'IT'일 경우 'IT부서', 그 외의 경우 '기타부서'로 변환하여 조회합니다.
select
(case department when 'Sales' then '영업부'
when 'IT' then 'IT부서'
else '기타부서'
end) as department_name
from employees;
집합연산
union (중복제거)
select [컬럼명] (","로 구분)
from [테이블명A]
unionselect [컬럼명] (","로 구분)
from [테이블명B]
IT 부서 직원 또는 나이가 40이 넘는 직원들의 이름을 중복 없이 조회합니다.
select name from employees where department ='IT'unionselect name from employees where age >40;
union all
select [컬럼명] (","로 구분)
from [테이블명A]
union allselect [컬럼명] (","로 구분)
from [테이블명B]
IT 부서 직원 또는 나이가 40이 넘는 직원들의 이름을 중복을 포함하여 조회합니다.
select name from employees where department ='IT'union allselect name from employees where age >40;
순위집계
1. rank
동일한 점수는 같은 등수를 부여하지만, 다음 등수는 그만큼 밀립니다.
ex) 1등(100점), 2등(90점), 2등(90점), 4등(85점) ...
select rank() over([그룹할 컬럼들] order by [순위 기준 컬럼들])
from [테이블명]
직원들의 급여를 기준으로 내림차순으로 순위를 매기며, 동일 급여일 경우 동일한 순위를 부여합니다. 순위가 매겨진 후 상위 10명의 직원 정보를 출력합니다.
select name, salary, rank() over(order by salary desc) as ranking
from employees
order by ranking
limit10;
2. dense_rank
동일한 점수는 같은 등수를 부여하며, 등수가 밀리지 않습니다.
ex) 1등(100점), 2등(90점), 2등(90점), 3등(85점) ...
select dense_rank() over([그룹할 컬럼들] order by [순위 기준 컬럼들])
from [테이블명]
직원들의 급여를 기준으로 내림차순으로 순위를 매기며, 동일 급여일 경우 동일한 순위를 부여하고, 다음 순위가 밀리지 않습니다.
select name, salary, dense_rank() over(order by salary desc) as ranking
from employees;
3. row_number
동일한 점수라도 각 행에 다른 번호가 부여됩니다.
ex) 1등(100점), 2등(90점), 3등(90점), 4등(85점) ...
select name, salary, row_number() over(order by salary desc) as ranking
from employees;
직원들의 급여를 기준으로 내림차순으로 순위를 매기며, 동일 급여일 경우에도 고유한 순위를 부여합니다.
select name, salary, row_number() over(order by salary desc) as ranking
from employees;
조인
1. inner join
select*from [테이블1] as a
inner join [테이블2] as b
ona.key=b.key;
직원과 그 직원이 참여한 프로젝트 정보를 조회합니다. 두 테이블에 모두 존재하는 데이터만 반환합니다.
selecta.name, b.project_namefrom employees as a
inner join projects as b
ona.employee_id=b.employee_id;
2. outer join
left join
select*from [테이블1] as a
left outer join [테이블2] as b
ona.key=b.key;
모든 직원 정보를 반환하고, 프로젝트에 참여하지 않은 직원의 경우 프로젝트 이름은 null로 반환합니다.
selecta.name, b.project_namefrom employees as a
left outer join projects as b
ona.employee_id=b.employee_id;
right join
select*from [테이블1] as a
right outer join [테이블2] as b
ona.key=b.key;
모든 프로젝트 정보를 반환하고, 프로젝트에 참여하지 않은 직원의 경우 직원 이름은 null로 반환합니다.
selecta.name, b.project_namefrom employees as a
right outer join projects as b
ona.employee_id=b.employee_id;
3. full outer join
select*from [테이블1] as a
�full outer join [테이블2] as b
ona.key=b.key;
직원과 프로젝트의 모든 정보를 반환하며, 매칭되지 않는 경우 null로 반환합니다.
selecta.name, b.project_namefrom employees as a
full outer join projects as b
ona.employee_id=b.employee_id;
4. self join
select*from [테이블1] as a
join [테이블1] as b
ona.key=b.key;
직원과 그 직원의 매니저 정보를 조회합니다. 같은 테이블을 두 번 사용하여 서로 연결된 데이터를 조회합니다.
selecta.nameas employee1, b.nameas employee2
from employees as a
join employees as b
ona.manager_id=b.employee_id;
5. cross join
select*from [테이블1] as a
cross join [테이블2] as b
직원과 프로젝트의 모든 조합을 반환합니다.
selecta.name, b.project_namefrom employees as a
cross join projects as b;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
기본검색 및 정렬
그룹 제어
분기문
집합연산
union (중복제거)
union all
순위집계
1. rank
동일한 점수는 같은 등수를 부여하지만, 다음 등수는 그만큼 밀립니다.
2. dense_rank
동일한 점수는 같은 등수를 부여하며, 등수가 밀리지 않습니다.
3. row_number
동일한 점수라도 각 행에 다른 번호가 부여됩니다.
조인
1. inner join
2. outer join
left join
right join
3. full outer join
4. self join
5. cross join
문자열 함수
문자열 자르기
select substring(타겟문자열, 시작, 끝)양 옆 공백 없애기
select ltrim(타겟문자열)select rtrim(타겟문자열)문자열 채우기
'select lpad(타겟문자열, 자릿수, 채울문자)
'select rpad(타겟문자열, 자릿수, 채울문자)문자열 대체
select replace(타겟문자열, 변경될문자열, 변경할문자열)문자열 길이 출력
select length(타겟문자열)날짜 함수
현재 날짜 및 시간 출력
now()현재 날짜와 타겟날짜와의 시간 차이 계싼
age(timestamp)두 날짜 사이의 시간 차이 계산
age(timestamp, timestamp)날짜 정보 획득
year(타겟날짜)month(타겟날짜)day(타겟날짜)기준 날짜 키워드에 해당하는 값 이하 날짜 데이터 default 처리하고 반환
date_trunc(날짜키워드, 날짜)원하는 포맷으로 날짜 변환
date_format(타겟날짜, 날짜포맷)그 외 문제에 사용되는 함수
null일 경우 다음 순서 인자를 반환
select [컬럼명], coalesce([컬럼명], null이면반환할것)source_type을 target_type으로 변환해서 반환
select cast(source_type, target_type)v 값을 소수점 s자리까지 반올림하고 s자리 미만은 버림
select round(v, s)All reactions