DP-420 · Question #173
DP-420 Question #173: Real Exam Question with Answer & Explanation
The question requires completing an Azure Cosmos DB SQL query to calculate the average age of pets older than two years for each owner by correctly using 'VALUE' for projection and 'IN' for iterating through an array.
Question
Drag and Drop Question You have an Azure Cosmos DB container named owners. Each item in owners represents an owner and their pets. The following is a sample of the JSON You are writing a query that will retrieve the average age of each pet owned by each owner, if the pet is older than two years. How should you complete the query? To answer, drag the appropriate values to the correct targets. Each value may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content. NOTE: Each correct selection is worth one point. Answer:
Explanation
The question requires completing an Azure Cosmos DB SQL query to calculate the average age of pets older than two years for each owner by correctly using 'VALUE' for projection and 'IN' for iterating through an array.
Approach. The correct interaction is to drag 'VALUE' to the first blank target and 'IN' to the second blank target.
-
The subquery
FROM p [BLANK 2] c.petsis designed to iterate over thepetsarray within each owner's document (c). In Azure Cosmos DB SQL, theINkeyword is used to flatten an array, treating each element as a distinct item that can be aliased (in this case, asp). Therefore, 'IN' correctly completesFROM p IN c.pets. -
The subquery's
SELECT [BLANK 1] (p.age)aims to project the age of each pet (p.age) that satisfies theWHERE p.age > 2condition. When you want to project a raw value (likep.age) directly without wrapping it in a JSON object,SELECT VALUEis the appropriate syntax. This creates a simple array of age values that can then be aggregated by the outerAVG(PetAges)function. Therefore, 'VALUE' correctly completesSELECT VALUE (p.age).
Common mistakes.
- common_mistake. Using 'JOIN' for the second blank ('FROM p [BLANK 2] c.pets') is incorrect because 'JOIN' is used to combine documents from different collections or to join a self-referencing document, not to iterate over elements within an array of a single document. Using 'COUNT', 'SUM', or 'AS' for the first blank ('SELECT [BLANK 1] (p.age)') would also be incorrect. 'COUNT' and 'SUM' are aggregate functions that would return a single number, not a collection of individual pet ages needed for the outer AVG function. 'AS' is used for aliasing properties, and while
SELECT p.age AS PetAgeis valid, it would return an object{ 'PetAge': <age_value> }for each pet, making it unsuitable for direct averaging asAVG()expects a numerical value, and the outerAVG(PetAges)suggestsPetAgesis an array of values, not an array of objects.
Concept tested. Azure Cosmos DB SQL API querying, specifically involving subqueries for array iteration, the IN keyword for flattening arrays, and the SELECT VALUE keyword for projecting raw scalar values for aggregation.
Topics
Community Discussion
No community discussion yet for this question.