set 멤버십 [in / not in] |
Set 멤버십 [where] => 값이 어떤 집합의 원소나 아니냐… [ in / not in ] - in = intersect - not in = except - where 부분 중 서브쿼리부분을 먼저 실행하고 그 자리를 대체하기 Q) find courses offered in Fall 2017 and in Spring 2018 => (select course_id from section where sem = ‘Fall’ and year = ‘2017’) intersect ( ~~ ) => select distinct 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) ; distinct가 없으니 여기선 중복 허용함 course_id가 서브쿼리 값들 중에 있어야 한다 Q) find courses offered in Fall 2017 but not in Spring 2018 => (select course_id from section where sem = ‘Fall’ and year = ‘2017’) except ( ~~ ) => select distinct course_id from section where semester = “Fall” and year = 2017 and course_id not in ( select course_id from section where semester = ‘Spring’ and year = 2018) ; Q) name all instructors whose name is neither “Mozart” nor “Einstein” => select distinct name from instructor where name not in (‘Mozart’, ‘Einstein’) 이 자리에 SFW 서브쿼리 대신 constant로 구성된 테이블값을 준 것임 Q) find total number of ( distinct ) students who have taken course sections taught by instructor with ID 10101 => select count (distinct ID) from takes where ( course_id, sec_id, semester, year ) in (select course_id, sec_id, semester, year from teaches where teaches.ID = 10101 ); where 절에 또 SFW 있어도 ㄱㅊ (n레벨 중첩) //질의는 1개 (= 맨 바깥). 나머지는 다 조건임 in 앞의 컬럼과 뒤의 컬럼들은 union호환성처럼 순서, 개수, 데이터 타입, 도메인 호환되어야 함 (course_id, sec_id, semester, year)은 컬럼4개 PK 2테이블이 조인하고 있음 비중첩으로 쓰려면 from절에 takes, teaches 열거하고, where절에 나열 또는 (0,0,0,0) = (0,0,0,0) |
'데이터베이스설계' 카테고리의 다른 글
[데이터베이스설계] 3-10 from 절의 sub쿼리 (0) | 2025.02.15 |
---|---|
[데이터베이스설계] 3-9 exist (0) | 2025.02.15 |
[데이터베이스설계] 3-7 having절, 중첩 질의문 (0) | 2024.11.26 |
[데이터베이스설계] 3-6 집계함수, group by (0) | 2024.11.25 |
[데이터베이스설계] 3-5 set 연산, null값 (1) | 2024.11.24 |