GQL is a SQL-like language for retrieving entities or keys from the App Engine scalable datastore. While GQL's features are different from those of a query language for a traditional relational database, the GQL syntax is similar to that of SQL.
Syntax
The GQL syntax can be summarized as follows:
SELECT [DISTINCT] [* | <property list> | __key__] [FROM <kind>] [WHERE <condition> [AND <condition> ...]] [ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]] [LIMIT [<offset>,]<count>] [OFFSET <offset>] <property list> := <property> [, <property> ...] <condition> := <property> {< | <= | > | >= | = | != } <value> <condition> := <property> IN <list> <condition> := ANCESTOR IS <entity or key> <list> := (<value> [, <value> ...]])
As with SQL, GQL keywords are case insensitive. Kind and property names are case sensitive.
A GQL query returns zero or more entire entities,
projected entities
,
or
keys
of the requested kind. Every GQL query always begins with
SELECT *
,
SELECT __key__
or SELECT
<property list>
, where
property
is a comma delimited list of one or more entity properties
to be returned from the query. (A GQL query cannot perform a SQL-like "join"
query.)
Tip:
SELECT __key__ or SELECT
<property list>
queries are faster and use less CPU
time than
SELECT *
queries.
The optional
DISTINCT
(experimental)
clause
specifies that only completely unique results will be returned in a result set. This will
only return the first result for entities which have the same values for the properties
that are being projected.
The optional
FROM
clause limits the result set to those entities
of the given kind. A query without a
FROM
clause is called a
kindless query and cannot include filters on properties.
The optional
WHERE
clause filters the result set to those
entities that meet one or more conditions. Each condition compares a property
of the entity with a value using a comparison operator. If multiple conditions
are given with the
AND
keyword, then an entity must meet all of the
conditions to be returned by the query. GQL does not have an
OR
operator. However, it does have an
IN
operator, which provides a
limited form of
OR
.
The
IN
operator compares value of a property to each item in a
list. The
IN
operator is equivalent to many
=
queries,
one for each value, that are ORed together. An entity whose value for the given
property equals any of the values in the list can be returned for the query.
Note:
The
IN
and
!=
operators
use multiple queries behind the scenes. For example, the
IN
operator executes a separate underlying datastore query for every item in the
list. The entities returned are a result of the cross-product of all the
underlying datastore queries and are de-duplicated. A maximum of 30 datastore
queries are allowed for any single GQL query.
A condition can also test whether an entity has a given entity as an
ancestor, using the
ANCESTOR IS
operator. The value is a model
instance or
key
for the ancestor entity. For more information on ancestors, see
Keys and Entity Groups
.
The left-hand side of a comparison is always a property name. A typical
property name consists of alphanumeric characters optionally mixed with
underscores and dots. In other words, they match the regular expression
[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*
.
Caution:
Property names
containing other printable characters must be quoted with double-quotes. For
example:
"first-name"
. Spaces or non-printable characters in
property names are not supported.
The right-hand side of a comparison can be one of the following (as appropriate for the property's data type):
-
a
str
literal, as a single-quoted string. Single-quote characters in the string must be escaped as''
. For example:'Joe''s Diner'
-
an integer or floating point number literal. For example:
42.7
-
a Boolean literal, as
TRUE
orFALSE
. -
the
NULL
literal, which represents the null value (None
in Python). -
a datetime, date, or time literal, with either numeric values or a string
representation, in the following forms:
-
DATETIME( year , month , day , hour , minute , second )
-
DATETIME(' YYYY-MM-DD HH:MM:SS ')
-
DATE( year , month , day )
-
DATE(' YYYY-MM-DD ')
-
TIME( hour , minute , second )
-
TIME(' HH:MM:SS ')
-
- an entity key literal, with either a string-encoded key or a complete path of kinds and key names/IDs :