Working with multiple tables in a Query
Often Queries will use information from a single source, however it is possible to specify multiple sources.
- Joins – where relationships are defined between tables, allowing you to add columns from each. See below...
- Unions – where tables are combined to form a resultset. Read more...
A Join (known technically as an 'Inner Join') is used to indicate a relationship between two tables, and tells the Query to combine records from each table based on matches at the join.
To use fields from more than one table
- Right-click in the top pane and choose Add Source
- Select and Add each of the sources
- Close the Add Source dialog
- Drag the fields down from the list in the top pane to the grid below
- Join the tables in the top pane (see below)
To specify a join
- Click on the source field in one table, and
- Drag to the corresponding field on the other table
- A line will be displayed to indicate the join
To remove a join
- Click on the join line to select it. When selected it is shown in bold
- Right click on the line to display its context menu
- Click 'Remove Join'
It is possible (albeit unusual) to join two tables with more than one join line. In that case each join relationship must match simultaneously for the records to show in the resultset.
It is possible for more than two tables to be involved in joins, with 'table a' joining to 'table b' and 'table b' joining to 'table c' etc.
If more than one table is added to a Query, but there is no join line, then the resultset will show each row in the first table multiple times, once for each row in the second table. This is known as a 'Cross Join', or 'Cartesian Product Join'.
This can be useful if the second table only has one record, to provide access to the field values in that table.