Navicat Blog

January 3, 2018 by Robert Gravelle

Available in Non-Essentials editions of Navicat for MySQL, PostgreSQL, SQLite, MariaDB, and Navicat Premium, the Query Builder allows anyone to create and edit queries with only a cursory knowledge of SQL. In today's blog, we'll use it to write a query to fetch a list of actors that appeared in movies released during a given year.

The Source Database

The query that we'll be building will run against the Sakila sample database. A former member of the MySQL AB documentation team named Mike Hillyer created the Sakila database specifically for the purpose of providing a standard schema for use in books, tutorials, and articles just like the one you're reading.

The database contains a number of tables themed around the film industry that cover everything from actors and film studios to video rental stores. Please refer to the Generating Reports on MySQL Data tutorial for instructions on downloading and installing the Sakila database.

Opening the Query Builder

You can think of the Query Builder as a tool for building queries visually. It's accessible from the Query Designer screen. Let's bring it up by opening a new query:

  • Click the Query icon on the main toolbar, followed by the New Query button from the Object toolbar:
  • In the Query Designer, click the Query Builder button to open the visual SQL Builder.

    The database objects are displayed in left pane, whereas the right pane is divided into two portions: the upper Diagram Design pane, and the lower Syntax pane:

Constructing the Actors for Year's Films Query

It's a good idea to select the tables first, so that the Query Builder knows which fields to present for the field list:

  • Drag a table/view from the left pane to the Diagram Design pane or double-click it to add it to query. We'll be needing the actor, film_actor, and film tables.
  • You can assign table aliases by clicking on "<alias>" beside each table. To add the table alias, simply double-click the table name and enter the alias in the Diagram Design pane.
  • Note how the Query Builder already knows the table relationships. That's because foreign key constraints have already been declared on Table objects:

  • To include a field in the query, check the left of the field name in the Diagram Design pane. To include all the fields, click at the left of the object caption. Select the actor first and last names as well as the film title.

Adding WHERE Criteria

Clicking on "<Click here to add conditions>" beside the WHERE keyword adds a default WHERE condition of "<--> = <-->".

  • Click on the left-hand "<--> = <-->" to select a field. That opens a popup dialog that contains a List of fields as well as an Edit tab.
  • Click the List tab and choose the f.release_year field.
  • Click OK to close the dialog.
  • Next, click on the right-hand "<--> = <-->" to set the release year. This time enter a value of "2006" in the Edit tab. Click OK to close the dialog.
  • Click OK to close the Query Builder. You should now see the generated SELECT statement in the Query Editor:
  • SELECT
    a.first_name,
    a.last_name,
    f.title
    FROM
    actor AS a
    INNER JOIN film_actor AS fa ON fa.actor_id = a.actor_id
    INNER JOIN film AS f ON fa.film_id = f.film_id
    WHERE
    f.release_year = 2006
    	
  • Click the Run button to execute the query. The results will be sorted by Film title:

Conclusion

Whether you're a novice or experience DBA, Navicat's Query Builder makes writing SELECT queries easier than ever before. In an upcoming blog, we'll get into some of its more advanced features.

Navicat Blogs
Feed Entries
Blog Archives
Share