Question
Consider a table Orders with columns OrderID,
CustomerID, OrderDate, and TotalAmount. Which SQL query would correctly find the total number of orders and the average TotalAmount for each CustomerID who has placed more than 5 orders? Option A) SELECT CustomerID, COUNT(OrderID), AVG(TotalAmount) FROM Orders GROUP BY CustomerID WHERE COUNT(OrderID) > 5; Option B) SELECT CustomerID, COUNT(OrderID), AVG(TotalAmount) FROM Orders WHERE COUNT(OrderID) > 5 GROUP BY CustomerID; Option C) SELECT CustomerID, COUNT(OrderID), AVG(TotalAmount) FROM Orders GROUP BY CustomerID HAVING COUNT(OrderID) > 5; Option D) SELECT CustomerID, COUNT(OrderID), AVG(TotalAmount) FROM Orders HAVING COUNT(OrderID) > 5 GROUP BY CustomerID; Option E) None of the aboveSolution
SELECT CustomerID, COUNT(OrderID), AVG(TotalAmount) FROM Orders GROUP BY CustomerID HAVING COUNT(OrderID) > 5;Â Explanation: The WHERE clause is used to filter individual rows *before* grouping, and it cannot contain aggregate functions. The HAVING clause is used to filter groups *after* the GROUP BY clause has been applied, and it can use aggregate functions. The correct order of execution in a SELECT statement is FROM, JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. Therefore, to filter groups based on an aggregate condition (COUNT(OrderID) > 5), the HAVING clause must be used after GROUP BY.Â
Match the following musical instruments with the maestros who play them.
 Bharat Parv is a five – day annual event that celebrates the spirit of India is being organised virtually from January 26, 2021 till January ...
Which of the following set of countries participated in the Malabar Naval Exercise 2020?
What is the minimum capital-to-risk-weighted assets ratio required for Infrastructure Debt Fund-NBFCs (IDF-NBFCs) under the revised guidelines by the Re...
Who was given the Dronacharya Award 2017 for coaching in boxing?
Who is the new head of Europeon Parliament elected in January 2022 ?
The 'Sugar Bowl of the World' is:
Where did Rani Lakshmi Bai die fighting the British?
Revenue account includes items related to the _______.
Which of the following is true ?Â