SELECT Clause
The SELECT clause retrieves columns from a table.
SELECT query syntax takes the form of a required SELECT statement followed by one or more optional clauses, such as WHERE, GROUP BY, and ORDER BY.
For a simple column projection, SQL outputs the results with the name of the column provided. If you include additional expressions and functions, you must use an alias to name the column output. Include an alias by following the structure {expression} {AliasName} or {expression as AliasName}. The “as” is not required.
Unlike other versions of SQL, the CRM Analytics SQL server throws an error if you don’t include an alias. It does not automatically provide an alias name.
Let’s look at an example.
Here are the first ten results.
| City | StoreCount |
|---|---|
| Aberdeen | 1 |
| Abilene | 1 |
| Akron | 21 |
| Albuquerque | 14 |
| Alexandria | 16 |
| Allen | 4 |
| Allentown | 7 |
| Altoona | 2 |
| Amarillo | 10 |
| Anaheim | 27 |
This query returns a table with two columns, City and the alias StoreCount. StoreCount is the column name given to the expression, COUNT(*), which means count the number of rows for each city in the Superstore table. The GROUP BY clause groups all of the store counts by the City column.
SELECT supports the following operators.
| Type | Operator Name | Supported Types |
|---|---|---|
| Arithmetic Operators | + | Numeric, String |
| - | Numeric | |
| * | Numeric | |
| / | Numeric | |
| % | Numeric | |
| Comparison Operators | !=, = | Numeric, String, Boolean |
| <, <=, >, >= | Numeric, String | |
IN, NOT IN | Numeric, String | |
| Logical Operators | AND, OR, NOT | Boolean |
| Case Operators | CASE ... END | Numeric, String, Boolean |
SELECT supports arithmetic operators between measure fields and using ”+” between string fields.
-
The
FROMclause defines which table or table function to query. CRM Analytics SQL supports usingFROMwith a single table only. You can’t select from multiple tables. -
By default, a SQL query retrieves every row in your dataset. Use the optional
WHEREclause to restrict your query results to a conditional expression. -
Boolean Expressions in SELECT Clause
A boolean expression is a logical statement that returns either
trueorfalse. -
The
GROUP BYclause organizes the rows returned from aSELECTstatement into groups. Within each group, you can apply an aggregate function, likecount()orsum()to get the number of items or sum, respectively. -
ROLLUPis a sub-clause ofGROUP BYthat creates and displays aggregations of column data. The results output ofROLLUPis based on column order in your query. -
Use the
GROUPING()clause withROLLUPto determine whether null values are the result of aROLLUPoperation or part of your dataset.GROUPING()returns 1 if the null value is a subtotal generated by a rollup. It returns 0 otherwise. -
Use the
HAVINGclause to filter grouped results from grouped columns, aggregate functions, or grouping functions. -
SELECTreturns rows in an unspecified order by default. To sort returned rows in ascending or descending order, use theORDER BYclause. -
Use
LIKEto match single characters and patterns found anywhere in a string—beginning, ending, or somewhere in between. -
Use
BETWEENto check whether values fall within a given range.BETWEENaccepts numeric, string, and date data types, and can be used with aggregate, window, and math functions. -
The
LIMITclause specifies the maximum number of rows to return. If noLIMITclause is specified, then SQL returns all rows. -
The
FETCHclause specifies the number of rows to return. If noFETCHclause is specified, then SQL returns all rows. -
By default, a SQL query retrieves every row in your dataset. Use the optional
WHEREclause to restrict your query results to a conditional expression. -
Use case statements to express if/then logic. A case statement always has a pair of
WHEREandTHENstatements. CRM Analytics supports the simple and searched forms of case expressions in aSELECTstatement. Case statements have two formats: simple and searched. -
You can use the
coalesce()function as shorthand for case statements. Thecoalesce()function replaces null values in your dataset with another value. The function takes a series of arguments and returns the first value that is not null. -
Use the
nullif()function as shorthand for a searched case statement where two equal expressions return null. -
Use the
UNIONoperator to combine the results of two or moreSELECTstatements. The joined statements must have the same number of columns and the same data types for corresponding columns. -
A subquery is a query that is nested inside a
SELECTstatement. Nesting queries allows you to perform multi-step operations. -
A window function lets you perform calculations on a selection—or “window”—of rows that are related to the current row. Unlike a regular aggregate function, such as
avg(),sum(), orcount(), the row output of a window function isn’t grouped into a single row.