Visually Creating MySQL Queries With dbForge Studio for MySQL
Tags: database, sql, query, dbforge
Published: 2020-02-19 16:41:48

*This small guidance will be useful for non-technical users who have limited knowledge of the SQL language and want to explore the data. And also for savvy SQL developers who might be struggling with a particular complex query. As the graphical tool can be an intuitive way to build the query’s logic.*

Fortunately, we have [dbForge Studio for MySQL](https://www.devart.com/dbforge/mysql/studio/) whose graphic designer allows us to select fields from multiple related tables and views, use search conditions, build subqueries, insert MySQL functions, create aggregations sort by different orders and so on. And we come to talk about this tool.

Basically, when developing your query visually with dbForge, you can easily switch to the text view to see the SQL text that’s being generated. You can also check the data view to see the data returned. This is great for debugging purposes. 

Once happy, you can save the query as an SQL script file, use it as a data source for creating a report or simply save the text as a code snippet.

**Here is an example.** 
We are looking at a simple MySQL sample database called employees. It has a few tables typical of any HR system:

![employees MySQL sample database](https://i.ibb.co/0jgCSDY/image14.png)

Let’s say, as HR users, we want to check the past employees of the company. We want to see the departments they have worked in. We want to see the job titles they held in those departments when employed, and the time they held each job title.

We can either write an SQL query, or we can click the “New Query” button in the toolbar:

![Toolbar](https://i.ibb.co/bFLkpW9/image35.png)

This opens a new query window with an empty palette. We can select the relevant tables from the “Database Explorer” pane to the palette. dbForge Studio automatically shows any existing relationship between tables:

![Existing relationship](https://i.ibb.co/RB4Yyrt/image9.png)

Under the palette, a tabbed dialog also appears. These tabs allow you to select one or more fields, change, insert or delete joining clauses and specify WHERE, GROUP BY, HAVING and ORDER BY clauses:

![tabbed_dialog_01](https://i.ibb.co/swnZLGT/image52.png)

![tabbed_dialog_02](https://i.ibb.co/FHFZ3JR/image71.png)

![tabbed_dialog_03](https://i.ibb.co/s9hnwHW/image53.png)

![tabbed_dialog_04](https://i.ibb.co/3z2MPTY/image15.png)

![tabbed_dialog_05](https://i.ibb.co/C5VfJcW/image63.png)

![tabbed_dialog_06](https://i.ibb.co/rwFjh0X/image8.png)

We will intentionally use a subquery here. We will select a subset of rows from the dept_emp table where the to_date field is not equal to ‘9999-01-01’ - in other words the employee is no longer active. To create a subquery, we right click on an empty space in the palette and choose “Create Subquery” from the context menu:

![Context menu](https://i.ibb.co/BGMrGzs/image69.png)

This opens another sub-tab under the main query tab. The original palette’s tab is shown as “Root Query” and the new palette’s tab is shown as “SubQuery”:

![Root Query](https://i.ibb.co/MP0Swvk/image11.png)

Next, we get rid of the dept_emp table from the “Root Query” palette by selecting it and pressing Delete: 

![dept_emp](https://i.ibb.co/L6k62YP/image12.png)

In the “SubQuery” palette, we drag the dept_emp table only. For this table, we are selecting all fields. In the “Where” tab below it, we specify our selection condition:

![specify our selection condition_01](https://i.ibb.co/2K4W7m8/image76.png)

![specify our selection condition_02](https://i.ibb.co/Sw23fXQ/image6.png)

![specify our selection condition_03](https://i.ibb.co/Jsbx2xP/image2.png)

We double click on the box’s title “SubQuery”, change it to “dept_emp2”.

![dept_emp2 change](https://i.ibb.co/QDMZdMQ/image86.png)

We then drag the “dept_no” field from the “departments” table’s box to “dept_emp2”, and drag the “emp_no” field from “employees” to “dept_emp2”. The palette should look something like the following:

![dept_no_departments](https://i.ibb.co/ydVSJm9/image3.png)

Next, we select different fields from the tables, create two calculated fields, specify a WHERE condition and order by two fields:

![two calculated fields](https://i.ibb.co/TvchS9X/image19.png)

If we switch to the “Text” tab at the bottom of the query window:, we can see the SQL text that was generated:
```
SELECT
  employees.emp_no AS Employee_No,
  CONCAT(employees.first_name, ' ', employees.last_name) AS Employee_Name,
  departments.dept_name AS Department,
  dept_emp2.from_date AS Worked_in_Department_From,
  dept_emp2.to_date AS Worked_in_Department_To,
  titles.title AS Job_Title,
  titles.from_date AS Worked_in_Role_From,
  titles.to_date AS Worked_in_Role_To,
  DATEDIFF(titles.to_date, titles.from_date) AS Days_Worked_in_this_Role
FROM employees
  INNER JOIN titles
    ON titles.emp_no = employees.emp_no
  INNER JOIN (SELECT
      dept_emp.emp_no,
      dept_emp.from_date,
      dept_emp.dept_no,
      dept_emp.to_date
    FROM dept_emp
    WHERE dept_emp.to_date <> '9999-01-01') dept_emp2
    ON employees.emp_no = dept_emp2.emp_no
  INNER JOIN departments
    ON departments.dept_no = dept_emp2.dept_no
WHERE titles.to_date <> '9999-01-01'
ORDER BY Employee_Name, Worked_in_Role_From DESC
```
Clicking on the “Execute” button from the toolbar will show the data in a grid in the “Data” tab:

![data in a grid](https://i.ibb.co/s9YWKGq/image81.png)

So this is how you can visually build queries in [dbForge Studio for MySQL](https://www.devart.com/dbforge/mysql/studio/). Needless to say, production grade queries in a database application can be really complex, but once you know the basics, you can easily break those queries down and start building the subqueries in the query builder. Also, we have not touched on other types of joins (the example here showed only inner joins) and other query types (INSERT, UPDATE , DELETE etc.). All these can be set and configured from the query builder.

 Cheers!