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.Β
Which of the following is used to speed up data retrieval in a relational database?
What does the term "referential integrity" mean in a database context?
In the context of databases, what does the term sharding refer to?
Which database model is based on the mathematical set theory and is the foundation of many modern databases?
Pick the odd one out.
What is the full form of DBMS?
How many types of architecture we have in DBMS
Which of the following joins returns all rows from both tables, filling in NULL values for non-matching rows?
Which of the following is true about Information?
Which of the following storage devices is considered non-volatile?