A newer version of this documentation is available.

View Latest

N1QL Language Reference

  • concept
    The N1QL reference guide describes the N1QL language structure and syntax. It provides reference information about the basic elements in N1QL which can be combined to build N1QL statements.

    The N1QL language is composed of statements, expressions, and comments.


    N1QL statements are categorized into the following groups:


    The following are the different types of N1QL expressions:

    Nested Path Expressions

    In N1QL, nested paths indicate an expression to access nested sub-documents within a JSON document or expression.

    For example, the latitude of a location in the airport keyspace is in the geo sub-document, and can be addressed as the nested path geo.lat:

    SELECT airportname, city, geo, ROUND(geo.lat) AS latitude
    FROM `travel-sample`.inventory.airport t1
    LIMIT 1;
        "airportname": "Calais Dunkerque",
        "city": "Calais",
        "geo": {
          "alt": 12,
          "lat": 50.962097,
          "lon": 1.954764
        "latitude": 51,

    You can use nested operators to access sub-document fields within a document.


    N1QL supports block comments and line comments.

    Block Comments

    block-comment ::= '/*' ( text | newline )* '*/'
    Syntax diagram

    A block comment starts with /* and ends with */. The query engine ignores the start and end markers /* */, and any text between them.

    A block comment may start on a new line, or in the middle of a line after other N1QL statements. A block comment may contain line breaks.

    There may also be further N1QL statements on the same line after the end of a block comment — the query engine does not ignore these.

    Line Comments

    line-comment ::=  '--' text?
    Syntax diagram

    You can use line comments in Couchbase Server 6.5 and later. A line comment starts with two hyphens --. The query engine ignores the two hyphens, and any text following them up to the end of the line.

    A line comment may start on a new line, or in the middle of a line after other N1QL statements. A line comment may not contain line breaks.

    Optimizer Hints

    (Introduced in Couchbase Server 7.1)

    In Couchbase Server 7.1 and later, you can supply hints to the optimizer within a specially-formatted hint comment. For further details, refer to Optimizer Hints.