Query Builder

The Query Builder writes a SELECT query for you while you click tables and columns. You join tables, tick the columns you want, and DbSchema shows the SQL as you go.

The Query Builder only reads data. Running its query changes neither the database nor your DbSchema design.

In the free Community edition, the Query Builder works on one table: you can tick columns, filter, count and group. Joins and sub-queries require DbSchema Pro or Architect.

The Query Builder with customers joined to orders, count(*) on orders, the generated SQL at the right and the result grid below

Build a query from a table

A join combines the rows of two tables, such as each customer with their orders. The join condition says which rows belong together, such as o.customer_id = c1.customer_id. The Query Builder uses a foreign key to create the join, so you do not type the join condition. A foreign key is a column that points to a row in another table, such as orders.customer_id.

  1. Right-click a table on the diagram, such as customers, and choose Query Builder. You can also select the table and choose Query → Query Builder, or press Ctrl+B.
  2. The builder opens under the diagram. To expand it over the whole diagram area, double-click its tab.
  3. Click the arrow icon at the right of a key column, such as customer_id. It lists the tables that are connected to this table by foreign keys.
  4. Choose a table, such as Join orders via orders_customer_id_fkey.
  5. Untick the columns that you do not want in the result.
  6. Press Run.

The SQL on the right updates with every click. The result grid, the table under the builder, shows the rows that the query returns.

The join is an inner join: it keeps only the customers that have orders. To change that, click the join label on the line between the tables, and choose Left Join or Full Join. A left join also keeps the customers without orders. A full join keeps the rows of both tables, with or without a match.

If your database has no foreign keys, the builder can follow virtual foreign keys. A virtual foreign key exists only in your DbSchema design, not in the database.

Filter the rows

A filter becomes a WHERE condition, such as o.status = 'DELIVERED'.

  1. Right-click a column in the builder and choose Filter.
  2. Choose the operator, the way to compare the values, such as equals. Then type the value.
  3. Press Apply.
The Filter Dialog for the column status, with equals and the value CANCELLED

The filter appears in italics at the bottom of the table. To sort the result, use Order Ascending (A to Z, smallest first) or Order Descending in the same right-click menu.

Count and group rows

An aggregate function calculates one value from many rows. For example, it can count the orders of each customer.

  1. Right-click a column, such as order_id in orders.
  2. Choose Aggregate, then a function, such as Count(*). The other functions are Min(?), Max(?), Count( distinct ?), Avg(?) and Sum(?). The ? stands for the column that you right-clicked.
customers joined to orders, with first_name and last_name ticked, and count(*) at the bottom of orders

DbSchema adds a GROUP BY with the other ticked columns. GROUP BY puts the rows with the same values in one group. In this example, the query counts the orders of each combination of first and last name. Two customers with the same name fall into one group, so tick customer_id as well to count per customer.

Join tables that have no foreign key

Two tables can have a column in common even if no foreign key connects them. For example, orders and reviews both have a customer_id. Drag one column onto the other to join the tables on it.

  1. Drag a table from the project tree on the left into the builder, such as reviews. If no foreign key connects it to the tables of the query, DbSchema adds it without a join.
  2. Drag a column from the new table, such as reviews.customer_id, onto a column of a table that is already in the query, such as orders.customer_id.
The Query Builder with orders joined to reviews by an Inner Join line from orders.customer_id to reviews.customer_id, and the INNER JOIN in the SQL at the right

The builder adds an inner join on that pair of columns. To add another pair of columns to the join condition, drag another column in the same way. To remove a pair, click the join label. Then choose the Remove option for that pair of columns, such as Remove r.customer_id = o.customer_id.

In the query, each table has only one parent table: the table that it is joined to. If a drag would give a table a second parent, DbSchema stops it and explains why.

Keep rows that have a match in another table

A sub-query is a query inside another query. Use a sub-query to find the rows that have, or do not have, a match in another table. For example: the customers who have cancelled an order.

  1. Join the two tables, such as customers and orders, as Build a query from a table shows.
  2. Click the join label on the line between the tables.
  3. Under As a Sub-query, choose In.
  4. To keep only some of the orders, add a filter in orders, such as status equals CANCELLED, as Filter the rows shows.
  5. Press Run.
customers with an IN sub-query on orders, drawn as a dashed frame with the filter status = 'CANCELLED', the WHERE customer_id IN SELECT in the SQL at the right, and 99 customers in the result

DbSchema draws a dashed frame around the tables of the sub-query and writes WHERE c1.customer_id IN (SELECT o.customer_id FROM shop.orders o WHERE o.status = 'CANCELLED'). The result lists each matching customer only once, even if several orders match. Ticking a column inside the frame does not add it to the result.

The other kinds of sub-query are in the same menu:

ChooseTo keep the rows
Inwhose column value is in the result of the sub-query
Not Inwhose column value is not in the result of the sub-query
Existsthat have at least one matching row
Not Existsthat have no matching row

Not In with the same filter lists the customers who have never cancelled an order. To turn the sub-query back into a join, choose Inner Join in the same menu.

Keep the query for later

When you close the builder, DbSchema asks Save this Query Builder?.

  • Save and Close keeps the query in the project. Open it again with Query → Reopen. The query is saved to the project file when you save the project.
  • Don't Save closes the builder without saving the query.

Common problems

The join list says "Joining tables requires the Architect or Pro edition"

Your license does not cover Pro or Architect. Register a license for either edition.

"No foreign keys found."

No foreign key links this column to another table. Drag a column onto a column instead, as Join tables that have no foreign key shows, or add a virtual foreign key.

"Tables Already Connected"

The two tables are already connected through other tables of the query. A direct join would make a circle of joins. Use the connection that exists, or remove one of the joins on that path first.

Next steps