Which Transact-SQL query should you use?

You administer a Microsoft SQL Server 2012 databasethat includes a table named Products. The Products
table has columns named Productld, ProductName, andCreatedDateTime. The table contains a unique
constraint on the combination of ProductName and CreatedDateTime. You need to modify the Products table to
meet the following requirements:
Remove all duplicates of the Products table based on the ProductName column.
Retain only the newest Products row.
Which Transact-SQL query should you use?

You administer a Microsoft SQL Server 2012 databasethat includes a table named Products. The Products
table has columns named Productld, ProductName, andCreatedDateTime. The table contains a unique
constraint on the combination of ProductName and CreatedDateTime. You need to modify the Products table to
meet the following requirements:
Remove all duplicates of the Products table based on the ProductName column.
Retain only the newest Products row.
Which Transact-SQL query should you use?

A.
WITH CTEDupRecords
AS
(
SELECT MAX(CreatedDateTime) AS CreatedDateTime, ProductName
FROM Products
GROUP BY ProductName
HAVING COUNT(*) > 1
)
DELETE p
FROM Products p
JOIN CTEDupRecords cte ON
p.ProductName = cte.ProductName
AND p.CreatedDateTime > cte.CreatedDateTime

B.
WITH CTEDupRecords
AS
(
SELECT MAX(CreatedDateTime) AS CreatedDateTime, ProductName
FROM Products
GROUP BY ProductName
HAVING COUNT(*) > 1
)
DELETE p
FROM Products p
JOIN CTEDupRecords cte ON
cte.ProductName = p.ProductName
AND cte.CreatedDateTime > p.CreatedDateTime

C.
WITH CTEDupRecords
AS
(
SELECT MIN(CreatedDateTime) AS CreatedDateTime, ProductName
FROM Products
GROUP BY ProductName
)
DELETE p
FROM Products p
JOIN CTEDupRecords cte ON
p.ProductName = cte.ProductName

D.
WITH CTEDupRecords
AS
(
SELECT MAX(CreatedDateTime) AS CreatedDateTime, ProductName
FROM Products
GROUP BY ProductName
HAVING COUNT(*) > 1
)
DELETE p
FROM Products p
JOIN CTEDupRecords cte ON
p.ProductName = cte.ProductName

Explanation:
Verified answer as correct.



Leave a Reply 6

Your email address will not be published. Required fields are marked *


Mr.Awesome

Mr.Awesome

How is it B??

“FROM Products p
JOIN CTEDupRecords cte ON
cte.ProductName = p.ProductName ” does not makes sense at all

it should be “DELETE p
FROM Products p
JOIN CTEDupRecords cte ON
p.ProductName = cte.ProductName ” which leaves A to be the correct answer.

in any place you look how to do a join the table after the “FROM” Always comes before the table after the join.

dusg84

dusg84

i think that b is the correct answer not a because the subjet said “remove all duplicate of the product tabla based on the product name column” then AND cte.crateddatetime > p.createddatetime is the key