Quantcast
Viewing all articles
Browse latest Browse all 18766

How To: Create a Query in One Shot

To get information from a database it is necessary to execute a query to get this data.

Usually an ordinary SQL editor is used to create queries. To use such editor, one should remember the syntax of the SELECT operator and the names of tables and columns.

Let’s use a visual instrument developed specially to design queries, and see that it’s much easier to create queries visually instead of typing them in an editor.

Task:

It’s necessary to show the salaries of the employees of departments situated in different cities for the 2008 year in descending order.

We will do this on a MySQL server database. The process of creating this database was described in the How to: Create MySQL Database in One Shot article. You can Download MySQL Demo Database (or for SQL Server Download SQL Server Demo Database).

Solution:

Let’s create an empty document in dbForge Query Builder for MySQL (dbForge Query Builder for SQL Server).

After this let’s drag tables from Database Explorer to the diagram, the order of tables during dragging doesn’t matter.

As we can see, the application joins these tables automatically.

Image may be NSFW.
Clik here to view.
Query Builder: Query Diagram

Query Builder: Query Diagram

Now let’s select the columns you need to get data from.

Click the checkbox near the Loc column of the dept table on the diagram, and after that the SalAmount column of the sal table.

You can see the selected columns on the Selection tab.

Image may be NSFW.
Clik here to view.
Query Builder: Selection Tab

Query Builder: Selection Tab

Now let’s select the sum function on this tab in the column with aggregate functions for the SumAmount column.

Now it is necessary to set grouping by the Loc column, but the application selected to group data by the Loc column automatically. Let’s make sure of that by going to the Group By tab.

Image may be NSFW.
Clik here to view.
Query Builder: Group By Tab

Query Builder: Group By Tab

Now we should cut the selection and keep only data of the 2008 year in the result.

To do this, let’s go to the Where tab and click the button with the green plus on it. The “=” symbol should appear. Let’s click the first phrase – <enter a value>.

Image may be NSFW.
Clik here to view.
Query Builder: Enter Value

Query Builder: Enter Value

After this the Operand Editor form should appear. Let’s select the Date and Time group from the Function list and double click the year(date) function in the list. After that let’s choose and double click the SalDate column in the other list.

Image may be NSFW.
Clik here to view.
Query Builder: Operand Editor

Query Builder: Operand Editor

Let’s close the form and click the second one. Let’s enter 2008 there.

Image may be NSFW.
Clik here to view.
Query Builder: Type Constant

Query Builder: Type Constant

It’s time to execute the query we’ve created visually. To do this, let’s click F5.

Image may be NSFW.
Clik here to view.
Query Builder: Query Result

Query Builder: Query Result

Now let’s look at the structure of the query we’ve created. To do this, let’s open Document Outline and open all nods.

Image may be NSFW.
Clik here to view.
Query Builder: Document Outline

Query Builder: Document Outline

Now let’s look at the DML of the created query. To do this, let’s go to the Text tab.

Image may be NSFW.
Clik here to view.
Query Builder: SELECT Query

Query Builder: SELECT Query

Conclusion:

As we can see, the usage of a visual instrument for building queries allows to solve the task visually without going deep into the refinements of syntax of the SELECT statement itself and of the specifics connected with differences between MySQL and SQL Server syntax, to look at the syntax of the created query, to decrease the duration of the data selection process, and to look at the structure of the available query as a tree.


PlanetMySQL Voting: Vote UP / Vote DOWN

Viewing all articles
Browse latest Browse all 18766

Trending Articles