본문 바로가기
SQL

[분석해 보세요] 결과값이 뭘까?

by 펜네임 2021. 5. 18.

회사에서 접한 쿼리를 이용한 SQL 문제풀이.

 

문제1

SELECT *
  FROM (
      SELECT '1' AS A
      FROM DUAL
      UNION ALL
      SELECT '2' AS A
      FROM DUAL
      UNION ALL
      SELECT '3' AS A
      FROM DUAL
   ) M
WHERE NOT EXISTS (
   SELECT 1
       FROM (
	      SELECT '1' AS A
	      FROM DUAL
	      UNION ALL
	      SELECT '2' AS A
	      FROM DUAL
	      UNION ALL
	      SELECT '3' AS A
	      FROM DUAL
      ) X
    WHERE X.A < M.A
)

 

not exists의 개념을 이해하고 있어야 풀 수 있다.

정답 :

 

문제2

SELECT *
  FROM (
      SELECT '1' AS A
      FROM DUAL
      UNION ALL
      SELECT '2' AS A
      FROM DUAL
      UNION ALL
      SELECT '3' AS A
      FROM DUAL
      UNION ALL
      SELECT '4' AS A
      FROM DUAL
      UNION ALL
      SELECT '6' AS A
      FROM DUAL
   ) M
WHERE NOT EXISTS (
   SELECT 1
       FROM (
	      SELECT '2' AS A
	      FROM DUAL
	      UNION ALL
	      SELECT '3' AS A
	      FROM DUAL
	      UNION ALL
	      SELECT '4' AS A
	      FROM DUAL
	      UNION ALL
	      SELECT '5' AS A
	      FROM DUAL
      ) X
    WHERE X.A > M.A
)

 

정답 :

 

댓글