You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Learn about the SQL syntax, and example for FROM clause for Azure Cosmos DB. This article also shows examples to scope results, and get sub items by using the FROM clause.
4
-
author: seesharprun
2
+
title: FROM
3
+
titleSuffix: Azure Cosmos DB for NoSQL
4
+
description: An Azure Cosmos DB for NoSQL clause that identifies the source of data for a query.
The FROM (`FROM <from_specification>`) clause is optional, unless the source is filtered or projected later in the query. A query like `SELECT * FROM Families` enumerates over the entire `Families` container. You can also use the special identifier ROOT for the container instead of using the container name.
15
+
#FROM clause (NoSQL query)
17
16
18
-
The `FROM` clause enforces the following rules per query:
*The container can be aliased, such as `SELECT f.id FROM Families AS f` or simply `SELECT f.id FROM Families f`. Here `f` is the alias for `Families`. AS is an optional keyword to [alias](working-with-json.md#aliasing)the identifier.
19
+
The FROM (``FROM <from_specification>``) clause is optional, unless the source is filtered or projected later in the query. A query like ``SELECT * FROM products`` enumerates over an entire container regardless of the name. You can also use the special identifier ``ROOT`` for the container instead of using the container name.
21
20
22
-
* Once aliased, the original source name cannot be bound. For example, `SELECT Families.id FROM Families f` is syntactically invalid because the identifier `Families` has been aliased and can't be resolved anymore.
21
+
The ``FROM`` clause enforces the following rules per query:
23
22
24
-
* All referenced properties must be fully qualified, to avoid any ambiguous bindings in the absence of strict schema adherence. For example, `SELECT id FROM Families f` is syntactically invalid because the property `id` isn't bound.
23
+
- The container can be aliased, such as ``SELECT p.id FROM products AS p`` or simply ``SELECT p.id FROM products p``. Here, ``p`` is the alias for the container. The container doesn't necessarily need to be named ``products`` or ``p``. ``AS`` is an optional keyword to [alias](working-with-json.md#aliasing) the identifier.
24
+
- Once aliased, the original source name can't be bound. For example, ``SELECT products.id FROM products p`` is syntactically invalid because the identifier ``products`` has been aliased and can't be resolved anymore.
25
+
- All referenced properties must be fully qualified, to avoid any ambiguous bindings in the absence of strict schema adherence. For example, ``SELECT id FROM products p`` is syntactically invalid because the property ``id`` isn't bound. The query should instead reference the property ``id`` using ``p.id`` (or ``<alias>.<property-name>``).
Specifies a data source, with or without an alias. If alias is not specified, it will be inferred from the `<container_expression>` using following rules:
51
-
52
-
- If the expression is a container_name, then container_name will be used as an alias.
53
-
54
-
- If the expression is `<container_expression>`, then property_name, then property_name will be used as an alias. If the expression is a container_name, then container_name will be used as an alias.
55
-
56
-
- AS `input_alias`
57
-
58
-
Specifies that the `input_alias` is a set of values returned by the underlying container expression.
59
-
60
-
-`input_alias` IN
61
-
62
-
Specifies that the `input_alias` should represent the set of values obtained by iterating over all array elements of each array returned by the underlying container expression. Any value returned by underlying container expression that is not an array is ignored.
63
-
64
-
-`<container_expression>`
65
-
66
-
Specifies the container expression to be used to retrieve the documents.
67
-
68
-
-`ROOT`
69
-
70
-
Specifies that document should be retrieved from the default, currently connected container.
71
-
72
-
-`container_name`
73
-
74
-
Specifies that document should be retrieved from the provided container. The name of the container must match the name of the container currently connected to.
75
-
76
-
-`input_alias`
77
-
78
-
Specifies that document should be retrieved from the other source defined by the provided alias.
79
-
80
-
-`<container_expression> '.' property_name`
81
-
82
-
Specifies that document should be retrieved by accessing the `property_name` property.
Specifies that document should be retrieved by accessing the `property_name` property or array_index array element for all documents retrieved by specified container expression.
87
-
48
+
49
+
|| Description |
50
+
| --- | --- |
51
+
|**``<from_source>``**| Specifies a data source, with or without an alias. If alias isn't specified, it's inferred from the ``<container_expression>`` using following rules. If the expression is a ``container_name``, then ``container_name`` is used as an alias. If the expression is ``<container_expression>``, then ``property_name`` is used as an alias. If the expression is a ``container_name``, then ``container_name`` is used as an alias. |
52
+
|**AS ``input_alias``**| Specifies that the ``input_alias`` is a set of values returned by the underlying container expression. |
53
+
|**``input_alias`` IN**| Specifies that the ``input_alias`` should represent the set of values obtained by iterating over all array elements of each array returned by the underlying container expression. Any value returned by underlying container expression that isn't an array is ignored. |
54
+
|**``<container_expression>``**| Specifies the container expression to be used to retrieve the items. |
55
+
|**``ROOT``**| Specifies that the item should be retrieved from the default, currently connected container. |
56
+
|**``container_name``**| Specifies that the item should be retrieved from the provided container. The name of the container must match the name of the container currently connected to. |
57
+
|**``input_alias``**| Specifies that the item should be retrieved from the other source defined by the provided alias. |
58
+
|**``<container_expression> '.' property_name``**| Specifies that the item should be retrieved by accessing the ``property_name`` property. |
59
+
|**``<container_expression> '[' "property_name" \| array_index ']'``**| Specifies that the item should be retrieved by accessing the ``property_name`` property or ``array_index`` array element for all items retrieved by specified container expression. |
60
+
88
61
## Remarks
89
-
90
-
All aliases provided or inferred in the `<from_source>`(s) must be unique. The Syntax `<container_expression> '.' property_name` is the same as `<container_expression> '[' "property_name" ']'`. However, the latter syntax can be used if a property name contains a non-identifier character.
91
-
62
+
63
+
All aliases provided or inferred in the ``<from_source>``(s) must be unique. The Syntax ``<container_expression> '.' property_name`` is the same as ``<container_expression> '[' "property_name" ']'``. However, the latter syntax can be used if a property name contains a nonidentifier character.
64
+
92
65
### Handling missing properties, missing array elements, and undefined values
93
-
94
-
If a container expression accesses properties or array elements and that value does not exist, that value will be ignored and not processed further.
95
-
66
+
67
+
If a container expression accesses properties or array elements and that value doesn't exist, that value is ignored and not processed further.
68
+
96
69
### Container expression context scoping
97
-
98
-
A container expression may be container-scoped or document-scoped:
99
-
100
-
- An expression is container-scoped, if the underlying source of the container expression is either ROOT or `container_name`. Such an expression represents a set of documents retrieved from the container directly, and is not dependent on the processing of other container expressions.
101
-
102
-
- An expression is document-scoped, if the underlying source of the container expression is `input_alias` introduced earlier in the query. Such an expression represents a set of documents obtained by evaluating the container expressionin the scope of each document belonging to the set associated with the aliased container. The resulting set will be a union of sets obtained by evaluating the container expression for each of the documents in the underlying set.
70
+
71
+
A container expression may be container-scoped or item-scoped:
72
+
73
+
- An expression is container-scoped, if the underlying source of the container expression is either ``ROOT`` or ``container_name``. Such an expression represents a set of items retrieved from the container directly, and isn't dependent on the processing of other container expressions.
74
+
75
+
- An expression is item-scoped, if the underlying source of the container expression is ``input_alias`` introduced earlier in the query. Such an expression represents a set of items obtained by evaluating the container expression. This evaluation is performed in the scope of each item belonging to the set associated with the aliased container. The resulting set is a union of sets obtained by evaluating the container expression for each of the items in the underlying set.
103
76
104
77
## Examples
105
78
106
-
### Get subitems by using the FROM clause
107
-
108
-
The FROM clause can reduce the source to a smaller subset. To enumerate only a subtree in each item, the subroot can become the source, as shown in the following example:
109
-
110
-
```sql
111
-
SELECT*
112
-
FROMFamilies.children
113
-
```
114
-
115
-
The results are:
116
-
117
-
```json
118
-
[
119
-
[
120
-
{
121
-
"firstName": "Henriette Thaulow",
122
-
"gender": "female",
123
-
"grade": 5,
124
-
"pets": [
125
-
{
126
-
"givenName": "Fluffy"
127
-
}
128
-
]
129
-
}
130
-
],
131
-
[
132
-
{
133
-
"familyName": "Merriam",
134
-
"givenName": "Jesse",
135
-
"gender": "female",
136
-
"grade": 1
137
-
},
138
-
{
139
-
"familyName": "Miller",
140
-
"givenName": "Lisa",
141
-
"gender": "female",
142
-
"grade": 8
143
-
}
144
-
]
145
-
]
146
-
```
147
-
148
-
The preceding query used an array as the source, but you can also use an object as the source. The query considers any valid, defined JSON value in the source for inclusion in the result. The following example would exclude `Families` that don't have an `address.state` value.
149
-
150
-
```sql
151
-
SELECT*
152
-
FROMFamilies.address.state
153
-
```
154
-
155
-
The results are:
156
-
157
-
```json
158
-
[
159
-
"WA",
160
-
"NY"
161
-
]
162
-
```
79
+
In this first example, the ``FROM`` clause is used to specify the current container as a source, give it a unique name, and then alias it. The alias is then used to project specific fields in the query results.
In this next example, the ``FROM`` clause can also reduce the source to a smaller subset. To enumerate only a subtree in each item, the subroot can become the source. An array or object subroot can be used as a source.
0 commit comments