Answer to Question #181273 in Databases | SQL | Oracle | MS Access for shakshi

Question #181273

Write separate queries using a join, a subquery, a CTE, and then an EXISTS to list all AdventureWorks customers who have not placed an order.


1
Expert's answer
2021-04-14T05:16:05-0400
SELECT DISTINCT P.BusinessEntityID, P.LastName, P.MiddleName, P.FirstName

FROM Person.Person P

LEFT JOIN Sales.Customer C

ON P.BusinessEntityID = C.PersonID

LEFT JOIN Sales.SalesOrderHeader H

ON C.CustomerID = H.CustomerID

WHERE H.SalesOrderID IS NULL

 

SELECT DISTINCT P.BusinessEntityID, P.LastName, P.MiddleName, P.FirstName FROM Person.Person P

WHERE P.BusinessEntityID NOT IN (SELECT C.PersonID

FROM Sales.Customer C

JOIN Sales.SalesOrderHeader H

ON C.CustomerID = H.CustomerID );

 

WITH ActiveCustomers (BusinessEntityID) AS

 ( SELECT DISTINCT C.PersonID

FROM Sales.Customer C

JOIN Sales.SalesOrderHeader H

ON C.CustomerID = H.CustomerID )

SELECT DISTINCT P.BusinessEntityID, P.LastName, P.MiddleName, P.FirstName

FROM Person.Person P

LEFT JOIN ActiveCustomers A

ON A.BusinessEntityID = P.BusinessEntityID

WHERE A.BusinessEntityID IS NULL;

 

SELECT DISTINCT P.BusinessEntityID, P.LastName, P.MiddleName, P.FirstName

FROM Person.Person P

LEFT JOIN ActiveCustomers A

ON A.BusinessEntityID = P.BusinessEntityID

WHERE A.BusinessEntityID IS NULL;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS