오늘 학습한 내용
- Java : 인터페이스 복습, contains 메소드, split 메소드
- SQL : 데이터를 조회하는 sql 기본 문법들 복습
- 내일배움캠프 : 미니프로젝트 완성
내용
복습한 sql 정리
mysql로 실습
실습데이터는 다음 youtube 강의를 참고하면 다운 받을 수 있다.
참고 : YOUTUBE 부부개발단
https://www.youtube.com/@funnyjava
-- employees 필드의 정보 확인하기
desc employees;
-- employees 테이블 전체 내용 보기
select * from employees;
-- 별명 붙이기 as
select first_name as fn, last_name as ln, salary as s from employees;
-- 필드 합치기 concat
select concat(first_name, ' ',last_name) as 이름, hire_date as 입사일 from employees;
-- 결과값 중복을 제거 distinct
select distinct manager_id from employees;
-- order by 결과를 정렬하고 싶을 때 asc(오름차순, 생략가능) or desc(내림차순)
select first_name, last_name, hire_date, salary from employees order by salary;
select first_name, last_name, hire_date, salary from employees order by salary desc;
-- 조건을 주고 싶을 때 where절
select * from employees where last_name = 'King';
select * from employees where hire_date > '1989-06-17';
select * from employees where first_name = 'Steven' and last_name = 'King';
select * from employees where first_name = 'Steven' or last_name = 'King';
-- NULL 다루기
select * from employees where commission_pct is null;
select * from employees where commission_pct is not null;
-- 패턴 매칭 like
select * from employees where first_name like 'b%'; /* 필드값이 b로 시작하는 것들만 조회 */
select * from employees where first_name like '%t'; /* 필드값이 t로 끝나는 것들만 조회 */
select * from employees where first_name like '%t%'; /* 필드값에 t가 포함된 것들만 조회 */
select * from employees where first_name like '___'; /* 필드값의 문자수와 언더바(_) 의 개수와 같은 것들만 조회 */
select * from employees where first_name like 'h____'; /* 필드값이 h로 시작하고 필드값의 문자수와 언더바의 개수+1 와 같은 것들만 조회 */
-- IN
select * from employees where department_id in(90,100); /*department_id의 값이 90 또는 100인 값 조회*/
-- 문자형 함수 ucase, upper
select ucase(last_name) from employees; /* employees의 last_name값을 모두 대문자로 */
select upper(last_name) from employees; /* employees의 last_name값을 모두 대문자로 */
select ucase('jun'); /* jun->JUN */
select upper('jun'); /* jun->JUN */
-- 문자형 함수 lcase, lower
select lcase(last_name) from employees; /* employees의 last_name값을 모두 소문자로 */
select lower(last_name) from employees; /* employees의 last_name값을 모두 대문자로 */
select lcase('JUN'); /* JUN->jun */
select lower('JUN'); /* JUN->jun */
-- 문자형 함수 substring
select substring('happy day',4,2); /* happy day의 4번째 글자부터 2개만 조회 */
select substring(first_name,1,1) from employees; /* employees의 first_name에서 1번째 1글자만 조회 */
select * from employees where substring(first_name,1,1)='A'; /* employees에서 first_namedml 1번째 1글자가 a인것만 조회 */
-- 되도록이면 좌변은 변형시키지 않는 것이 좋다. index와 실행 계획 때문에,,
'TIL&WIL' 카테고리의 다른 글
2023-05 3주차 WIL (0) | 2023.05.19 |
---|---|
2023-05-19 TIL (Java : Replace, ReplaceAll, ReplaceFirst) (0) | 2023.05.19 |
2023-05-17 TIL (프로그래머스 문제풀이) (0) | 2023.05.17 |
2023-05-16 TIL (Java : 향상된 for문) (0) | 2023.05.16 |
2023-05-15 TIL (내일배움캠프 미니프로젝트) (0) | 2023.05.15 |