백엔드/DB

[MariaDB] MariaDB 학습 - 서브쿼리

tero1115 2023. 7. 10. 14:03

서브쿼리(Sub Query)란?

 - 쿼리문 안에 적는 쿼리문

 

스칼라 서브쿼리

 - select 서브쿼리

인라인뷰 서브쿼리

 - from 서브쿼리

서브쿼리

 - where 서브쿼리 


서브쿼리

-- 값이 조건이 아니라 유동적인 데이터일 경우 서브쿼리를 사용할 수 있다

-- 비교 조건일 경우에는 서브쿼리 결과가 단일행 단일열이어야 한다

-- 이름이 Taylor인 사람의 월급보다 같거나 많은 사원의 이름과 월급을 출력하시오
select salary
from employees
where first_name = 'Jonathon';

select first_name , salary 
from employees
where salary >= 8600;

select first_name , salary 
from employees
-- 값이 조건이 아니라 유동적인 데이터일 경우 서브쿼리를 사용할 수 있다
-- 비교 조건일 경우에는 서브쿼리 결과가 단일행 단일열이어야 한다
where salary >= (
		select salary
		from employees
		where first_name = 'Jonathon');

 

 

서브쿼리

-- 서브쿼리를 사용할 때에는 본쿼리보다 서브쿼리를 먼저 작성해본다.

-- in절을 걸 때에는 1개 이상의 행이 나오도록 서브쿼리를 작성할 수 있다

-- first_name이 J로 시작하는 사람들의 월급하고 같은 사람들의 first_name과 salary를 출력하시오
-- 서브쿼리를 사용할 때에는 본쿼리보다 서브쿼리를 먼저 작성해본다.
	
-- 서브쿼리
select *
from employees
where first_name like 'J%';

select *
from employees
where salary in (8200, 7000, 3200);

select first_name, salary
from employees
-- in절을 걸 때에는 1개 이상의 행이 나오도록 서브쿼리를 작성할 수 있다
where salary in (
	select salary
	from employees
	where first_name like 'J%')

 

스칼라 서브쿼리

-- 스칼라 서브쿼리는 쿼리횟수가 결과행만큼 실행될 수 있다
-- join으로 해결가능하면 join으로 해결하는 것이 좋다

-- 직원의 first_name고 department_name을 출력하시오(department_name을 서브쿼리로)
-- 스칼라 서브쿼리는 쿼리횟수가 결과행만큼 실행될 수 있다
-- join으로 해결가능하면 join으로 해결하는 것이 좋다
select e.first_name ,(
	select department_name 
	from departments d
	where e.department_id = d.department_id  
)
from employees e;

 

 

인라인뷰 서브쿼리

-- form절 데이터를 줄여서 가져오거나 가공해서 가져오거나 이름을 바꿔서 가져오거나 할 때 사용한다

-- form절 데이터를 줄여서 가져오거나 가공해서 가져오거나 이름을 바꿔서 가져오거나 할 때 사용한다

select concat(first_name, ' ', last_name) as name, salary 
from employees;

select *
from (
	select concat(first_name, ' ', last_name) as name, salary 
	from employees
) as emp;