Group by having aggregation

Write codeMedium25 minSnowflake and Databricks style

Your task

You are implementing the GROUP BY / HAVING stage of a query engine. Given a list of rows, each a dict with keys 'dept' (string) and 'salary' (int), group the rows by 'dept', compute the average salary per group, and return only the groups whose average salary is strictly greater than threshold. Return a list of [dept, avg_salary] pairs where avg_salary is the integer floor of the average. Order the result by dept ascending. There are at most 10000 rows; salaries are non-negative.

Examples

having_avg_salary(rows: list[dict], threshold: int) -> list[list]
Example 1
Inputhaving_avg_salary([{"dept": "eng", "salary": 100}, {"dept": "eng", "salary": 200}, {"dept": "sales", "salary": 50}], 80)
Output[["eng", 150]]