SQL Tips

yozzum·2022년 9월 12일
0

SQL

목록 보기
1/25
  1. SEMIJOIN : 겉으로 드러나는 조인이 아닌, 내부적으로 수행 원리에 의해 생기는 조인 방식. 주로 IN이나 EXIST를 썼을 때 실행계획에서 볼 수 있는 조인 방식입니다.

    1) 서브쿼리가 옵티마이저에 의해 조인으로 풀립니다.
    2) 조인으로 풀린 서브쿼리 집합은 후행처리가 됩니다. (OUTER와 INNER가 있을 때 INNER로 처리된다는 뜻)
    3) OUTER테이블에서 INNER테이블로 조인을 시도했을 때 하나의 로우가 조인에 성공하면 진행을 멈추고 OUTER테이블의 다음 로우를 계속 처리해가는 방식입니다.

  • 아래 쿼리는 옵티마이저에 의해 HASH(SEMI) JOIN으로 풀립니다. EXIST문 안에 ROWNUM을 쓰면 옵티마이저가 쿼리를 SEMI JOIN으로 풀지 못하기 때문에 유의해야합니다.
select company, group_name
from idol_group t1
where exists (select '1' 
              from idol_member t2
              where t1.group_name = t2.group_name
              and t2.birthday like '1997%');
  1. EXIST : 교집합, NOT EXIST : 차집합
select * from 런닝맨 t1 where exists (select 'X'from 무한도전 t2 where t1.name = t2.name);
select * from 런닝맨 t1 where not exists (select 1 from 무한도전 t2 where t1.name = t2.name);
  1. NTILE : 등급매기기
select 이름
      ,ntile(5) over (order by 국어 desc) 국어등급
      ,국어
      ,ntile(5) over (order by 수학 desc) 수학등급
      ,수학
      ,ntile(5) over (order by 영어 desc) 영어등급
      ,영어
from exam_score;
  1. 누적 값 구하기
select t1.no, max(t1.title), t1.count_dt, max(t1.download_cnt), sum(t2.download_cnt) "누적 값"
from ssak3 t2, ssak3 t2
where t1.no = t2.no
and t1.count_dt >= count_dt
group by t1.no, t1.count_dt
order by t1.no, t1.count_dt
select no, title, count_dt, download_cnt
      ,sum(download_cnt) over (partition by no order by count_dt rows between unbounded preceding and current row) "누적 값"
      ,sum(download_cnt) over (partition by no order by count_dt rows between current row and unbounded following) "거꾸로 누적값"
from ssak3

출처: 정미나쌤 유투브 강의

profile
yozzum

0개의 댓글