SQL generation

How to Select Tables and Review Joins in Generated SQL

Start with the tables you know are relevant, investigate disconnected tables, review bridge tables, and check the joins SQL Mocker generates.

How do I select the right tables and review SQL Mocker's generated joins?

Start by entering the tables you already know are relevant to your question in Exact table names. SQL Mocker imports their metadata and, where supported foreign-key metadata identifies one, can include a one-step bridge or link table needed to connect them. If the reviewed tables remain disconnected, use Investigate Relationships to analyse possible connections before confirmation. After you review the schema and relationship/ERD map, SQL Mocker uses that context together with your question to generate the join path and join types. Review the generated SQL before use.

Load the tables SQL Mocker needs

Select Write SQL with AI, select your Database System, and then choose Extract Schema. Enter the tables you already know are relevant. SQL Mocker uses these as the starting point for metadata extraction and may include a one-step bridge or link table where supported foreign-key metadata identifies one.

Choose the initial Exact table names

Begin with the information the question must return. Enter the tables you know supply its business entities, measures, filters, or grouping fields. Use Schema Discovery to identify an unfamiliar supporting table, then add it to the extraction before investigating how the loaded tables connect.

You do not need to know the full join path or enter join syntax in advance. SQL Mocker uses relationships imported from the database metadata to determine how the reviewed tables connect. Avoid adding unrelated tables merely because their names match the general topic.

Review the relationship context SQL Mocker will use

Check the imported schema

  • Confirm that the tables and fields needed for the question are present.
  • Remove unrelated tables or fields that should not be available for generation.
  • Open Relationships Overview and review how the imported tables connect.
  • Confirm any bridge or link table that SQL Mocker added from foreign-key metadata.
  • Select Investigate Relationships when tables that should connect remain disconnected.

Review the join type SQL Mocker generates

In the SQL Generation Workspace, SQL Mocker chooses the join type from the row preservation intent in your question and the reviewed relationship path. For example, a request for every customer, including customers without orders, normally requires a LEFT JOIN; a request limited to customers with matching orders normally uses an INNER JOIN.

Review the generated SQL to confirm that the join preserves or excludes unmatched rows as intended. Relationship metadata identifies how tables connect, but it does not prescribe a fixed join type. Refine the question when the reporting requirement needs to be more explicit.

Include bridge and history tables in the reviewed path

When you use Extract Schema, the exact table names are the starting set. Automatic one-step bridge discovery is currently available for SQL Server, PostgreSQL, Oracle, MySQL, MariaDB, and Snowflake when their catalogues expose declared foreign keys. SQL Mocker includes an unlisted bridge or link table when it connects the requested tables. For other database systems, enter the bridge or link table name yourself. Review the expanded path in Relationships Overview before generating SQL.

When a bridge table's foreign keys are missing, incomplete, unavailable from the selected database system, or more than one relationship step away, SQL Mocker does not add it as a database-defined connection. Use Schema Discovery if you first need help identifying the bridge table, then add its exact name to the extraction. In Relationships Overview, select Investigate Relationships to generate database-specific investigation SQL and analyse structural evidence for the disconnected tables. Historical tables may also require date-range conditions in addition to identifiers.

What SQL Mocker determines and what you should review

SQL Mocker can determine

Available relationship path

You should review

Whether the path matches the business meaning

SQL Mocker can determine

Supported bridge tables

You should review

Whether the bridge is appropriate

SQL Mocker can determine

Join type based on the question

You should review

Whether unmatched rows should be kept or excluded

SQL Mocker can determine

Database-specific SQL syntax

You should review

Whether the final query returns the intended result

Use Schema Discovery first, then Investigate Relationships

If you are unsure which tables supply the required fields or whether an intermediate table is needed, describe the intended result in Schema Discovery. It can suggest likely tables, columns, joins, and bridge tables to investigate. After confirming the database system and likely table names, generate the metadata SQL and load the returned structure into the workspace.

If those tables remain disconnected, open Relationships Overview and select Investigate Relationships. Run the generated investigation SQL in your database, return the results, and review the structural evidence for each candidate. You can also select one or more candidates for further data validation before confirming or rejecting them. Confirmed suggestions are stored as discovered workspace relationships, not as database-defined connections.

See Discover Missing Table Relationships for a walkthrough of the investigation and confirmation workflow.

Example: joining orders to customer regions

The query below follows two reviewed relationships: orders to customers, then customers to regions. Each join has a clear purpose in the requested output.

This example is illustrative. SQL Mocker generates the appropriate syntax for the database system selected in the workspace.

Illustrative multi-table query
SELECT
    r.region_name,
    SUM(o.total_amount) AS total_order_value
FROM orders AS o
JOIN customers AS c
  ON c.customer_id = o.customer_id
JOIN regions AS r
  ON r.region_id = c.region_id
GROUP BY r.region_name
ORDER BY total_order_value DESC;
AI-generated SQL may contain errors. Review and test before use.

Review the generated SQL before use

Relationship metadata identifies how tables connect, but it does not prescribe a fixed join type. SQL Mocker chooses the join type from the user's question, so the final query should be reviewed before it is copied to a database tool.

  • Confirm that the generated query uses the tables expected for the question.
  • Check that any bridge or link table follows the path reviewed in Relationships Overview.
  • Confirm that INNER, LEFT, or another join type preserves or excludes unmatched rows as intended.
  • Refine the question when the reporting requirement or row-preservation intent needs to be more explicit.

Ready to generate SQL?

Add the tables you already know, review the imported relationships, and describe the result you need. SQL Mocker uses that reviewed schema context to generate SQL tailored to your selected database system.

Related guides

Table relationships

How to Find Table Relationships When Foreign Keys Are Missing

Use Investigate Relationships to identify, validate, and confirm likely table connections when foreign-key metadata is missing or incomplete.

Read guide

AI SQL generation

How to Generate SQL for Your Database Without Connecting It to AI

Use extracted schema metadata and natural-language questions to generate SQL tailored to your database without connecting it to AI.

Read guide