SQL문 실습
오름차순과 내림차순
select emp_no, hire_date
from employees
where hire_date >= "1990-01-01"
and hire_date <= "1990-12-31"
order by hire_date desc;
사원의 번호, 이름을 가져오되 이름이 K로 시작하는 사원을 번호 기준 오름차순으로 가져와라.
select emp_no, first_name, last_name
from employees
where first_name like 'K%'
order by emp_no desc
숫자 함수
- pow(데이터,승)
- greatest(…) : max
- least(…) : min
위치 함수
- concat() : 전달받은 문자열을 모두 결합하여 하나의 문자열로 반환합니다. 전달받은 데이터가 널 하나라도 껴있으면 문자열로 반환 안되고 널로 반환됨.
- locate(찾을 데이터, 찾을 문자열) : 특정 문자열에서 처음으로 나타나는 위치를 반환, 없다면 0을 반환한다.
- left() : 왼쪽부터 명시한 개수만큼의 문자를 반환
- right()
select left("mysql php html java",5)
대소문자 변경
- lower(), upper()
문자열 대체
- replace()
select max
select emp_no
from salaries
where max(salary) = salary
Group by ~ having
유형 별로 그룹지어서 확인하되, 유형의 조건을 having절에 입력한다. 나누고자 하는 그룹의 컬럼명을 select 절과 group by 뒤에 추가한다.
# 사원의 수를 성별로 가져오기
select gender, count(*)
from employees
group by gender
# 각 부서별 근무하고 있는 사원들 수를 가져온다.
select dept_no, count(*)
from dept_emp
where to_date="9999-01-01"
group by dept_no;
# 직원들 중에서 생일이 01-05인 사람들을 그룹화! 수를 카운트
select birth_date, count(*)
from employees
group by birth_date
having substring(birth_date, 6, 5) = "01-05";