How to Create Joins in Queries
Joins are used to combine rows from two or more tables, based on a related column between them. In this tutorial, we provide you with two examples of how to create joins in queries.
Simple Example
First let us consider a simple example of joins with two tables — parent and child. For this let's query the number of Salesforce accounts owned by users for each user. Since there are a number of relations between the account and user tables, we need to specify the relation to join the tables by. In order to create this query, you need to perform the following steps:
-
Click + Create New in the top menu.
-
Click Builder under QUERY.
-
In the Select connection list, select your connection to Salesforce.
-
First, we need to open the child table. In our case it is the Account table. Click the Account table in the Connection Object List. You can quickly find it by typing "Account" in the Type to filter box in the top part of the Connection Object List.

-
Drag the Records count pseudo-field from the Connection Object List to the Result Fields pane.

-
Navigate to the OwnerId field and click the
icon to the right of this field (you can use the Type to filter box to quickly find it as well). The User table will open. -
Drag the Name field from the Connection Object List to the Result Fields pane.

That's all, our query is ready. It correctly joins the Account and User tables by the OwnerId field. You can switch to the SQL view by clicking SQL on the Query toolbar and check the generated SQL statement. Note that the User table is assigned with an alias Owner corresponding to the name of the relation between the tables.
Complex Example
Now consider a more complex example with several tables and multiple relations in the hierarchy. This example uses SQL Server and AdventureWorks2022, a Microsoft sample database. You can download it from the AdventureWorks sample databases page.
The query counts orders by salesperson and sales territory. Here are the tables that participate in the query.

The number of orders comes from the Sales.SalesOrderHeader table, and the territory name comes from the Sales.SalesTerritory table. Salesperson names are stored in the Person.Person table. However, Person.Person also stores customer names, and Sales.SalesOrderHeader is related to it through the Sales.Customer table as well. To get salesperson names, join the tables all the way through the Sales.SalesPerson and HumanResources.Employee tables by the corresponding relations.
To create this query, perform the following steps:
-
Click + Create New in the top menu.
-
Click Builder under QUERY.
-
In the Select connection list, select your connection to the AdventureWorks2022 SQL Server database.
-
Find the "most child" table of the query. In this query, no table has Sales.SalesOrderHeader as a parent, so click Sales.SalesOrderHeader in the Connection Object List. To find it quickly, type "SalesOrderHeader" in the Type to filter box at the top of the Connection Object List.

-
Drag the Records count pseudo-field from the Connection Object List to the Result Fields pane.
-
Find the SalesPersonID field and click the
icon to the right of it. You can use the Type to filter box to find the field quickly. The Sales.SalesPerson table opens. -
Click the
icon to the right of the BusinessEntityID field. The HumanResources.Employee table opens. -
Click the
icon to the right of the BusinessEntityID field again. The Person.Person table opens. -
Drag the FirstName and LastName fields from the Connection Object List to the Result Fields pane.
-
Now add the territory name. Go back to the Sales.SalesOrderHeader table, which has a foreign key to the Sales.SalesTerritory table. For this, click the
button in the Connection Object List header. The breadcrumbs list shows the tables you have navigated through and the foreign key fields used for navigation.
-
Click the Sales.SalesOrderHeader table.
-
Find the TerritoryID field and click the
icon to the right of it. The Sales.SalesTerritory table opens. -
Drag the Name field from the Connection Object List to the Result Fields pane.

The query is ready, and it joins the tables correctly. To check the generated SQL statement, click SQL on the Query toolbar. Note that the joined tables have aliases generated by concatenating the foreign key names used for joins.
Last updated: