SQL | EXISTS - GeeksforGeeks

27 Apr, 2017
The EXISTS condition in SQL is used to check whether the result of a correlated nested query is empty (contains no tuples) or not. The result of EXISTS is a boolean value True or False. It can be used in a SELECT, UPDATE, INSERT or DELETE statement.
Syntax:
SELECT column_name(s) 
FROM table_name
WHERE EXISTS 
  (SELECT column_name(s) 
   FROM table_name
   WHERE condition);

Examples:Consider the following two relation “Customers” and “Orders”.
Queries
Using EXISTS condition with SELECT statementTo fetch the first and last name of the customers who placed atleast one order.SELECT fname, lname 
FROM Customers 
WHERE EXISTS (SELECT * 
              FROM Orders 
              WHERE Customers.customer_id = Orders.c_id);Output:Using NOT with EXISTSFetch last and first name of the customers who has not placed any order.SELECT lname, fname
FROM Customer
WHERE NOT EXISTS (SELECT * 
                  FROM Orders 
                  WHERE Customers.customer_id = Orders.c_id);Output:Using EXISTS condition with DELETE statementDelete the record of all the customer from Order Table whose last name is ‘Mehra’.DELETE 
FROM Orders
WHERE EXISTS (SELECT *
              FROM customers
              WHERE Customers.customer_id = Orders.cid
              AND Customers.lname = 'Mehra');SELECT * FROM Orders;Output:Using EXISTS condition with UPDATE statementUpdate the lname as ‘Kumari’ of customer in Customer Table whose customer_id is 401.UPDATE Customers
SET lname = 'Kumari'
WHERE EXISTS (SELECT *
              FROM Customers
              WHERE customer_id = 401);SELECT * FROM Customers;Output:
Using EXISTS condition with SELECT statementTo fetch the first and last name of the customers who placed atleast one order.SELECT fname, lname 
FROM Customers 
WHERE EXISTS (SELECT * 
              FROM Orders 
              WHERE Customers.customer_id = Orders.c_id);Output:
SELECT fname, lname 
FROM Customers 
WHERE EXISTS (SELECT * 
              FROM Orders 
              WHERE Customers.customer_id = Orders.c_id);
Output:
Using NOT with EXISTSFetch last and first name of the customers who has not placed any order.SELECT lname, fname
FROM Customer
WHERE NOT EXISTS (SELECT * 
                  FROM Orders 
                  WHERE Customers.customer_id = Orders.c_id);Output:
SELECT lname, fname
FROM Customer
WHERE NOT EXISTS (SELECT * 
                  FROM Orders 
                  WHERE Customers.customer_id = Orders.c_id);
Output:
Using EXISTS condition with DELETE statementDelete the record of all the customer from Order Table whose last name is ‘Mehra’.DELETE 
FROM Orders
WHERE EXISTS (SELECT *
              FROM customers
              WHERE Customers.customer_id = Orders.cid
              AND Customers.lname = 'Mehra');SELECT * FROM Orders;Output:
DELETE 
FROM Orders
WHERE EXISTS (SELECT *
              FROM customers
              WHERE Customers.customer_id = Orders.cid
              AND Customers.lname = 'Mehra');
SELECT * FROM Orders;
Using EXISTS condition with UPDATE statementUpdate the lname as ‘Kumari’ of customer in Customer Table whose customer_id is 401.UPDATE Customers
SET lname = 'Kumari'
WHERE EXISTS (SELECT *
              FROM Customers
              WHERE customer_id = 401);SELECT * FROM Customers;Output:
UPDATE Customers
SET lname = 'Kumari'
WHERE EXISTS (SELECT *
              FROM Customers
              WHERE customer_id = 401);
SELECT * FROM Customers;
Output:
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
DBMS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SQL Trigger | Student Database
Introduction of B-Tree
CTE in SQL
Difference between Clustered and Non-clustered index
SQL Interview Questions
Introduction of ER Model
Data Preprocessing in Data Mining
Introduction of DBMS (Database Management System) | Set 1
SQL | Views
Difference between DELETE, DROP and TRUNCATE