SQL Exists

2023. 4. 8. 10:38·📦 Database/SQL

💡 Exists

하위 쿼리에 레코드가 있는지 테스트하는 데 사용된다.

Exists 하위 쿼리가 하나 이상의 레코드를 반환하면 True를 반환한다.


Sample Table

Products 테이블

ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 bags 18
2 Chang 1 1 24 - 12 oz bottles 19
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10
4 Chef Anton's Cajun Seasoning 2 2 48 - 6 oz jars 22
5 Chef Anton's Gumbo Mix 2 2 36 boxes 21.35

Suppliers 테이블

SupplierID SupplierName ContactName Address City PostalCode Country
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. London EC1 4SD UK
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA
3 Grandma Kelly's Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA
4 Tokyo Traders Yoshi Nagase 9-8 Sekimai Musashino-shi Tokyo 100 Japan

예시

제품 가격이 20 미만인 Supplier를 반환한다.

SELECT SupplierName
FROM Suppliers
WHERE EXISTS 
(SELECT ProductName 
 FROM Products 
 WHERE Products.SupplierID = Suppliers.SupplierID 
 AND Price < 20);

제품 가격이 22인 Supplier를 반환한다.

SELECT SupplierName
FROM Suppliers
WHERE EXISTS
(SELECT ProductName
FROM Products
WHERE Products.SupplierID = Suppliers.SupplierID
AND Price = 22);
저작자표시 (새창열림)

'📦 Database > SQL' 카테고리의 다른 글

SQL Select Into & Insert Into Select  (0) 2023.04.09
SQL Any & All  (0) 2023.04.08
SQL Having  (0) 2023.04.08
SQL Group By  (0) 2023.04.08
SQL Union  (0) 2023.04.08
'📦 Database/SQL' 카테고리의 다른 글
  • SQL Select Into & Insert Into Select
  • SQL Any & All
  • SQL Having
  • SQL Group By
신건우
신건우
조용한 개발자
  • 신건우
    우주먼지
    신건우
  • 전체
    오늘
    어제
    • 분류 전체보기 (422)
      • 📘 Frontend (71)
        • Markup (1)
        • Style Sheet (2)
        • Dart (8)
        • Javascript (12)
        • TypeScript (1)
        • Vue (36)
        • React (2)
        • Flutter (9)
      • 📘 Backend (143)
        • Java (34)
        • Concurrency (19)
        • Reflection (1)
        • Kotlin (29)
        • Python (1)
        • Spring (42)
        • Spring Cloud (5)
        • Message Broker (5)
        • Streaming (2)
        • 기능 개발 (5)
      • 💻 Server (6)
        • Linux (6)
      • ❌ Error Handling (11)
      • 📦 Database (62)
        • SQL (31)
        • NoSQL (2)
        • JPQL (9)
        • QueryDSL (12)
        • Basic (4)
        • Firebase (4)
      • ⚙️ Ops (57)
        • CS (6)
        • AWS (9)
        • Docker (8)
        • Kubernetes (13)
        • MSA (1)
        • CI & CD (20)
      • 📚 Data Architect (48)
        • Data Structure (10)
        • Algorithm (8)
        • Programmers (17)
        • BaekJoon (5)
        • CodeUp (4)
        • Design Pattern (4)
        • AI (0)
      • ⚒️ Management & Tool (8)
        • Git (7)
        • IntelliJ (1)
      • 📄 Document (10)
        • Project 설계 (6)
        • Server Migration (3)
      • 📄 책읽기 (2)
        • 시작하세요! 도커 & 쿠버네티스 (2)
      • 🎮 Game (4)
        • Stardew Vally (1)
        • Path of Exile (3)
  • 블로그 메뉴

    • 링크

      • Github
    • 공지사항

    • 인기 글

    • 태그

      GStreamer #Pipeline
      Lock #Thread #Concurrency
      React #Markdown
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.0
    신건우
    SQL Exists
    상단으로

    티스토리툴바