SQL-like SELECT statement
The SQL-like SELECT statement defines a tabular output. It introduces the GROUP BY clause, as well as internal table structures which can be printed.
EBNF for SQL-Like Select Statement
1
sqlSelectBlock := sqlSelectClause
2
fromClause
3
[whereClause]
4
[groupByClause]
5
[havingClause]
6
[orderClause]
7
[limitClause]
8
9
sqlSelectClause := SELECT [DISTINCT] columnExpr ("," columnExpr)*
10
INTO tableName
11
columnExpr := expr [AS columnName]
12
| aggregator "("[DISTINCT] expr ")" [AS columnName]
13
columnName := name
14
tableName := name
Copied!
Just as in SQL, the SELECT keyword is followed by an ordered list of expressions (columnExpr), each expression defining one column in the output table. The expressions can refer to the attributes or accumulators of the vertices and edges selected in the FROM clause of this query. A column can apply one of the aggregator functions (COUNT, SUM, AVG, MIN, MAX), so that the table aggregates the raw data into summary results. Each column can be given an alias for its heading (AS columnName).
If some columns are aggregated, the non-aggregated (grouping) columns must come first.
Unlike the classic GSQL SELECT statement which assigns its output to a vertex set variable, the SQL-like form uses INTO tableName to store the results in a table. tableName can be any identifier that has not been used in the query before.
DISTINCT has the same meaning as it does in SQL: values that appear more than once in the raw data should only be used once. It can be used either individually for aggregated columns (e.g., count how many unique values there are) or collectively so that there are no repeated rows in the table.
The SQL-like form may use the GROUP BY clause but may not use the ACCUM and POST-ACCUM clauses.
This feature is only available in syntax v2. Ensure that the correct syntax version is specified in the CREATE QUERY header.

Example:

In the example graph socialNet, which has vertex types person and post, and edge type liked for when someone likes a post, use a query to retrieve timestamps of all likes, the subject of the posts being liked, and the primary IDs of the people who liked the posts.
Query
Output
1
CREATE QUERY getLikes() FOR GRAPH socialNet SYNTAX v2 {
2
SELECT l.actionTime, pt.subject, p INTO T
3
FROM person:p -(liked>:l)- post:pt;
4
PRINT T;
5
}
Copied!
1
[
2
{
3
"Table1": [
4
{
5
"actionTime": "2010-01-12 11:22:05",
6
"p": "person7",
7
"subject": "cats"
8
},
9
{
10
"actionTime": "2010-01-12 21:12:05",
11
"p": "person5",
12
"subject": "tigergraph"
13
},
14
{
15
"actionTime": "2010-01-11 11:32:00",
16
"p": "person1",
17
"subject": "Graphs"
18
},
19
{
20
"actionTime": "2010-01-13 03:16:05",
21
"p": "person4",
22
"subject": "coffee"
23
},
24
{
25
"actionTime": "2010-01-12 10:52:15",
26
"p": "person2",
27
"subject": "Graphs"
28
},
29
{
30
"actionTime": "2010-01-11 16:02:26",
31
"p": "person2",
32
"subject": "cats"
33
},
34
{
35
"actionTime": "2010-01-16 05:15:53",
36
"p": "person3",
37
"subject": "Graphs"
38
},
39
{
40
"actionTime": "2010-01-11 03:26:05",
41
"p": "person8",
42
"subject": "coffee"
43
},
44
{
45
"actionTime": "2010-01-14 11:23:05",
46
"p": "person6",
47
"subject": "cats"
48
}
49
]
50
}
51
]
Copied!

GROUP BY Clause

The optional GROUP BY clause acts just as it does in basic SQL: it groups and merge rows of data together, so that the output table summarizes the data, rather than showing every individual tuple which matches the SELECT and FROM clauses.
EBNF for GROUP BY clause
1
groupByClause := GROUP BY groupExpr ("," groupExpr)*
2
groupExpr = expr
Copied!
The SELECT clause's list of column expressions defines a set of tuples or rows. All the tuples which have the same value for groupExpr are grouped together. If there is a second groupExpr, then this is used to subdivide each group into subgroups, and so on through the list of groupExpr from left to right.

Coordinating SELECT and GROUP BY

The SELECT clause and GROUP BY clause must be coordinated. Each expression groupExpr may be a vertex or edge alias from the FROM clause, or an attribute of a vertex or edge alias. Furthermore, each groupExpr must either be the same as or the basis of a SELECT columnExpr. In the SELECT clause, any columns which are not associated with a groupExpr must be be aggregated (with COUNT, SUM, AVG, MIN or MAX) and must be at the end of the list of columns. For example, in the workNet graph, if we have this FROM clause: FROM person:p -(worksFor:w)- company:c and if we want columns for company's country, whether working full time or not, and employees, with no grouping we could have
1
SELECT c.country, w.fulltime, p INTO T
2
FROM person:p -(worksFor:w)- company:c
Copied!
The non-grouped output would look like this:
1
[{"T": [
2
{"country": "us","fullTime": true,"p": "person3"},
3
{"country": "us","fullTime": true,"p": "person6"},
4
{"country": "us","fullTime": true,"p": "person10"},
5
...
Copied!
If we want to group by country and then by work status, we could have this:
Coordination between SELECT columns and GROUP BY expressions
1
SELECT c.country, w.fulltime, COUNT(p) AS numEmployees INTO T
2
FROM person:p -(worksFor:w)- company:c
3
GROUP BY c.country, w.fulltime
Copied!
Then the grouped output would look like this:
1
[{"T": [
2
{"country": "us","fullTime": true,"numEmployees": 7},
3
{"country": "chn","fullTime": false,"numEmployees": 4},
4
{"country": "chn","fullTime": true,"numEmployees": 2},
5
...
Copied!

Implied GROUP BY

If the SELECT clause contains aggregator functions, the GROUP BY clause can be omitted. Instead, GSQL will assume that every SELECT expression that is not aggregrated is to be used for grouping, in left-to-right order.

Examples

Example 1 (grouping and aggregation): For each employee, find the number of its employers
Query
Output
1
CREATE QUERY tabularEx1() FOR GRAPH workNet SYNTAX v2 {
2
SELECT p AS employee, count(c) AS employerCount INTO T
3
FROM person:p -(worksFor)- company:c
4
GROUP BY p;
5
6
PRINT T;
7
}
Copied!
1
{"version":{"edition":"enterprise",
2
"api":"v2",
3
"schema":0},
4
"error":false,
5
"message":"",
6
"results":[{"T":[
7
{"employee":"person6","employerCount":1},
8
{"employee":"person7","employerCount":2},
9
{"employee":"person12","employerCount":1},
10
{"employee":"person3","employerCount":1},
11
{"employee":"person11","employerCount":1},
12
{"employee":"person4","employerCount":1},
13
{"employee":"person9","employerCount":2},
14
{"employee":"person10","employerCount":2},
15
{"employee":"person1","employerCount":2},
16
{"employee":"person5","employerCount":1},
17
{"employee":"person2","employerCount":2},
18
{"employee":"person8","employerCount":1}]}]}
Copied!
Example 2 (HAVING clause): Find persons with at least 2 employers.
Query
Output
1
CREATE QUERY tabularEx2() FOR GRAPH workNet SYNTAX v2 {
2
SELECT p AS employee, count(c) AS employerCount INTO T
3
FROM person:p -(worksFor)- company:c
4
GROUP BY p
5
HAVING employerCount > 1;
6
7
PRINT T;
8
}
Copied!
1
{
2
"error": false,
3
"message": "",
4
"version": {
5
"schema": 0,
6
"edition": "enterprise",
7
"api": "v2"
8
},
9
"results": [{"T": [
10
{"employee": "person2","employerCount": 2},
11
{"employee": "person1","employerCount": 2},
12
{"employee": "person7","employerCount": 2},
13
{ "employee": "person10","employerCount": 2},
14
{"employee": "person9","employerCount": 2}
15
]}]
16
}
Copied!
Example 2a (implicit grouping): Same as Example 2, but with implicit grouping from the SELECT clause.
1
CREATE QUERY tabularEx2a() FOR GRAPH workNet SYNTAX v2 {
2
SELECT p AS employee, count(c) AS employerCount INTO T
3
FROM person:p -(worksFor)- company:c
4
HAVING employerCount > 1;
5
6
PRINT T;
7
}
Copied!
The output is the same as for Example 2.
Example 3 (grouping, aggregation, order by and limit): Group employees by country and by work status, sorted by group size and then by country name.
Query
Output
1
CREATE QUERY tabularEx3() SYNTAX v2 {
2
SELECT c.country, w.fullTime, COUNT(p) AS numEmployees INTO T
3
FROM person:p -(worksFor:w)- company:c
4
GROUP BY c.country, w.fullTime
5
ORDER BY numEmployees DESC, c.country ASC
6
LIMIT 10;
7
8
PRINT T;
9
}
Copied!
1
{
2
"error": false,
3
"message": "",
4
"version": {
5
"schema": 0,
6
"edition": "enterprise",
7
"api": "v2"
8
},
9
"results": [{"T": [
10
{"country":"us", "numEmployees":7, "fullTime":true},
11
{"country":"chn", "numEmployees":4, "fullTime":false},
12
{"country":"chn", "numEmployees":2, "fullTime":true},
13
{"country":"jp", "numEmployees":2, "fullTime":false},
14
{"country":"can", "numEmployees":1, "fullTime":true},
15
{"country":"jp", "numEmployees":1, "fullTime":true}
16
]}]
17
}
Copied!
Last modified 2mo ago
Export as PDF
Copy link