Skip to main content

HAVING

Use HAVING to filter query results after ShopifyQL calculates metrics. To filter rows before aggregation, use WHERE. A HAVING condition can use returned metrics, aliases, supported function calls, and dimensions that are part of the query result. It can also filter generated result columns when an attribution modifier generates them.

Note

Non-attribution generated result columns aren't valid in HAVING, including total columns, group-total columns, comparison columns, percent-change columns, and cumulative columns. Attribution result columns are valid only when an attribution modifier generates them.

Syntax

HAVING condition

condition:
boolean_expression
value comparison_operator value
value [NOT] IN (value, ...)
value [NOT] BETWEEN low AND high
value STARTS WITH|ENDS WITH|[NOT] CONTAINS value
value IS [NOT] NULL|TRUE|FALSE
NOT condition
condition AND|OR condition
(condition)

value:
returned_metric
result_dimension
alias
literal
supported_function_call

A HAVING condition must evaluate to a Boolean value. It can compare compatible returned metrics, aliases, supported function calls, or result dimensions to values. A condition can include literals, parentheses, and the supported operators described in the following sections.

A metric in HAVING must be returned by SHOW, or selected by VISUALIZE when the query doesn't include SHOW. A dimension in HAVING must be part of the query result, such as a dimension returned by SHOW, a dimension grouped by GROUP BY, or the time dimension selected by TIMESERIES. If SHOW renames a returned field with AS, then use the alias in HAVING. For a returned expression that you want to filter, use AS in SHOW, and reference that alias in HAVING.

Examples

ShopifyQL

FROM sales
SHOW net_sales
GROUP BY product_title
HAVING net_sales > 1000
DURING last_month
ORDER BY net_sales DESC
LIMIT 10

Anchor to Comparison operatorsComparison operators

Compare compatible result values, aliases, supported function calls, or literals in a HAVING condition. Ordered comparisons require ordered values such as numbers, money, dates, or text.

OptionSyntaxDescription
equals<column> = <value>Matches values equal to the right-hand side. Works with compatible scalar values.
not equals<column> != <value>Matches values not equal to the right-hand side. Works with compatible scalar values.
less than<column> < <value>Matches values less than the right-hand side. Use with ordered values such as numbers, money, dates, or text.
less than or equal<column> <= <value>Matches values less than or equal to the right-hand side. Use with ordered values such as numbers, money, dates, or text.
greater than<column> > <value>Matches values greater than the right-hand side. Use with ordered values such as numbers, money, dates, or text.
greater than or equal<column> >= <value>Matches values greater than or equal to the right-hand side. Use with ordered values such as numbers, money, dates, or text.
Examples

ShopifyQL

FROM sales
SHOW net_sales
GROUP BY sales_channel
HAVING net_sales > 100
SINCE -30d UNTIL today
ORDER BY net_sales DESC

Anchor to Range and membership operatorsRange and membership operators

Match a result value against a compatible list, or test whether an ordered value is inside an inclusive range. In HAVING, every referenced field must be available in the query result.

OptionSyntaxDescription
IN<column> IN (<value>, ...)Matches any compatible value in a list.
NOT IN<column> NOT IN (<value>, ...)Excludes any compatible value in a list.
BETWEEN<column> BETWEEN <low> AND <high>Matches values within an inclusive range. Use with ordered values such as numbers, money, dates, or text.
NOT BETWEEN<column> NOT BETWEEN <low> AND <high>Matches values outside an inclusive range. Use with ordered values such as numbers, money, dates, or text.
Examples

ShopifyQL

FROM sessions
SHOW sessions
GROUP BY session_device_type
HAVING session_device_type IN ('desktop', 'mobile')
DURING last_month
ORDER BY sessions DESC

Anchor to Null and Boolean checksNull and Boolean checks

Test nullable values or Boolean values in a HAVING condition. Null checks work with any nullable value. Boolean checks require Boolean values.

OptionSyntaxDescription
IS NULLIS NULLValue is null.
IS NOT NULLIS NOT NULLValue is not null.
IS TRUEIS TRUEValue is true.
IS NOT TRUEIS NOT TRUEValue is not true, including false or null.
IS FALSEIS FALSEValue is false.
IS NOT FALSEIS NOT FALSEValue is not false, including true or null.
Examples

ShopifyQL

FROM sales
SHOW net_sales
GROUP BY product_type
HAVING product_type IS NULL
DURING last_month
ORDER BY net_sales DESC

Combine or negate HAVING conditions. AND is evaluated before OR, so group conditions with parentheses to control the order, for example HAVING a AND (b OR c).

OptionSyntaxDescription
AND<condition> AND <condition>Requires both conditions to match.
OR<condition> OR <condition>Requires either condition to match.
NOTNOT <condition>Negates a condition.
Examples

ShopifyQL

FROM sales
SHOW net_sales, orders
GROUP BY sales_channel
HAVING net_sales > 1000 AND orders > 10
DURING last_month
ORDER BY net_sales DESC

Anchor to String and array matchingString and array matching

Match compatible string values by prefix, suffix, or substring, or check array values for a compatible element. Use matching operators as part of a Boolean HAVING condition.

OptionSyntaxDescription
STARTS WITH<column> STARTS WITH <value>Matches string-like values with the specified prefix.
ENDS WITH<column> ENDS WITH <value>Matches string-like values with the specified suffix.
CONTAINS<column> CONTAINS <value>Matches string-like values containing the specified substring, or arrays containing the specified element.
NOT CONTAINS<column> NOT CONTAINS <value>Matches string-like values or arrays that do not contain the specified value.
Examples

ShopifyQL

FROM sessions
SHOW sessions
GROUP BY landing_page_path
HAVING landing_page_path CONTAINS '/products'
DURING last_month
ORDER BY sessions DESC
LIMIT 10

Anchor to Generated result columnsGenerated result columns

Use attribution result columns in HAVING only when an attribution modifier generates them. When a query applies an attribution modifier to a metric, filter the suffixed attribution result column instead of the base metric or its alias.

For attribution column names and modifier details, refer to the WITH attribution result columns section.

Examples

ShopifyQL

FROM sales
SHOW total_sales
GROUP BY referring_channel WITH FIRST_CLICK_ATTRIBUTION
HAVING total_sales__first_click > 1000
SINCE -30d UNTIL today
ORDER BY total_sales__first_click DESC

Was this page helpful?