---
title: COMPARE TO
description: Compare ShopifyQL results to another period or target.
api_version: 2026-07
source_url:
  html: 'https://shopify.dev/docs/api/shopifyql/latest/syntax/compare-to'
  md: 'https://shopify.dev/docs/api/shopifyql/latest/syntax/compare-to.md'
api_name: shopifyql
---

# COMPARE TO

Use `COMPARE TO` to compare the query's reporting period against another baseline. A comparison can use another [comparison range](#comparison-ranges), a [relative comparison period](#relative-comparison-periods), or configured targets. ShopifyQL adds a [generated comparison column](#generated-comparison-columns) for each compared metric. Set the reporting period with [`SINCE`, `UNTIL`, or `DURING`](https://shopify.dev/docs/api/shopifyql/latest/syntax/since-until-during), and reference the comparison columns from [`SHOW`](https://shopify.dev/docs/api/shopifyql/latest/syntax/from-and-show#show), [`ORDER BY`](https://shopify.dev/docs/api/shopifyql/latest/syntax/order-by), or [`HAVING`](https://shopify.dev/docs/api/shopifyql/latest/syntax/having).

## Syntax

```shopifyql
COMPARE TO comparison[, ...]


comparison:
  date [UNTIL date]
  TARGETS
```

***

## Comparison ranges

**Note:**

`TARGETS` is available only when the query's schema supports it.

Set what ShopifyQL compares the reporting period against. A date comparison reuses the same [relative, absolute, named, and date-function forms](https://shopify.dev/docs/api/shopifyql/latest/syntax/since-until-during) as the reporting period, with an optional `UNTIL` to set the comparison's end. Other comparisons measure results against configured metric goals.

* Compare to a relative period with a named operator, such as `COMPARE TO previous_year`.
* Compare to a period starting on an absolute date, such as `COMPARE TO 2023-01-01`.
* Compare to a named date range, such as `COMPARE TO last_week`.
* Compare to a period resolved by a date function, such as `COMPARE TO startOfQuarter(-3q)`.
* Set an explicit comparison range end with `UNTIL`, such as `COMPARE TO startOfQuarter(-3q) UNTIL endOfQuarter(-3q)`.
* Compare against configured metric targets with `COMPARE TO TARGETS`.

Examples

### Examples

* ####

  ##### Description

  Compare daily \[\`sessions\`]\(/docs/api/shopifyql/latest/schemas/sessions\_and\_behavior/sessions#sessionsmetric-propertydetail-sessions) across January 2026 against the same days in January 2025. This example uses an absolute \`COMPARE TO\` range to anchor the comparison column to fixed prior dates.

  ##### ShopifyQL

  ```shopifyql
  FROM sessions
    SHOW sessions
    TIMESERIES day
    SINCE 2026-01-01 UNTIL 2026-01-31
    COMPARE TO 2025-01-01 UNTIL 2025-01-31
    ORDER BY day ASC
  ```

* ####

  ##### Description

  Compare daily \[\`total\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-totalsales) for the previous quarter against the quarter two before it. This example uses date functions with \`COMPARE TO ... UNTIL\` (\`startOfQuarter\` and \`endOfQuarter\`) to resolve both the reporting period and a fixed comparison range.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW total_sales
    TIMESERIES day
    SINCE startOfQuarter(-1q) UNTIL endOfQuarter(-1q)
    COMPARE TO startOfQuarter(-3q) UNTIL endOfQuarter(-3q)
    ORDER BY day ASC
  ```

***

## Relative comparison periods

Compare the reporting period to an earlier period of the same length, measured back from the selected period. Use a named relative operator instead of writing out the comparison date range.

* **previous\_​period**

  **previous\_period**

  One period before the base date range.

* **previous\_​year**

  **previous\_year**

  One year before the base date range.

* **previous\_​year\_​match\_​day\_​of\_​week**

  **previous\_year\_match\_day\_of\_week**

  52 weeks before the base date range.

* **previous\_​quarter**

  **previous\_quarter**

  One quarter before the base date range.

* **previous\_​month**

  **previous\_month**

  One month before the base date range.

* **previous\_​week**

  **previous\_week**

  One week before the base date range.

* **previous\_​day**

  **previous\_day**

  One day before the base date range.

* **previous\_​hour**

  **previous\_hour**

  One hour before the base date range.

* **previous\_​minute**

  **previous\_minute**

  One minute before the base date range.

* **previous\_​second**

  **previous\_second**

  One second before the base date range.

Examples

### Examples

* ####

  ##### Description

  Compare daily \[\`sessions\`]\(/docs/api/shopifyql/latest/schemas/sessions\_and\_behavior/sessions#sessionsmetric-propertydetail-sessions) for last week against the week immediately before. This example uses \`COMPARE TO previous\_period\` to derive the comparison range automatically from the reporting period's length.

  ##### ShopifyQL

  ```shopifyql
  FROM sessions
    SHOW sessions
    TIMESERIES day
    DURING last_week
    COMPARE TO previous_period
    ORDER BY day ASC
  ```

* ####

  ##### Description

  Compare daily \[\`total\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-totalsales) by sales channel for last month against the month before. This example uses \`COMPARE TO previous\_month\` to benchmark each channel against the prior month.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW total_sales
    GROUP BY sales_channel
    TIMESERIES day
    DURING last_month
    COMPARE TO previous_month
    ORDER BY day ASC, total_sales DESC
  ```

***

## Generated comparison columns

When a query uses `COMPARE TO`, ShopifyQL adds a result column for each compared metric. The generated name combines the metric and the comparison period, so the same metric can carry more than one comparison column. Reference a generated comparison column by name in [`ORDER BY`](https://shopify.dev/docs/api/shopifyql/latest/syntax/order-by), [`HAVING`](https://shopify.dev/docs/api/shopifyql/latest/syntax/having), or [`VISUALIZE`](https://shopify.dev/docs/api/shopifyql/latest/syntax/visualize#visualize). The comparison period determines the suffix:

* A relative comparison generates `comparison_net_sales__previous_year`.
* An absolute-date comparison generates `comparison_net_sales__20230101`.
* A named-date comparison generates `comparison_net_sales__last_week`.
* A date-function comparison generates `comparison_net_sales__startOfQuarter_sub_3q`, where minus signs become `sub`.

Examples

### Examples

* ####

  ##### Description

  Rank product types by \[\`gross\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-grosssales) for the current year. This example uses the generated \`comparison\_gross\_sales\_\_previous\_year\` column in \[\`ORDER BY\`]\(/docs/api/shopifyql/latest/syntax/order-by) to sort by the previous year.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW gross_sales
    GROUP BY product_type
    DURING this_year
    COMPARE TO previous_year
    ORDER BY comparison_gross_sales__previous_year DESC
    LIMIT 10
  ```

* ####

  ##### Description

  Sort billing countries by \[\`orders\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-orders) for January 2024. This example uses an absolute \`COMPARE TO\` range, which generates a date-stamped column like \`comparison\_orders\_\_20230101\` to sort by.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW orders
    GROUP BY billing_country
    SINCE 2024-01-01 UNTIL 2024-01-31
    COMPARE TO 2023-01-01 UNTIL 2023-01-31
    ORDER BY comparison_orders__20230101 DESC
    LIMIT 10
  ```

***
