Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Databases Programming Interviews:BtrieveClipperData ModelingData StructuresDatabase AdministrationDatabase Administrator (DBA)Database AnalystDatabase DeveloperDB DevelopmentEDI/Data Integration ExpertFirebirdHierarchicalIBM DB2InformixJava DatabaseMariaDBMicrosoft Access DeveloperMongoDBMS SQL ServerMySQL ProgrammingNetworkNoSQLObject RelationalPostgrePostgreSQLProgressRDBMSRelationalSQLSQL AdministratorSQL and PL/SQLSQL Notification ServicesSQL server 2008SSRSStored ProcedureSybaseTeradata
Copyright © 2018. All Rights Reserved
MS SQL Server Interview Question:
How To Test Subquery Results with the EXISTS Operator?
Submitted by: AdministratorEXISTS 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
Submitted by: Administrator
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
Submitted by: Administrator
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.