SQL Puzzle – Correct the Incorrect Query – Win Price Worth USD 1000 – Aggregate and Subquery

SQL Puzzle - Correct the Incorrect Query - Win Price Worth USD 1000 - Aggregate and Subquery puzzle It has been a while since we have seen a puzzle so let us do a small puzzle today. Actually, this is a very simple puzzle if you have worked with SQL Server for a while. I got an email from one of the readers from the blog post and he wanted me to correct the following query which he had written. Well, I have been working with SQL Server for over 19 years and I often do not remember the syntax and have to look up online. Here is a very simple puzzle for you which involves Aggregate and Subquery.

Important Update: The Workshop is now over and it was one of the landmark workshops, where I shared business secrets of successful Performance Tuning Expert. You can watch the recording of the workshop for a limited period of time.

Puzzle – Aggregate and Subquery

Here is the query when we run against the AdventureWorks database, it gives us an error. Remember AdventureWorks database is a sample database and you can download and install AdventureWorks over here.

USE AdventureWorks2014
GO
SELECT pch.StandardCost, p.ProductID
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > AVG(p.StandardCost)
GO

Now when you run the above query, it gives us the following error:

Msg 147, Level 15, State 1, Line 17
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

Now here is your task, you need to help me to fix the above query. The hint to resolve the query is already in the blog post’s title and in the query error. You need to understand what my query is attempting to do and use the hint which is already pointed out to you. After you successfully fix the query it will produce the following results.

SQL Puzzle - Correct the Incorrect Query - Win Price Worth USD 1000 - Aggregate and Subquery correctresult

Rules

Well, there are no real rules, as long as you modify my query to get me above results. Please post your query in the comments section below before April 22, 2019. I will keep all the answers hidden till April 22, 2019 11:59:00 PM.

Winner

One Winner will get 1 free seat for my upcoming class, which is worth USD 1000.

SQL Server Performance Tuning Practical Workshop for EVERYONE
Date: April 23, 2019, Tuesday
8:00 AM Pacific Time | 11:00 AM Eastern Time

If you do not want to take part in the contest, you can directly register for the class here.

Update: April 23, 2019

The contest is closed and the winning Answer is here:

USE AdventureWorks2014
GO
SELECT pch.ProductID, pch.StartDate,pch.EndDate, pch.StandardCost, pch.ModifiedDate
FROM Production.ProductCostHistory pch
INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
WHERE pch.StandardCost > p.StandardCost
GO

There is no need for the AVG on the StandardCost as there will be always a single entry for the product. Even if you change the query with group by + having you will still see the query uses the simple join.

Congratulations to the winner Nicholas Månsson. He is very excited to join the SQL Server Performance Tuning Practical Workshop for EVERYONE.

Everyone who participated you got to solve this difficult puzzle so indirectly you are a winner (of knowledge) as well. This was an intense competition with over 500 comments.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Contest, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Relating Unrelated Tables – A Question from Reader
Next Post
Puzzle – DELETE Qualified Rows From Multiple Tables – Win USD 1000 Worth Class

Related Posts

527 Comments. Leave new

  • USE AdventureWorks2014
    GO
    SELECT pch.StandardCost, p.ProductID
    FROM Production.ProductCostHistory pch
    INNER JOIN (SELECT ProductID, AVG(StandardCost) StandardCost FROM Production.Product group by ProductID) p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > (p.StandardCost)
    GO

    Reply
  • Saranya Kothandaraman
    April 22, 2019 4:45 pm

    USE AdventureWorks2014
    GO
    SELECT pch.StandardCost, p.ProductID
    FROM Production.Product p
    INNER JOIN (SELECT ProductID, AVG(StandardCost) StandardCost FROM Production.ProductCostHistory group by ProductID) pch ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > (p.StandardCost)
    GO

    Reply
  • Hannis P. Wardum
    April 22, 2019 4:46 pm

    with x as (
    select t1.ProductID, AVG(t1.StandardCost) as avgCost
    from Production.Product T1
    INNER JOIN Production.ProductCostHistory t2 ON t2.ProductID = t1.ProductID
    group by t1.ProductID)
    SELECT p.ProductID, pch.StartDate, pch.EndDate, pch.StandardCost, p.ModifiedDate
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > (select avgCost from x
    where x.ProductID = pch.ProductID)

    Reply
  • Apologies re-submitting just for completeness….

    –Corrected DBName, StartDate and EndDate prefixes

    USE AdventureWorks2014
    GO

    SELECT p.ProductID, pch.StartDate, pch.EndDate, pch.StandardCost, pch.modifieddate
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    GROUP by p.ProductID, pch.StartDate, pch.EndDate, pch.StandardCost, pch.modifieddate
    HAVING pch.StandardCost > AVG(p.StandardCost)
    GO

    Reply
  • roberto mirelman
    April 22, 2019 4:49 pm

    ;WITH PA AS(
    Select
    AVG(p.StandardCost) P_AVG,
    ProductID
    FROM Production.Product P
    GROUP BY ProductID
    ),
    pch as (
    SELECT
    productID,
    StartDate,
    EndDate,
    ModifiedDate,
    StandardCost
    FROM Production.ProductCostHistory
    )
    SELECT p.ProductID, pch.StartDate, pch.EndDate,
    pch.StandardCost, pch.ModifiedDate ,
    PA.P_AVG
    FROM pch INNER JOIN
    Production.Product p ON pch.ProductID = p.ProductID INNER JOIN
    PA ON PA.ProductID = P.ProductID
    WHERE pch.StandardCost > PA.P_AVG
    order by pch.ProductID

    Reply
  • roberto mirelman
    April 22, 2019 4:56 pm

    Pinal, this simple query will produce same results, no subquery needed at al:

    SELECT p.ProductID, pch.StartDate, pch.EndDate,
    pch.StandardCost, pch.ModifiedDate ,
    P.StandardCost
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > p.StandardCost

    Reply
  • SELECT pch.*
    FROM Production.ProductCostHistory pch
    INNER JOIN
    (Select p.ProductID, AVG(p.StandardCost) AvgStdCost
    from Production.Product p
    Group by p.ProductID) p
    ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > p.AvgStdCost

    Reply
  • SELECT pch.*
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > (select AVG(p1.StandardCost) from Production.Product p1 WHERE p.ProductID = p1.ProductID)
    order by pch.ProductID

    Reply
  • Or maybe this:
    SELECT pch.*
    FROM Production.ProductCostHistory pch
    CROSS APPLY (select AVG(p.StandardCost) as media from Production.Product p where pch.ProductID = p.ProductID) p1
    WHERE pch.StandardCost > p1.media
    order by pch.ProductID

    Reply
  • Venkatesh Krishnamurthy
    April 22, 2019 5:03 pm

    This is the original query to get exact output as stated above. AVG – is an aggregate function and can’t be used directly with WHERE condition.

    select p.ProductID,startdate,enddate,pch.StandardCost,pch.ModifiedDate
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > p.StandardCost

    Reply
  • SELECT pch.*
    FROM Production.ProductCostHistory pch
    WHERE pch.StandardCost >
    (select avg(StandardCost)from Production.Product p
    WHERE pch.ProductID = p.ProductID
    )

    Reply
  • SELECT pch.StandardCost, p.ProductID
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    Group by pch.StandardCost, p.ProductID
    Having pch.StandardCost > AVG(p.StandardCost)

    Reply
  • Siva Krishna P
    April 22, 2019 5:17 pm

    USE AdventureWorks
    GO
    SELECT p.ProductID, max(pch.StartDate),max(pch.Enddate),max(pch.StandardCost),max(pch.ModifiedDate)
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    group by pch.ProductID , p.ProductID
    having max(pch.StandardCost) > AVG(p.StandardCost)
    GO

    Reply
  • Shivaprasad K
    April 22, 2019 5:19 pm

    SELECT p.ProductID, pch.StartDate, pch.EndDate, pch.StandardCost, p.ModifiedDate
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    group by p.ProductID, pch.StartDate, pch.EndDate, p.ModifiedDate, pch.StandardCost
    having pch.StandardCost > AVG(p.StandardCost)
    GO

    Reply
  • Carlos Benito
    April 22, 2019 5:20 pm

    SELECT pch.ProductID
    , pch.StartDate
    , pch.EndDate
    , pch.StandardCost
    , pch.ModifiedDate
    FROM Production.ProductCostHistory
    pch
    WHERE EXISTS ( SELECT 1
    FROM Production.Product
    p
    WHERE pch.ProductID = p.ProductID
    AND pch.StandardCost > p.StandardCost
    )

    or

    SELECT pch.ProductID
    , pch.StartDate
    , pch.EndDate
    , pch.StandardCost
    , pch.ModifiedDate
    FROM Production.ProductCostHistory
    pch
    INNER
    JOIN Production.Product
    p
    ON p.ProductID = pch.ProductID
    WHERE pch.StandardCost > P.StandardCost
    GO

    Reply
  • Padma Valluri
    April 22, 2019 5:24 pm

    USE AdventureWorks2014
    GO
    SELECT pch.StandardCost, p.ProductID
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > (select AVG(StandardCost) from Production)
    GO

    Reply
  • Eagerly waiting for results.

    Reply
  • Michael McRorey
    April 22, 2019 5:33 pm

    SELECT
    p.ProductID
    , pch.StartDate
    , pch.EndDate
    , pch.StandardCost
    , pch.ModifiedDate
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > p.StandardCost
    order by p.ProductID

    Reply
  • SELECT pch.StandardCost, p.ProductID
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > (select AVG(standardCost) from Production.ProductCostHistory abc where abc.ProductID = p.ProductID)

    Reply
  • below are the 2 ways to resolve the above bug

    SELECT pch.StandardCost, p.ProductID
    FROM Production.ProductCostHistory pch
    INNER JOIN (select productid ,AVG(StandardCost) over() StandardCost from Production.Product) p ON pch.ProductID = p.ProductID
    where pch.StandardCost >p.StandardCost

    SELECT pch.StandardCost, p.ProductID
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE pch.StandardCost > (select AVG(StandardCost) from Production.Product)

    Reply

Leave a Reply