EXISTS usage in SQL

Source: Internet
Author: User

Syntax
EXISTS subquery

Parameters
Subquery
Is a restricted SELECT statement (the COMPUTE clause and the INTO keyword are not allowed)

Result type
Boolean

Result value
If the subquery contains rows, TRUE. EXISTS (select null) is returned and True is returned.

 

NOT EXISTS
Not exists is opposite to EXISTS. If the subquery does NOT return rows, the WHERE clause in not exists is satisfied. In this example, find the name of the publisher who does not publish commercial books.

 

The code is as follows: Copy code
SELECT pub_name
FROM publishers
WHERE NOT EXISTS
(SELECT *
FROM titles
WHERE pub_id = publishers. pub_id
AND type = 'business ')
Order by pub_name


In this example, NULL is specified in the subquery and the result set is returned. By using EXISTS, the value is still TRUE.

 

The code is as follows: Copy code
SELECT CategoryName
FROM Categories
Where exists (select null)
Order by CategoryName ASC

Instance

The code is as follows: Copy code


SELECT c. CustomerId, CompanyName
From mers c
Where exists (
SELECT OrderID FROM Orders o
WHERE o. CustomerID = cu. CustomerID)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.