Skip to content

Commit e965605

Browse files
Update Advanced SQL Puzzles Solutions.sql
1 parent a3d7bb0 commit e965605

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Advanced SQL Puzzles/Advanced SQL Puzzles Solutions.sql

+18-3
Original file line numberDiff line numberDiff line change
@@ -1614,11 +1614,10 @@ INSERT INTO #ManufacturingTimes VALUES
16141614
GO
16151615

16161616
--Solution 1
1617-
--MAX
1617+
--MAX with INNER JOIN
16181618
WITH cte_Max AS
16191619
(
1620-
SELECT ProductID,
1621-
MAX(DaysToManufacture) AS MaxDaysToManufacture
1620+
SELECT ProductID, MAX(DaysToManufacture) AS MaxDaysToManufacture
16221621
FROM #ManufacturingTimes b
16231622
GROUP BY ProductID
16241623
)
@@ -1628,6 +1627,22 @@ FROM #Orders a INNER JOIN
16281627
GO
16291628

16301629
--Solution 2
1630+
--MAX with correlated subquery
1631+
WITH cte_Max AS
1632+
(
1633+
SELECT ProductID, MAX(DaysToManufacture) AS MaxDaysToManufacture
1634+
FROM #ManufacturingTimes b
1635+
GROUP BY ProductID
1636+
)
1637+
SELECT *
1638+
FROM #Orders a
1639+
WHERE EXISTS (SELECT *
1640+
FROM cte_Max b
1641+
WHERE a.ProductID = b.ProductID AND
1642+
a.DaysToDelivery >= b.MaxDaysToManufacture);
1643+
GO
1644+
1645+
--Solution 3
16311646
--ALL
16321647
SELECT a.*
16331648
FROM #Orders a

0 commit comments

Comments
 (0)