Collections:
EXISTS - Testing Subquery Results in SQL Server
How To Test Subquery Results with the EXISTS Operator in SQL Server Transact-SQL?
✍: FYIcenter.com
EXISTS is a special operator used to test subquery results. EXISTS can be used in two ways:
EXISTS (SELECT ...) -- Returns TRUE if the specified subquery has one or more rows returned. NOT EXISTS (SELECT ...) -- Returns TRUE if the specified subquery no rows returned.
The following tutorial exercise shows you two examples of EXISTS operators. The sample database AdventureWorksLT provided by Microsoft is used.
USE AdventureWorksLT GO -- Number of customers with orders SELECT COUNT(*) FROM SalesLT.Customer c WHERE EXISTS ( SELECT * FROM SalesLT.SalesOrderHeader s WHERE s.CustomerID = c.CustomerID ) GO 32 -- Number of customers without orders SELECT COUNT(*) FROM SalesLT.Customer c WHERE NOT EXISTS ( SELECT * FROM SalesLT.SalesOrderHeader s WHERE s.CustomerID = c.CustomerID ) 408
Â
⇒Boolean Values and Logical Operations in SQL Server Transact-SQL
2017-01-21, 1342🔥, 0💬
Popular Posts:
What are single-byte character string data types supported in SQL Server Transact-SQL? Single-byte c...
What Happens to Your Transactions When ERROR 1205 Occurred in MySQL? If your transaction receives th...
How To Convert a Unicode Strings to Non-Unicode Strings in SQL Server Transact-SQL? Since Unicode ch...
How To Convert a Unicode Strings to Non-Unicode Strings in SQL Server Transact-SQL? Since Unicode ch...
What is dba.FYIcenter.com Website about? dba.FYIcenter.com is a Website for DBAs (database administr...