GROUP BY clause

GROUP BY clause

group-by-clause:

GROUP BY expression [ , expression ]* [ letting-clause ] 
    [having-clause] ] | [letting-clause]  

letting-clause:

LETTING  alias = expression [(, alias = expression) ]*

having-clause:

HAVING condition

GROUP BY allows you to organize a result set by one or more expressions. It can be used with aggregate functions to group the result set. The optional LETTING and HAVING clauses follow the GROUP BY clause.

Example


SELECT relation, COUNT(*) AS count
    FROM tutorial
        GROUP BY relation 

Results


{
  "results": [
    {
      "count": 2,
      "relation": "friend"
    },
    {
      "count": 1,
      "relation": "coworker"
    },
    {
      "count": 1,
      "relation": "parent"
    },
    {
      "count": 2,
      "relation": "cousin"
    }
  ]
}

Example

SELECT title, type, COUNT(*) AS count FROM catalog GROUP BY type

LETTING Clause


LETTING  alias = expression [(, alias = expression) ]*

Aliases in the LETTING clause create new names that can be referred to in the HAVING, SELECT, and ORDER BY clauses.

HAVING Clause

HAVING condition

The HAVING clause optionally follows the GROUP BY clause. It filters a stream of variable bindings, retaining those bindings for which the truth value of the HAVING clause is TRUE and discarding the bindings for which this truth value is FALSE.