SQL
SQL 문법 연습 - 1) 돈을 벌기 위해 일을 합시다!
김예나
2024. 12. 3. 15:16
id | name | position | salary | hire_date |
1 | 르탄이 | 개발자 | 30000 | 2022-05-01 |
2 | 배캠이 | PM | 40000 | 2021-09-25 |
3 | 구구이 | 파트장 | 35000 | 2023-06-01 |
4 | 이션이 | 팀장 | 50000 | 2021-07-09 |
- sparta_employees 테이블에서 모든 직원의 이름(name)과 직급(position)을 선택하는 쿼리를 작성해주세요.
- select name, position from sparta_employees
- sparta_employees 테이블에서 중복 없이 모든 직급(position)을 선택하는 쿼리를 작성해주세요.
- select distinct position from sparta_employees
- sparta_employees 테이블에서 연봉(salary)이 40000과 60000 사이인 직원들을 선택하는 쿼리를 작성해주세요.
- select * from sparta_employees where salary >= 40000 and salary <= 60000
- sparta_employees 테이블에서 입사일(hire_date)이 2023년 1월 1일 이전인 모든 직원들을 선택하는 쿼리를 작성해주세요.
- select * from sparta_employees where hire_date < '2023-01-01'
배운 것
- distinct
- 중복 없이 데이터를 조회할 때 사용
- select할 행 앞에 붙임
- 여러 컬럼 조합 앞에 두면 해당 조합을 중복없이 조회함
SELECT DISTINCT department_id, job_id
FROM employees;
- 날짜 조건 검색
- 특정 기간을 포함한 데이터를 조회할 때는 between 사용
select *
from table_name
where hire_date between '2024-01-01' and '2024-05-05';