> [!NOTE]
> **Note:** If you're using Looker's [new Explore experience](https://docs.cloud.google.com/looker/docs/2614/viewing-and-interacting-with-nextgen-explores), see the [Joining data from multiple queries in the new Explore experience](https://docs.cloud.google.com/looker/docs/2614/merge-queries-new-explore.md.txt) documentation page.

> [!NOTE]
> **Note:** This page is part of the [Retrieve and chart data](https://docs.cloud.google.com/looker/docs/2614/retrieve-and-chart-data.md.txt) learning series.

Explores in Looker are designed by your Looker developers to combine the data from your database tables in the most effective way by using defined relationships between data fields and tables. Because of this, it's best to use a single Explore to examine your data.

However, there may be times when your Looker developers haven't created the relationships you need or have encountered technical limitations. In these cases, **Merged Results** lets you combine results from different Explores (even from different models or projects) to create data tables on which to [Explore](https://docs.cloud.google.com/looker/docs/2614/creating-and-editing-explores.md.txt) and [create visualizations](https://docs.cloud.google.com/looker/docs/2614/creating-visualizations.md.txt). You can use **Merged Results** as a proof of concept to further develop and define your LookML projects and models.

## Understanding merged results

When you merge queries, you start out by creating a single query from a single Explore, and then you combine other queries with that first query.

By default, that first query is considered the *primary* query. This is an important concept because when Looker matches the data to create the merged results, it matches each added query to the primary query (not to any other added query). So, whenever you add a query, you need to include a dimension that can be matched to a dimension in the primary query.

For example, consider the following queries.

The primary query returns the following results:

| Products Category | Products Count |
|---|---|
| Active | 5 |
| Jeans | 9 |
| Formalwear | 3 |

<br />

The secondary query returns the following results:

| Products Category | Items in Inventory Count |
|---|---|
| Active | 11 |
| Jeans | 16 |
| Formalwear | 6 |

<br />

If you merge these queries on the **Products Category** field, Looker produces the following merged results:

| Products Category | Products Count | Items in Inventory Count |
|---|---|---|
| Active | 5 | 11 |
| Jeans | 9 | 16 |
| Formalwear | 3 | 6 |

Merged results don't perform an actual SQL join. But, for those who are familiar with SQL joins, the **Merged Results** feature combines the results of multiple Explores in a similar way as would a *left join*. The results of the added query are combined with the results of the primary query as if they are being left joined into the primary query.

You don't have to be familiar with the idea of a left join to understand how merged results work. In practical terms, here's why it matters which query is the primary query:

- **How field names appear:** For matching fields, the primary query's field names are used in the merged results, as shown in the previous example.
- **How merged results handle a query without a matching value:** The next section, [What if one query doesn't have a matching data value?](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#what_if_one_query_doesnt_have_a_matching_data_value), discusses how Looker handles merging data when only some of the queries have specific value(s) in the matching dimension(s).

  You can also reference the [Why are there nulls in my secondary merge results query?](https://docs.cloud.google.com/looker/docs/2614/best-practices/how-to-troubleshoot-nulls-in-secondary-merge-result-query) Best Practices page for more troubleshooting tips for missing or null merged results values.
- **How merged results handle a query having multiple matching values:** The section [What if one query has multiple rows for the same value?](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#what_if_one_query_has_multiple_rows_for_the_same_value?) later on this page discusses how Looker handles merging data when some of the queries have multiple rows with a specific value (or combination of values) in the matching dimension(s).

### What if one query doesn't have a matching data value?

Another reason the primary query is important is because of the way null values are handled in the matched dimensions:

- If a row exists in the primary query but not in the additional query, then the added query's fields will be NULL for that row.

- If a row exists in the added query but not in the primary query, then the row will not show in the results at all.

To illustrate these examples, consider the following example queries.

The primary query returns the following results:

| Products Category | Products Department | Products Count |
|---|---|---|
| Active | Kids | 522 |
| Active | Adults | 545 |
| Dresses | Adults | 878 |
| Formalwear | Adults | 349 |

<br />

The secondary query returns the following results:

| Products Category | Products Brand Name | Items in Inventory Count |
|---|---|---|
| Active | Brand 1 | 223 |
| Dresses | Brand 2 | 80 |
| Dresses | Brand 3 | 3 |
| Jeans | Brand 3 | 8 |
| Jeans | Brand 2 | 19 |

<br />

If you merge these queries on the **Products Category** field, Looker produces the following merged results:

| Products Category | Products Department | Products Brand Name | Products Count | Items in Inventory Count |
|---|---|---|---|---|
| Active | Adults | Brand 1 | 545 | 223 |
| Active | Kids | Brand 1 | 522 | 223 |
| Dresses | Adults | Brand 2 | 878 | 80 |
| Dresses | Adults | Brand 3 | 878 | 3 |
| Formalwear | Adults | ∅ | 349 | ∅ |

The primary query has a row for **Formalwear** , so the merged results will show this row. The added query does not have a **Formalwear** row, so any of the fields from the added query will show NULL (∅) for **Formalwear**.

The added query has two rows for **Jeans**, but the primary query does not. So this row is not shown in the merged results at all.

In this example, if you [switch the primary query](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#make_primary) to make the added query the new primary query, Looker instead produces the following merged results:

| Products Category | Products Brand Name | Products Department | Items in Inventory Count | Products Count |
|---|---|---|---|---|
| Active | Brand 1 | Adults | 223 | 545 |
| Active | Brand 1 | Kids | 223 | 522 |
| Dresses | Brand 2 | Adults | 80 | 878 |
| Dresses | Brand 3 | Adults | 3 | 878 |
| Jeans | Brand 2 | ∅ | 19 | ∅ |
| Jeans | Brand 3 | ∅ | 8 | ∅ |

Looker no longer displays the **Formalwear** rows because they do not exist in our new primary query. However, Looker now displays the **Jeans** rows, and those rows show NULL for the dimensions and measures that are only in the query that is *added* to the primary query.

You can also reference the [Why are there nulls in my secondary merge results query?](https://docs.cloud.google.com/looker/docs/2614/best-practices/how-to-troubleshoot-nulls-in-secondary-merge-result-query) Best Practices page for more troubleshooting tips for missing or null merged results values.

### What if one query has multiple rows for the same value?

Finally, designating the primary query is also important because of the way multiple rows with matching values are handled. If the added query has two or more rows with values that match a row in the primary query, the primary query row will be duplicated that number of times.

In the following example, the *added* query has two rows for **Dresses** . In the merged results, the **Dresses** values from the primary query appear twice, once for each of the **Dresses** rows from the added query.

The primary query returns the following results:

| Products Category | Products Department | Products Count |
|---|---|---|
| Active | Kids | 522 |
| Active | Adults | 545 |
| Dresses | Adults | 878 |
| Formalwear | Adults | 349 |

<br />

The secondary query returns the following results:

| Products Category | Products Brand Name | Items in Inventory Count |
|---|---|---|
| Active | Brand 1 | 223 |
| Dresses | Brand 2 | 80 |
| Dresses | Brand 3 | 3 |
| Jeans | Brand 3 | 8 |
| Jeans | Brand 2 | 19 |

<br />

The merged results query, merging on the **Products Category** field, returns the following results:

| Products Category | Products Department | Products Brand Name | Products Count | Items in Inventory Count |
|---|---|---|---|---|
| Active | Adults | Brand 1 | 545 | 223 |
| Active | Kids | Brand 1 | 522 | 223 |
| Dresses | Adults | Brand 2 | 878 | 80 |
| Dresses | Adults | Brand 3 | 878 | 3 |
| Formalwear | Adults | ∅ | 349 | ∅ |

Note that if you [switch the primary query](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#make_primary) in this case, you would still have two **Dresses** rows, since the newly designated primary query has two rows for **Dresses** . The takeaway is that, when you merge queries, the results may have more rows than the primary query has --- but there will never be *fewer* rows.

## In-database merge queries

> [!WARNING]
> **Preview:** This feature is subject to the "Pre-GA Offerings Terms" in the [General Service Terms](https://cloud.google.com/terms/service-terms#1) section of the Service Specific Terms. Pre-GA products and features are available "as is" and might have limited support. For more information, see the [launch stage descriptions](https://cloud.google.com/products#product-launch-stages).
>
> If you encounter an issue during the Public Preview of in-database merge queries, report it to [looker-in-database-merged-results-external@google.com](mailto:looker-in-database-merged-results-external@google.com.com).

By default, merged queries are processed in Looker memory, and therefore each query is limited to 5,000 rows that can be joined. If your Looker admin has enabled the [**In-database merge queries** Looker Labs feature](https://docs.cloud.google.com/looker/docs/2614/admin-panel-general-labs#in-database-merge-queries), the join between two queries that are on the same BigQuery connection is performed in the BigQuery database itself. Performing the join in the database is more performant and allows for unlimited rows that can be joined.

> [!NOTE]
> **Note:** In-database merge queries are performed as [left joins](https://docs.cloud.google.com/looker/docs/2614/reference/param-explore-join-type.md.txt#left-outer).

In addition, when you create each of the Explore queries for a merged result, the Looker UI contains a **Row Limit** field. Looker uses the **Row Limit** field in different ways, depending on how the merged result query is processed:

- For a merged query that is calculated in Looker memory, Looker retrieves the number of rows specified in the **Row Limit** field for each query and joins that data to create the merged result.
- For an in-database merged query, Looker disregards the **Row Limit** values for each of the source queries and joins the full datasets for each source query.

If Looker is able to perform an in-database join for your merged query, the **Merged Results** dialog will display a banner that indicates that the queries are being run in your database and that Looker is omitting the row limits from each of the source queries.

Then, in the results table for an in-database merged query, Looker displays a **Merged Results** field instead of a **Row Limit** field. The **Merged Results** field limits the rows that are being displayed in the results table, not the rows that are being processed, because in-database merged results queries don't have limits for how many rows can be processed.

## Merging queries

To merge the results from multiple queries, follow these steps:

1. [Create the first source query](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#creating_the_primary_query), called the *primary query*.
2. [Add the next source query](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#adding_the_next_source_query).
3. [Check the merge rules for those queries and run the merge](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#checking_the_merge_rules_and_running_the_merge).
4. [Optionally, you can](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#using_and_modifying_the_merged_results):

   - Sort, pivot, and create visualizations for the results.
   - Reuse and share the results using the URL.
   - Modify the results by editing the source queries or adding source queries.

### Creating the primary query

To merge the results from multiple queries, start by preparing the *primary* query by following these steps:

1. Select an Explore from the **Explore** menu.
2. Select the dimensions and measures of interest from the field picker. Don't pivot any dimensions during this step.

   This is all you need to start merging results. However, you can also use some advanced exploring techniques to further refine your query. You can:
3. Optionally, add [filters](https://docs.cloud.google.com/looker/docs/2614/filtering-and-limiting.md.txt) for the data.

4. Optionally, include [table calculations](https://docs.cloud.google.com/looker/docs/2614/table-calculations.md.txt) to create ad hoc metrics.

5. Optionally, click **Run** to see the results of your primary query and to test your filters and table calculations.

## Adding the next source query

Once you've created your primary Explore, add another source query by following these steps:

1. In your Explore, click the gear icon.
2. Select **Merge Results** . This will open the **Choose an Explore** window.
3. In the **Choose an Explore** window, click the name of an Explore where you'll create your next query.

   Looker opens the Explore in the **Edit Query** window, where you can build the new query to be merged into your primary query.

   > [!NOTE]
   > **Note:** To merge queries, Looker finds dimensions in the queries whose values can be matched. Be sure that your queries contain at least one common dimension whose values can be matched exactly. For example, if both queries have a **Date** dimension, but one query uses "2017-10-01" as a value and the other query uses "October 2017" as a value, Looker can't use that dimension to merge the queries.

4. Select the dimensions and measures of interest from the field picker. Be sure to include at least one dimension that will exactly match a dimension in the primary query. Do not pivot any dimensions during this step.

5. Optionally, include filters to narrow the data.

6. Optionally, incorporate table calculations to create new fields based on the query fields.

7. Optionally, click **Run** to see the results of the source query and to test your filters and table calculations.

8. Click **Save** to merge the query into your primary query.

### Checking the merge rules and running the merge

Looker automatically finds the best dimensions to use for matching the queries and displays these matches in the **Merge Rules** section. Looker displays which fields will be used for each merge.

1. Review the dimensions that Looker used to match the queries. (See [Editing merge rules](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#editing_merge_rules) for information on changing these rules.)

2. Click **Run** to see the merged query results.

Note that any table calculations from the source queries are displayed as standard dimensions in the merged results.

### Using and modifying the merged results

You can use the merged results to:

- Examine and sort the data, including [drilling into the data](https://docs.cloud.google.com/looker/docs/2614/viewing-and-interacting-with-explores.md.txt#drilling_down_into_the_data). If you drill into the dimension used to merge the two Explores, however, Looker displays only drill fields from the primary Explore.
- View [totals](https://docs.cloud.google.com/looker/docs/2614/creating-and-editing-explores.md.txt#displaying_totals). Looker calculates totals on each of the component queries and uses those totals in the merged results. Therefore, totals may appear too high, because what you are seeing are totals calculated before the [results were merged](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#what_if_one_query_doesnt_have_a_matching_data_value). One way to avoid this is to align the filters on each query.
- Create visualizations.
- [Pivot dimensions](https://docs.cloud.google.com/looker/docs/2614/creating-and-editing-explores.md.txt#pivoting_dimensions) in the merged results by selecting **Pivot** from the gear menu in the dimension's column of the data table. Note that you can't pivot dimensions in the source queries.

To reuse the merged results, you can:

- Share the results using the browser URL.
- Bookmark the URL in your browser to run the same merged query again in the future. You can't save the merged results as a Look.
- Save the merged results as a tile on a dashboard (see [Saving your merged results to a dashboard](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#save_merged_results)).

If you want to modify the merged results, you can:

- Merge queries from additional Explores by clicking the **Add Query** button and following the same steps.
- Edit the source queries or configure the way the queries are merged. See [Editing merged results](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#editing_merged_results) for more information.
- [Create filters](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#editing_merged_results) by adding those filters in the source queries, either when creating and merging the queries, or by editing the queries from the **Merged Results** page. Note that you can't add a filter directly to the merged results.

If you want to clear the cache and retrieve fresh results from your database, select the **Clear Cache \& Refresh** option from the gear menu at the top right of the **Merged Results** window.

To download your merged results query, you can [save the query to a dashboard](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#saving_your_merged_results_to_a_dashboard) and then [download the dashboard as a PDF or as a collection of CSV files](https://docs.cloud.google.com/looker/docs/2614/downloading.md.txt#downloading_data_from_a_dashboard).

## Editing merged results

Once you have your merged results, you can make the following changes to your merged results:

- [Editing the source queries](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#editing_the_source_queries)
- [Editing the merge rules](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#editing_the_merge_rules)
- [Switching the primary query](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#switching_the_primary_query)

### Editing the source queries

From the **Merged Results** window, you can go back and edit the source queries by clicking on the query name in the left pane, or by selecting **Edit** from the query's gear menu. These options take you back to the **Edit Query** screen.

You can add or remove fields, add table calculations, or change the source query's filters. Click **Save** to return to your merged results.

The query's gear menu also has these options:

- **Rename** : Specify a different name to display for the query in the **Merged Results** window.
- **Make Primary** : Make the query the base for the merged results. See [Understanding merged results](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#understanding_merged_results) to understand the role of the primary query.
- **Delete** : Remove the query from the merged results. (If you want to add the query back into the merged results after you've deleted it, you can use the **Back** button in your browser.)

### Editing the merge rules

When you add a query, Looker automatically finds dimensions that it can use to match the added query to the primary query. Each added query must have at least one dimension whose values exactly match up to a dimension in the primary query. Looker displays these matches in the **Merge Rules** section, showing which fields will be used to merge the queries.

You can use the Merge Rules section to change or add which fields Looker uses to merge the queries.

1. Use the drop-down menu to see other dimension options for matching the data.
2. If there are additional dimensions that could be used for matching, Looker displays **+ Add dimension** . Click **+ Add dimension** to configure an additional set of dimensions to use in the query merge.
3. Click the **X** if you don't want to match the data between the two dimensions.


### Switching the primary query

When merging queries, you start out by creating a single query from a single Explore and then you add other queries by combining them with that first query. By default, that first query is considered the *primary* query, but you can designate any query as the primary query by selecting **Make Primary** from the query's gear menu.

Each added query must have at least one dimension whose values can be matched exactly to a dimension in the primary query.

When you switch the primary query, the merged results are likely to change. See [Understanding merged results](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#understanding_merged_results) to understand the role of the primary query.


## Saving your merged results to a dashboard

Once you've added your merged results query to a dashboard, you can [add or apply dashboard filters](https://docs.cloud.google.com/looker/docs/2614/filters-user-defined-dashboards.md.txt) to your merged results tile, [rearrange the tile](https://docs.cloud.google.com/looker/docs/2614/editing-user-defined-dashboards.md.txt#rearranging_and_resizing_tiles), [edit the tile](https://docs.cloud.google.com/looker/docs/2614/editing-user-defined-dashboards.md.txt#editing_tiles), or [add new tiles](https://docs.cloud.google.com/looker/docs/2614/creating-user-defined-dashboards.md.txt#adding_tiles_and_text_to_a_dashboard) to your dashboard. You can't download the data from a tile based on merged results, but you can [download the dashboard as a PDF](https://docs.cloud.google.com/looker/docs/2614/downloading.md.txt#downloading_a_dashboard_as_a_pdf) or [as a collection of CSV files](https://docs.cloud.google.com/looker/docs/2614/downloading.md.txt#downloading_a_dashboard_as_csvs).

> [!IMPORTANT]
> **Important:** When you apply a dashboard filter to a merged query, the filtered dimension must be included in the merged query (the dimension can be [hidden from the visualization](https://docs.cloud.google.com/looker/docs/2614/creating-visualizations.md.txt#hiding_fields_from_visualizations)). Any dashboard filters that are applied to a merged results tile will be turned off if the merged query is changed in any way. You can reinstate the filters by turning them back on again in the [filter configuration window](https://docs.cloud.google.com/looker/docs/2614/filters-user-defined-dashboards.md.txt#editing_dashboard_filters).

## Merging queries in embedded Looks, dashboards, and Explores

You can merge queries within [embedded Looks, dashboards, and Explores](https://docs.cloud.google.com/looker/docs/2614/private-embedding.md.txt), if you have the appropriate permissions. To merge the query of an [embedded Look](https://docs.cloud.google.com/looker/docs/2614/private-embedding.md.txt#embedding_a_look) with another query:

1. Hold your pointer over the Look's title to reveal the Look's gear menu, and then click the gear menu.
2. Select **Merge Results** , then follow the [steps for merging queries](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#merging_queries).

To merge an [embedded dashboard](https://docs.cloud.google.com/looker/docs/2614/private-embedding.md.txt#embedding_a_dashboard) tile's query:

1. Hold your pointer over the dashboard tile to review the drop-down menu for the dashboard tile.
2. From the drop-down menu, choose **Explore From Here**. Looker opens the Explore for that tile's query.
3. Click the Explore's gear menu and select **Merge Results**.
4. Follow the [steps for merging queries](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#merging_queries).

## Notes

- All the primary query's fields are displayed in the merged results, using the primary query's names for the fields. This means that if the primary query and an added query use different names for a matching dimension, only the primary query's dimension name will be displayed in the results.
- Your **Merged Results** query may have row limits:

  - By default, the join for a **Merged Results** query is processed in Looker memory, and therefore each of the queries being merged is limited to 5,000 rows that can be joined (or up to 50,000 rows [if a higher limit is specified by your admin](https://docs.cloud.google.com/looker/docs/2614/admin-panel-performance-center-content-guardrails.md.txt#visualization-limits)). If you are joining a query that returns more than the limited rows of data, only the first number of limited rows that are returned are included in the merged results.
  - If your Looker admin has enabled the **In-database merge queries** Looker Labs feature, and you are merging the results from two queries that are on the same BigQuery connection, the join between two queries is performed in the BigQuery database itself. In-database merge queries are more performant and can join unlimited rows. See the [In-database merge queries](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#in-database-merge-queries) section for more information.
- For merged queries that are processed in Looker memory, **Merged Results** is a post-query processing feature that, if not used thoughtfully, can overtax Looker instance resources and cause your Looker instance to respond more slowly for all users. If you can't take advantage of [in-database merge queries](https://docs.cloud.google.com/looker/docs/2614/merged-results.md.txt#in-database-merge-queries), the best practice is to define functions and logic in LookML, which generates SQL that is processed by your database. View the [Optimize Looker performance](https://docs.cloud.google.com/looker/docs/2614/best-practices/how-to-optimize-looker-server-performance) Best Practices page for more information about optimizing Looker performance.

## Conclusion

Whenever possible, you should use the data from a single Explore because your Looker developers have carefully considered how the data from different database tables should be combined. When needed, though, merging results is a powerful technique that lets you combine data from multiple Explores and databases.