본문 바로가기

분류 전체보기25

Intermediate SQL. 1. Join operations. from 절 안에서 사용할 수 있고, Natural join, Inner join, Outer join 3가지가 있다. natural join 2022. 4. 5.
[SQL] With, 스칼라 서브쿼리, DB 수정. 1. with -- with로 만드는 테이블은 임시테이블이다. -- 가장 많은 예산을 가지고 있는 부서의 이름을 출력. 아래 두 쿼리는 같은 결과가 나온다. with max_budget(value) as (select max(budget) from department) select department.dept_name from department, max_budget where max_budget.value = department.budget; select dept_name from department where budget = (select max(budget) from department); -- 임시테이블을 여러개 만들 수도 있고, 임시테이블을 만들 때 이미 만든 임시테이블 참조 가능. -- 전체 .. 2022. 3. 31.
SQL 서브쿼리. Oracle 기준. 서브쿼리는, Select에 있으면 스칼라 서브쿼리, from에 있으면 인라인뷰 서브쿼리, where에 있으면 중첩 서브쿼리라고 부른다. 1. 중첩 서브쿼리(Nested subquery). -- in을 사용해서 서브쿼리가 리턴한 테이블에 값이 있는지 체크. -- 서브쿼리를 쓰지 않으면 intersect로 비슷하게 할 수 있을 것 같다. -- 2017 가을, 2018 봄에 개설된 과목 id를 출력. select course_id from section where semester = 'Fall' and year = 2017 and course_id in (select course_id from section where semester = 'Spring' and year = 2018); -- not in 을 사.. 2022. 3. 31.
SQL 명령어 정리. Oracle 기준. 1. select, from -- instructor relation에서 name 속성만 선택. select name from instructor; -- instructor relation에서 name, dept_name 속성을 선택. select name, dept_name from instructor; -- instructor relation에서 모든 속성을 선택. select * from instructor; -- SQL에서 속성 이름은 대소문자를 구분하지 않는다. 아래 두개는 같다. select name from instructor; select NAME from instructor; 2. distinct, all -- select는 중복을 허용한다. 중복을 제거하려면 select distinct.. 2022. 3. 30.