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

  • select pcha.*
    from
    (SELECT p.ProductID,pch.StandardCost
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    –WHERE pch.StandardCost > AVG(p.StandardCost)
    –where p.ProductID = 707
    group by p.ProductID,pch.StandardCost
    having pch.StandardCost>avg(p.standardCost)
    )A
    join Production.ProductCostHistory pcha on pcha.ProductID = a.ProductID and pcha.StandardCost = a.StandardCost
    order by pcha.ProductID
    GO

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

    Reply
  • Hi here is my answer.to the AdventureWorks query.
    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 > (select AVG(StandardCost) from Production.Product where pch.ProductID = ProductID)
    GO
    Regards
    Peter Jones

    Reply
  • I wish you had just given us the description of what result you are trying to show because the results shown above can be duplicated with this query that contains no aggregate or subquery. Surely that is not what you meant?
    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

    Reply
  • USE AdventureWorks2016
    GO
    SELECT p.ProductID,startdate,enddate, pch.StandardCost, PCH.ModifiedDate
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    group by p.ProductID,startdate,enddate, pch.StandardCost, PCH.ModifiedDate
    having pch.StandardCost > AVG(p.StandardCost)

    Reply
  • SELECT pch.StandardCost, p.ProductID,AVG_StandardCost
    FROM Production.ProductCostHistory pch
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    inner join (Select ProductID, AVG(StandardCost) AVG_StandardCost
    FROM Production.ProductCostHistory group by ProductID) Av on Av.productid=p.ProductID

    Reply
  • nishantcomp2512
    April 22, 2019 8:57 pm

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

    Reply
  • Anton Sabzanov
    April 22, 2019 9:09 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(Z.StandardCost) FROM Production.Product Z WHERE Z.ProductID = pch.ProductID);
    GO

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

    Reply
  • 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
  • Gopinath Srirangan
    April 22, 2019 9:50 pm

    SELECT * FROM Production.ProductCostHistory pch
    WHERE StandardCost > (SELECT StandardCost FROM Production.Product p WHERE p.ProductID = pch.ProductID)

    Reply
  • Jiwesh Chauhan
    April 22, 2019 10:02 pm

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

    Reply
  • Hi Pinal,

    I hope you have a great day.
    Today moning I end up to your page and Jija comment bring to think about query again and I go through query and records in db and I came up with the following query with out using sub query.

    Please review.

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

    Thanks
    Arsalan

    Reply
  • Jigar H Parekh
    April 22, 2019 10:19 pm

    SELECT PCH.* FROM Production.ProductCostHistory PCH
    INNER JOIN Production.Product p ON pch.ProductID = p.ProductID
    WHERE PCH.StandardCost > P.StandardCost

    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(PCHI.StandardCost)
    FROM Production.ProductCostHistory PCHI
    WHERE PCHI.ProductID = P.ProductID
    GROUP BY PCHI.ProductID)

    Reply
  • 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 and (pch.StandardCost>(p.StandardCost))
    group by p.productid,pch.StartDate,pch.EndDate,pch.StandardCost,pch.ModifiedDate
    having pch.StandardCost>avg(p.standardCost)

    Reply
  • Hi All, the winning query will be posted after 8 hours.

    10 people who have similar query will be sorted out. The one on the top will be informed and if she/he does not respond, the next in the line will be informed ever hour till we have a person who is willing to join the workshop :-)

    Reply
  • Ishan Chathuranga
    April 22, 2019 10:43 pm

    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,p.ProductID,pch.StartDate,pch.EndDate,pch.StandardCost,pch.ModifiedDate
    having pch.StandardCost > AVG(p.StandardCost)

    GO

    Reply
  • Hint: The winning answer DO NOT have GROUP BY or HAVING Keywords.

    Here is your chance to change your answer :)

    Reply
    • Ishan Chathuranga
      April 22, 2019 10:57 pm

      with Group by and Having i Got the result you posted.
      Answer :
      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,p.ProductID,pch.StartDate,pch.EndDate,pch.StandardCost,pch.ModifiedDate
      having pch.StandardCost > AVG(p.StandardCost)

      GO

      Just let me know whether this is wrong .

      Reply
    • I’m really excited to hear the winner..

      Reply
  • I hope i’m not in bed if I win it, i’ll be mighty cheesed off ;-). Good luck everyone

    Reply

Leave a Reply