Site icon Tutorial

EXISTS, INTERSECT and EXCEPT operator

EXISTS, INTERSECT and EXCEPT operator

In SQL Server 2008, there are several operators that can be used to manipulate data. Three of the most commonly used operators are EXISTS, INTERSECT, and EXCEPT.

EXISTS operator:

  1. The EXISTS operator is used to check whether a subquery returns any rows. It returns true if the subquery returns at least one row, and false otherwise. The EXISTS operator is typically used in a WHERE clause to filter rows based on the result of a subquery. For example, if you wanted to find all customers who have placed an order, you could use the following SQL statement:

SELECT * FROM Customers WHERE EXISTS (SELECT * FROM Orders WHERE Orders.CustomerID = Customers.CustomerID)

INTERSECT operator:

SELECT * FROM Customers WHERE EXISTS (SELECT * FROM Orders WHERE Orders.CustomerID = Customers.CustomerID)

INTERSECT

SELECT * FROM Customers WHERE City = ‘New York’

EXCEPT operator:

SELECT * FROM Orders WHERE NOT EXISTS (SELECT * FROM Payments WHERE Payments.OrderID = Orders.OrderID)

EXCEPT

SELECT * FROM Orders WHERE Paid = 1 These operators are very useful in manipulating data and allow for complex queries to be written in a concise and efficient manner.

Apply for SQL Server 2008 Certification!!

https://www.vskills.in/certification/certified-sql-server-2008-programmer

Back to Tutorials

Exit mobile version