Review
The real power of the language comes through when we combine all the elements together.
The query on the right illustrates many of the previous concepts working together.
- First we start with all 6 documents FROM the tutorial bucket.
- The WHERE clause eliminates children 10 years old or younger
- Next the GROUP BY forms 3 groups, one for each relation (“friend”, “parent”, “cousin”)
- Then the HAVING clause removes group “parent” (only has 1 member)
- Next the groups are ordered by the average age of the group members descending
- The we skip over one value in the output and limit the result to a single value.
- Finally the expressions in the SELECT clause are projected, showing the grouping criteria (relation), the count of items in the group, and average age of the group members
SELECT t.relation, count(*) as count, avg(c.age) as avg_age
FROM tutorial t
UNNEST t.children c
WHERE c.age > 10
GROUP BY t.relation
HAVING count(*) > 1
ORDER BY avg_age DESC
LIMIT 1 OFFSET 1