GQL(Google Query Language) Examples

GQL is Query Language used in Google App Engine for getting entries/keys from Datastore.

  • GQL syntax is similar to that of SQL.
  • A GQL query cannot perform a SQL-like “join” query
  • GQL does not have an OR operator. However, it does have an IN operator, which provides a limited form of OR.
  • A maximum of 30 datastore queries are allowed for any single GQL query.
  • SELECT __key__ queries are faster  than SELECT * queries.
GQL Syntax General Form->

 

 SELECT [* | __key__] FROM <kind>
    [WHERE <condition> [AND <condition> ...]]
    [ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]]
    [LIMIT [<offset>,]<count>]
    [OFFSET <offset>]

  <condition> := <property> {< | <= | > | >= | = | != } <value>
  <condition> := <property> IN <list>
  <condition> := ANCESTOR IS <entity or key>

 

Using Datastore Viewer /Admin

Let us take an Example Table :   Laptop ( Defined with following fields in laptop.java class)

  • Fields –  brand , memory ,processor,cost
1. Login to AppEngine Dashboard of your application.
2. Select Datastore Viewer from menu on the left and Click on Query Tab.
3. In Query Tab click on options to get textbox for entering GQL Queries on your Datastore entries
Example Queries:

 

  • Select Complete Data in Datastore

 

SELECT * FROM Laptop

 

  • Select an Entry with Key

With Encoded Key (Like ag9zfnNhbXBsZS1hc………)

 

SELECT * FROM Laptop WHERE __key__ = KEY('ag9zfnNhbXBsZS1hc.......')

 
With Decoded Key (Like Laptop: name=9000)

 

SELECT * FROM Laptop WHERE __key__ = KEY('Laptop', '9000')

 

  • Select Entries with some Conditions

 

Select all Laptops in Datastore which are from “DELL”  brand

 

SELECT * FROM Laptop WHERE brand='DELL'

 

Select all Laptops in Datastore which are from “DELL”  brand and Order then with Ascending Cost

 

SELECT * FROM Laptop WHERE brand='DELL' ORDER BY cost ASC

 

Select all Laptops in Datastore where all fields are NULL
SELECT * FROM Laptop WHERE brand=NULL AND cost=NULL AND memory=NULL AND processor=NULL

 

GQL Reference on Google Code