Declarative Objectivity (DO) Language : Data Clauses : RETURN Clause
RETURN Clause
A RETURN clause specifies the values to be returned from a DO statement’s source objects.
Syntax 
returnClause : RETURN '*' | RETURN projection (',' projection)*;
projection : (derivedValue | aggregateValue) alias?;
alias : AS name | AS string;
Used In 
DELETE Statement, RETURN Statement, UPDATE Statement
Quick Look 
Return all customers with more than 1000 reward points.
FROM Customers WHERE rewardPoints > 1000 RETURN *;
Return the number of customers with more than 1000 reward points.
FROM Customers WHERE rewardPoints > 1000 RETURN COUNT();
Return just the last name, user identifier, and postal code of each customer with more than 1000 points:
FROM Customers WHERE rewardPoints > 1000 RETURN lastName, userId, address.postalCode;
Return the list of rental contracts associated with each customer who has more than 1000 points:
FROM Customers WHERE rewardPoints > 1000 RETURN rentals;
Return the tracking numbers of the rental contracts associated with each customer who has more than 1000 points:
FROM Customers WHERE rewardPoints > 1000 RETURN rentals.trackingNumber;
Return all customer names in which the characters are uppercased:
FROM Customers RETURN UPPER(lastName), UPPER(firstName);
Return the make and model of vehicle models whose car manufacturer begins with 'T'.
FROM VehicleModel WHERE brand =~ '^T.*' RETURN brand AS 'Make', modelName AS 'Model';
Discussion 
A RETURN clause can occur in:
A RETURN Statement to specify the values returned by the query.
An UPDATE Statement to report what was modified.
A DELETE Statement to report the deleted objects.
An alternate name for RETURN is SELECT; see Alternative Syntax.
A RETURN clause specifies the values to be returned from a DO statement’s qualified source objects, which are identified by the statement’s query source and optionally filtered by a WHERE Clause.
A RETURN clause consists of one or more comma-separated projections, where each projection consists of an expression and an optional alias; see Aliases. The expression in a projection may:
Evaluate to individual values that are selected or derived from the qualified source objects; see Returning Derived Values.
Evaluate to a single value that combines, or aggregates, values obtained from the qualified source objects; see Returning an Aggregate Value.
For details about the RETURN clause and its projections, see Projection Operator.
Returning Derived Values
You can use a RETURN clause to return values that are derived in some way from each of the DO statement’s qualified source objects. A derived value may be the result of:
A reachable-value expression, which returns a particular value that can be reached from a source object. A reachable value may be a direct or indirect attribute value, or an element value from a collection attribute.
The special wildcard expression *, which returns the values of all direct attributes of a source object.
A DO operator expression, which computes a new value from any combination of reachable values.
Example. The RETURN clause in the following statement specifies three reachable-value expressions. When evaluated against a particular source object (a Customer), the first expression (lastName) identifies the value of a direct attribute of the Customer; the second (address.postalCode) identifies the value of an indirect attribute of the Customer; and the third (rentals[0]) identifies the first element of a collection attribute of the Customer.
FROM Customers WHERE rewardPoints > 1000 RETURN lastName, address.postalCode, rentals[0];
Example. The RETURN clause in both of the following statements return all of a source object’s direct attributes. The first statement uses the special wildcard expression. The second statement explicitly lists all of the direct attributes defined by the Customer class:
FROM Customers WHERE rewardPoints > 1000 RETURN *;
FROM Customers WHERE rewardPoints > 1000 RETURN firstName, lastName, address, userId, pw, rewardPoints, rentals;
Example. The RETURN clause in the following statement uses string operator expressions to compute two new strings from a source object’s direct and indirect attributes. The first returned string is the result of nested expressions that concatenate the values of the Customer’s firstName and lastName attributes, and then convert the result to uppercase letters. The second returned string is the result of concatenating the city and state attributes of the Customer’s embedded Address:
FROM Customers RETURN UPPER(firstName + ' ' + lastName), address.city + ' ' + address.state;
Note:When a reachable-value expression in a RETURN clause evaluates to an embedded object or a collection, all of the component values or elements are returned.
Example. The RETURN clause in following statement returns the address.street, address.city, address.state, and address.postalCode values from each Customer’s embedded Address:
FROM Customers WHERE rewardPoints > 1000 RETURN address;
Example. The RETURN clause in following statement returns the list of RentalContract references from each Customer’s rentals attribute. The return value is a list, even if it contains only one element:
FROM Customers WHERE rewardPoints > 1000 RETURN rentals;
Returning an Aggregate Value
You can use a RETURN clause to return aggregate values from the DO statement’s qualified source objects. An aggregate value is the result of an aggregate operator expression in which the operator gathers one value from each of the statement’s qualified source objects and then performs its calculation over the entire set of gathered values. If the statement contains a GROUP BY Clause, the operator gathers values from the objects within each group, and then performs its calculations on a group-by-group basis, returning one aggregate value per group.
Example. The RETURN clause in following statement returns the total number of Customer objects that satisfy the predicate in the WHERE clause. The effect is to find out how many customers live in California:
FROM Customers WHERE address.state == 'CA' RETURN COUNT();
If the statement did not include a WHERE clause, the COUNT operator would return the total number of Customer objects in the dataset.
Example. The RETURN clause in following statement finds just the Customer objects that satisfy the WHERE clause, subdivides them into groups according to their postal code, and then counts the number of customers in each group. The effect is to find out how many customers live in each California zip code:
FROM Customers WHERE address.state == 'CA' GROUP BY address.postalCode RETURN address.postalCode, COUNT();
Aliases
By default, each expression in a RETURN clause is used in the output as a label for the corresponding returned value. When the expression is a class attribute, the returned value is identified by the attribute’s name. For example, the output of following statement identifies each returned value using the string lastName:
FROM Customers WHERE rewardPoints > 1000 RETURN lastName;
You can optionally specify an alias for the value using the AS keyword. Aliases are typically used to make output more readable. This is especially useful when you specify a complex expression. For example, the output of following statement identifies each returned value using the string Full Name:
FROM Customers RETURN UPPER(firstName + ' ' + lastName) AS 'Full Name';
An alias must be enclosed in quotation marks if it contains a space. If the RETURN clause specifies multiple expressions, you can assign an alias to any or all of them.
Projection Operator
The RETURN clause acts as a projection operator because the value or values it returns for each qualified source object is called a projection. In SQL, a projection is a subset of the columns found in a table. In the federated database’s object model, a projection consists of the set of values returned for a source object. A projection may therefore consist of the entire set of the source object’s direct attribute values, a proper subset of those values, some number of values reachable through indirect or collection attributes, or some number of values computed from a combination of these possibilities.
The RETURN clause creates an ad hoc temporary class whose attributes are determined by the clause’s expressions (and aliases). The returned projections are transient objects that instantiate those classes.
For example, the following statement returns transient objects of a temporary class called Customer_Projection, which has exactly two attributes (namely, firstName and lastName).
FROM Customers WHERE rewardPoints > 1000 RETURN firstName, lastName;
If any aliases are assigned, they are used as the attribute names in the corresponding ad hoc projection class. So, the following example returns transient objects of a temporary class called Customer_Projection, which has exactly two attributes called firstName and surname.
FROM Customers WHERE rewardPoints > 1000 RETURN firstName, lastName AS surname;
Alternative Syntax
The RETURN clause can be the leading or trailing clause in the RETURN statement. In addition, the keyword SELECT can be substituted for RETURN. For example, these four statements are equivalent:
FROM Vehicles RETURN * ;
FROM Vehicles SELECT * ;
RETURN * FROM Vehicles ;
SELECT * FROM Vehicles ;