- Composite SQL Queries
- User-Defined Functions
- JavaScript Integration
- Linq to SQL Translation
- DocumentDB SQL - Built-in Function
- DocumentDB SQL - Parameterized
- DocumentDB - Scalar Expressions
- DocumentDB SQL - Array Creation
- DocumentDB SQL - Aliasing
- DocumentDB SQL - Joins
- DocumentDB SQL - Iteration
- DocumentDB SQL - Order By Clause
- DocumentDB SQL - Value Keyword
- DocumentDB SQL - In Keyword
- DocumentDB - Between Keyword
- DocumentDB SQL - Operators
- DocumentDB SQL - Where Clause
- DocumentDB SQL - From Clause
- DocumentDB SQL - Select Clause
- DocumentDB SQL - Overview
- DocumentDB SQL - Home
DocumentDB SQL Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
DocumentDB SQL - Order By Clause
Microsoft Azure DocumentDB supports querying documents using SQL over JSON documents. You can sort documents in the collection on numbers and strings using an ORDER BY clause in your query. The clause can include an optional ASC/DESC argument to specify the order in which results must be retrieved.
We will consider the same documents as in the previous examples.
Following is the AndersenFamily document.
{ "id": "AndersenFamily", "lastName": "Andersen", "parents": [ { "firstName": "Thomas", "relationship": "father" }, { "firstName": "Mary Kay", "relationship": "mother" } ], "children": [ { "firstName": "Henriette Thaulow", "gender": "female", "grade": 5, "pets": [ { "givenName": "Fluffy", "type": "Rabbit" } ] } ], "location": { "state": "WA", "county": "King", "city": "Seattle" }, "isRegistered": true }
Following is the SmithFamily document.
{ "id": "SmithFamily", "parents": [ { "familyName": "Smith", "givenName": "James" }, { "familyName": "Curtis", "givenName": "Helen" } ], "children": [ { "givenName": "Michelle", "gender": "female", "grade": 1 }, { "givenName": "John", "gender": "male", "grade": 7, "pets": [ { "givenName": "Tweetie", "type": "Bird" } ] } ], "location": { "state": "NY", "county": "Queens", "city": "Forest Hills" }, "isRegistered": true }
Following is the WakefieldFamily document.
{ "id": "WakefieldFamily", "parents": [ { "familyName": "Wakefield", "givenName": "Robin" }, { "familyName": "Miller", "givenName": "Ben" } ], "children": [ { "familyName": "Merriam", "givenName": "Jesse", "gender": "female", "grade": 6, "pets": [ { "givenName": "Charpe Brown", "type": "Dog" }, { "givenName": "Tiger", "type": "Cat" }, { "givenName": "Princess", "type": "Cat" } ] }, { "familyName": "Miller", "givenName": "Lisa", "gender": "female", "grade": 3, "pets": [ { "givenName": "Jake", "type": "Snake" } ] } ], "location": { "state": "NY", "county": "Manhattan", "city": "NY" }, "isRegistered": false }
Let’s take a look at a simple example.
Following is the query which contains the ORDER BY keyword.
SELECT f.id, f.children[0].givenName,f.children[0].grade FROM Famipes f ORDER BY f.children[0].grade
When the above query is executed, it produces the following output.
[ { "id": "SmithFamily", "givenName": "Michelle", "grade": 1 }, { "id": "AndersenFamily", "grade": 5 }, { "id": "WakefieldFamily", "givenName": "Jesse", "grade": 6 } ]
Let’s consider another simple example.
Following is the query which contains the ORDER BY keyword and DESC optional keyword.
SELECT f.id, f.parents[0].familyName FROM Famipes f ORDER BY f.parents[0].familyName DESC
When the above query is executed, it will produce the following output.
[ { "id": "WakefieldFamily", "familyName": "Wakefield" }, { "id": "SmithFamily", "familyName": "Smith" }, { "id": "AndersenFamily" } ]Advertisements