
from command syntax details
For overview information about the SPL2 from
command, see from command overview.
For examples using the SPL2 from
command, see from command examples.
Syntax
The SPL2 from
command has a flexible syntax, which enables you to start a search with either the FROM
clause or the SELECT
clause.
- If you start a search with the
FROM
clause, you only need to specify a <dataset>. - If you start a search with the
SELECT
clause, you must also specify theFROM
clause.
- For more information about this flexible syntax, see from command usage.
The required syntax is in bold.
- FROM <dataset> [ AS <alias>]
- [ JOIN <dataset> AS <alias> ON <join-condition>... ] ...
- [ WHERE ( <predicate-expression> [<logical-operator> <predicate-expression>] ) ... ]
- [ (GROUP BY | GROUPBY) ( <expression>[,<expression>... ] | span ( <field> ) | span ( <field>, [<int>]<timescale> ) | <field> span=( [<int>]<timescale> ) ]
- [ (SELECT | SELECT DISTINCT) <expression> [, <expression>] ... ]
- [ HAVING <expression> ]
- [ (ORDER BY | ORDERBY) <expression>[,<expression>... ] [ASC | DESC] ]
- [ LIMIT <integer> ]
- [ OFFSET <integer> ]
Required arguments
- dataset
- Syntax <dataset> [ AS <alias> [ <JOIN-TYPE> ] ]
- Description The name of the dataset to retrieve the data from.
- You can specify more than one dataset. Use the
indexes
dataset function if all of the datasets are indexes. See indexes dataset function. Use theunion
command if the datasets are a mixture of dataset types. See union command overview. - When you use the JOIN clause, you must specify an alias. You have the option of specifying the join type. The default join type is INNER. The supported join types are: INNER and LEFT.
Optional arguments
JOIN clause
- JOIN
- Syntax: ( <join-type> ) <dataset> AS <alias> ON <join-condition> [AND <join-condition>]... )...
- Description: You can enrich a dataset specified in the FROM clause with data from one or more datasets specified in the JOIN clause. When you use the JOIN clause, you must specify aliases for each of the datasets. The aliases are used to help identify the fields in each of the datasets.
- A maximum of 50000 rows from the dataset that you specify in the JOIN clause, sometimes referred to as the right-hand side dataset, can be joined with the dataset specified in the FROM clause. This maximum is set to limit the impact of the JOIN clause on performance and resource consumption.
- You have the option of specifying a join type. The default join type is INNER JOIN. You can also specify LEFT JOIN. Valid join types are described in the following table:
Join type Valid keywords Notes Inner join INNER JOIN or JOIN You can specify the keywords in lowercase. The keywords are shown in uppercase for readability. Left join LEFT JOIN or LEFT OUTER JOIN You can specify the keywords in lowercase. The keywords are shown in uppercase for readability. Right join A right join is not supported at this time.
- The <join-condition> specifies the key fields that each dataset has in common, such as a product ID or supplier ID. The syntax for the <join-condition> is <left-alias>.<left-field>=<right-alias>.<right-field>. The
alias
is a unique string you use in the search to identify the dataset. For example, if the dataset isorders
you could use the aliaso
. If theorders
dataset contains the fieldproductID
, the syntax to identify this field iso.productID
.
- You can specify multiple join conditions by using the AND operator between each condition. For example,
ON o.productID=p.pid AND o.supplierID=p.sid
.
- You can specify multiple JOIN clauses in a search. See from command examples.
- Default: None. You must specify a join type.
WHERE clause
- WHERE
- Syntax: WHERE <predicate-expression> [<logical-operator> <predicate-expression>...]
- Description: Use predicate expressions to filter your data. When specifying multiple predicate expressions, you must specify a logical operator between the expressions. For information about and examples of the types of predicate expressions you can specify, see Predicate expressions in the SPL2 Search Manual.
- Use the
WHERE
clause to filter your data before using other clauses that contain aggregations. For example, the following search contains an aggregation in theSELECT
clause. TheWHERE
clause filters the data by narrowing down the events based on a time range. The filtered data is then passed to theSELECT
clause:
FROM main WHERE earliest=-5m@m AND latest=@m GROUP BY host SELECT sum(bytes) AS sum, host HAVING sum > 1024*1024
- The
WHERE
clause supports using wildcards in the <predicate-expression> only with thelike
function. For more information, see Conditional and comparison functions.
- Default: None
GROUP BY clause
- GROUP BY
- Syntax: (GROUP BY | GROUPBY ) ( <expression>[,<expression>... ] | span ( <field> ) | span ( <field>, [<int>]<timescale> ) | <field> span=( [<int>]<timescale> )
- Description: One or more expressions separated by commas. The expression can be a field name or an arbitrary expression such as
first_name + last_name
orupper(first_name)
. See Types of expressions in the SPL2 Search Manual.
- When specifying a field that contains UNIX time, you can also specify a time span.
- You cannot use a wildcard character to specify multiple fields with similar names. You must specify each field separately.
- The
GROUP BY
clause returns one row for each distinct value in the list of fields.
- If the
GROUP BY
clause is specified, theSELECT
clause must also be specified. For examples, see theSELECT
clause section Dependencies between SELECT and GROUP BY.
- Using a time span
- When you use the
span
function in theGROUP BY
clause, the <field> must contain timestamps, such as the_time
field. - There are multiple ways to use a span:
Span type Syntax Description and Examples Auto span span (<field>)
The span is calculated automatically based on the time range in the Time Range Picker.
Example:span(_time)
Span using the default time unit.
There are two syntaxes you can use.span (<field>, <timescale>)
<field> span=<timescale>
You can specify a timescale, such as m
for minute andh
for hour, without specify a time unit. The default time unit,1
is used.
Examples:span(_time, m)
, which defaults to a span of 1 minute._time span=h
, which defaults to a span of 1 hour.
Span with a specified time unit.
There are two syntaxes you can use.span (<field>, <int><timescale>)
<field> span=<int><timescale>
You can specify a timescale with a time unit.
Examples:span (_time, 3h)
, which specifies a span of 3 hours._time span=3h
, which specifies a span of 3 hours.
- For more information about the
span
function, see span(<time>,<span-length> statistical function. - For more information about specifying
span
, see Specifying time spans in the SPL2 Search Manual.
- For more information about the
- Default: If no
GROUP BY
clause is specified, thefrom
command returns all of the rows, based on theWHERE
clause.
SELECT clause
- SELECT
- Syntax: (SELECT | SELECT DISTINCT) <expression> [ AS <field> [,<expression> [AS <field>] ]... ]
- Description: Use the
SELECT
clause to retrieve specific fields and perform an aggregate function that specifies a field, such asmax(delay)
. UseSELECT DISTINCT
to retrieve unique combinations of the selected field values. If multiple rows contain the same combination of field values, only one row is returned.
- The <expression> can be any expression. See Types of expressions in the SPL2 Search Manual.
- In the <expression>, you can specify a single field name or use the asterisk ( * ) wildcard to specify multiple fields, such as
'host*'
. The syntax with the asterisk must be enclosed in single quotation marks. For information about when quotation marks are required, see Quotations in the SPL2 Search Manual. - If you specify an <aggregate-function>, only one row is returned with the aggregation over the entire incoming result set.
- If you rename an expression using
AS <field>
, the name you specify for the field can't be a reserved word. For a list of the reserved words, see Reserved words. - If an <alias> is used to identify a dataset, such as when you use the JOIN clause, you must use that alias when specifying field names in the SELECT clause.
- Supported hierarchies
- The
SELECT
clause is supported in two different syntax hierarchies. One hierarchy starts with theFROM
clause. The other hierarchy starts with theSELECT
clause. See from command usage.
- FROM clause hierarchy: When the
GROUP BY
clause is specified, theSELECT
clause must also be specified. The expressions in theSELECT
clause must be field names or aggregate functions. For examples, see the section Dependencies between SELECT and GROUP BY. - SELECT clause hierarchy: When you start a search with the
SELECT
clause, theFROM
clause is also required.
- FROM clause hierarchy: When the
- Dependencies between SELECT and GROUP BY
- When you include the
GROUP BY
clause in a search, theSELECT
clause must also be included. The fields you specify in theSELECT
clause must also be specified in theGROUP BY
clause, unless the field in theSELECT
clause is used in an aggregate function.
- The following table includes several examples that show the dependency between the
SELECT
and theGROUP BY
clauses:
Description Example The SELECT clause has count()
, an aggregate function, and the fieldshost
and_time
. The GROUP BY clause must include both thehost
and_time
fields.FROM main WHERE sourcetype="web_access" GROUP BY host, _time SELECT count(), host, _time
The SELECT clause has count()
, an aggregate function, thehost
field, andlatest(_time) AS _time
aggregate function expression. The GROUP BY clause must include thehost
field.FROM main WHERE sourcetype="web_access" GROUP BY host SELECT count(), host, latest(_time) AS _time
- Default: None
ORDER BY clause
- ORDER BY
- Syntax: (ORDER BY | ORDERBY) <expression>[,<expression>... ] [ASC | DESC]
- Description: One or more expressions to order the results by. See Types of expressions in the SPL2 Search Manual.
- An expression can be a field name, or an arbitrary expression such as
first_name + last_name
orupper(first_name)
. Separate multiple expressions with commas. - If the expression is a field name, you can't use a wildcard character ( * ) to specify multiple fields with similar names. You must specify each field separately. You can specify either ascending or descending order.
- If the field you want to order the results by is the result of an aggregation, such as
count(host)
orsum(bytes)
, you can either enclose the field name in single quotation marks, because it contains special characters, or rename the field before using theORDER BY
clause.
- An expression can be a field name, or an arbitrary expression such as
- Default: None, the order is based on the source. The
ORDER BY
clause uses lexicographical order. See Lexicographical order in the SPL2 Search Manual.
HAVING clause
- HAVING
- Syntax: HAVING <expression>
- Description: Use the
HAVING
clause as a filter on the data. The expression you specify must result in either true or false. If the expression is a string, it must be enclosed in double quotation marks. See Predicate expressions in the SPL2 Search Manual.
- Traditionally you use the
HAVING
clause to filter data after clauses with aggregations. For example, this search contains an aggregation in theSELECT
clause. TheWHERE
clause is used to filter the data before the aggregation in the SELECT clause.
FROM main WHERE earliest=-5m@m AND latest=@m GROUP BY host SELECT sum(bytes) AS sum, host HAVING sum > 1024*1024
- Default: None
LIMIT clause
- LIMIT
- Syntax: LIMIT <integer>
- Description: Use to limit the number of rows returned. For example to return only the first 100 results.
- Default: None
OFFSET clause
- OFFSET
- Syntax: OFFSET <integer>
- Description: Use to skip past a number of matches. For example if you specify
OFFSET 15
, the 16th result is the first result that is returned. The OFFSET clause is often used in conjunction with the LIMIT clause. - Default: None
See also
- from command
- from command overview
- from command usage
- from command examples
PREVIOUS from command overview |
NEXT from command usage |
This documentation applies to the following versions of Splunk® Cloud Services: current
Feedback submitted, thanks!