The FROM clause defines one or more source tables as the data source. If multiple sources are specified, the result is the join of the sources. Use the FROM clause with the WHERE clause to reduce the number of returned values in the join.
The FROM clause can contain these elements.
-
<table_name>- : The name of an existing table or view.
-
<alias>- : A substitute name for the
FROMitem. When you provide an alias, the alias replaces the name for theSELECTcommand. For example,FROM foo AS f, the rest of theSELECTtofnotfoo. TheASkeyword is optional in Data 360 SQL. You can writeFROM foo finstead. If you provide an alias, you can also create a column alias list for one or more columns of the table.
- : A substitute name for the
-
<select>- : The
<select>is a subquery of the mainSELECTcommand. Surround the subquery in parentheses. If you don’t provide an alias, the columns of the subquery are only accessible if their names are unique. You can use VALUES as subquery.
- : The
-
<with_query_name>- : You can reference a
WITHquery by its name. You can also provide an alias in the same way.
- : You can reference a
-
<function_name>- : Function calls can appear in the
FROMclause. The function’s output is created as a temporary table for the duration of theSELECTcommand. You can provide a function alias and column alias list for one or more attributes of the function’s composite return type.
- : Function calls can appear in the
-
LATERAL- : The
LATERALkeyword can precede a subquery or a function callFROMitem. In Data 360 SQL, subqueries can always access the attributes of precedingFROMitems, even if theLATERALkeyword isn't specified.
- : The
Joins
A JOIN clause combines two FROM items. Use parentheses to determine the order of nesting. In the absence of parentheses, JOIN nests left to right. JOIN binds more tightly than the commas separating FROM-list items. For examples of joins, see Join Records from Different Data Model Objects (DMOs) and Data Lake Objects (DLOs).
For the INNER and OUTER join types, specify the NATURAL, ON join_condition, or
USING (join_column [, ...]) joins conditions.
-
<join_type>-
:
CROSS JOINandINNER JOINreturn all of the results of the provided items.LEFT OUTER JOINreturns all combined rows that pass its join condition and each row in the left-hand table. This join inserts null values in the left-hand row for the right-hand unmatched columns. TheJOINclause’s condition is considered to match rows. Outer conditions are applied after the join.RIGHT OUTER JOINreturns all combined rows that pass its join condition and each row in the right-hand table. This join inserts null values in the right-hand row for the left-hand unmatched columns.FULL OUTER JOINreturns all the joined rows, the unmatched left-hand rows (extended with nulls on the right), and the unmatched right-hand rows (extended with nulls on the left).
-
-
ON <join_condition>- :
ON <join_condition>is an expression that results in abooleanthat specifies the rows in a join that match.
- :
-
USING ( <join_column> [, ...] )- : One of each pair of equivalent columns is included in the join output.
-
NATURAL- :
NATURALis aUSINGlist with all columns in the two tables that have matching names. If there are no common column names,NATURALreturns all items.
- :
Syntax
SELECT [ ALL | DISTINCT [ ON ( <expression> [, ...] ) ] ]
[ FROM <from_item> [, ...] ]
...
from_item can be one of:
<table_name> [ [ AS ] <alias> [ ( <column_alias> [, ...] ) ] ]
<with_query_name> [ [ AS ] <alias> [ ( <column_alias> [, ...] ) ] ]
[ LATERAL ] ( <select> ) [ AS ] [<alias>] [ ( <column_alias> [, ...] ) ]
[ LATERAL ] <function_name> ( [ <argument> [, ...] ] )
[ [ AS ] <alias> [ ( <column_alias> [, ...] ) ] ]
<from_item> [ NATURAL ] <join_type> <from_item>
[ ON <join_condition> | USING ( <join_column> [, ...] ) ]
Examples
This example shows how to obtain the union of the tables distributors and actors, limiting the results to those that begin with the letter W in each table.
Distributors:
did | name
----+--------------
108 | Westward
111 | Walt Disney
112 | Warner Bros.
Actors:
id | name
----+----------------
1 | Woody Allen
2 | Warren Beatty
3 | Walter Matthau
SELECT distributors.name
FROM distributors
WHERE distributors.name LIKE 'W%'
UNION
SELECT actors.name
FROM actors
WHERE actors.name LIKE 'W%';
Results:
name
----
Walt Disney
Walter Matthau
Warner Bros.
Warren Beatty
Westward
Woody Allen
This example shows how to use a function in the FROM clause. Data 360 SQL doesn’t support user-defined table functions, but some built-in set-returning functions are supported, including result_scan for querying the results of a previously executed query.
SELECT * FROM generate_series(1, 5);
Results:
generate_series
---------------
1
2
3
4
5