FROM Clause
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. If you provide an alias, you can also create a column alias list for one or more columns of the table.<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.<with_query_name>You can reference a
WITHquery by its name. You can also provide an alias in the same way.<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.LATERALThe
LATERALkeyword can precede a subquery or a function callFROMitem. In Data Cloud SQL, subqueries can always access the attributes of precedingFROMitems, even if theLATERALkeyword isn’t specified.
Sampling
TABLESAMPLE sampling_method ( sample_size ) [ REPEATABLE ( seed ) ]A
TABLESAMPLEclause after a<table_name>specifies the<sampling_method>to use for retrieving a subset of the rows in that table. Table sampling occurs before other filters.The optional
REPEATABLEclause specifies a<seed>number to use for generating random numbers within the sampling method. The seed value can be any non-null floating-point literal. Two queries that specify the same seed and<sample_size>values return the same sample of the table. Different seed values usually produce different samples. If you don’t useREPEATABLE, a new random sample is selected for each query based on a system-generated seed.<sampling_method>Data Cloud SQL supports two sampling methods,
BERNOULLIandSYSTEM.The
BERNOULLImethod scans the whole table and selects or ignores individual rows independently.The
SYSTEMmethod does block-level sampling. TheSYSTEMmethod is faster than theBERNOULLImethod for small sampling percentages, but it can return a less random sample.<sample_size>The
<sample_size>argument determines the number of rows to be sampled from the table. Provide a fixed number of rows by using theROWSsyntax, or provide a percentage of rows by using thePERCENTsyntax. Use a numeric literal for the<sample_size>. Data Cloud SQL usesPERCENTby default.Optionally, you can use the
BETWEENclause withPERCENTsample size to define the minimum and maximum number of rows to return. Use theUNBOUNDEDkeyword to leave the minimum or maximum undefined.
The lower bound isn’t guaranteed. If the table has too few rows, the sample can be smaller than the given lower bound of PERCENT or the given row count of ROWS.
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 DMOs and 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.
NATURALNATURALis aUSINGlist with all columns in the two tables that have matching names. If there are no common column names,NATURALreturns all items.
from_item can be one of:
sample_size can be one of:
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.
This example shows how to use a function in the FROM clause. Data Cloud SQL doesn’t support user-defined table functions, but some built-in table functions are supported.