Prerequisite: Login to MySQL Database in MySQL Workbench
You must have MySQL Workbench on your system having a database in it. If you don’t have MySQL Workbench, then download it. Before that, ensure that MySQL is installed on your system by typing this command:
The output will display the installed version of MySQL on your system:
Simply download MySQL Workbench from the official website of MySQL and install it.
Connect it to the MySQL server by selecting the already existing MySQL server:
Note: MySQL will prompt you for a username and password for successful login, enter the required parameters, and you will successfully log in to the MySQL server
How to Execute SQL Query in MySQL Workbench?
To execute an SQL query, simply click on the first icon on the top bar, as displayed in the Image below:
In the new tab, you can write the SQL queries that you want to execute:
Example 1: Execute a Selected Portion or Everything Written in SQL Query Tab
If the user wants to enlist all the available databases in the MySQL server, type this query:
After writing the query, click on the first “thunderbolt” icon (as shown in the image below), to execute the query and the output will enlist the name of all databases:
Lets type query to enlist all the tabled from a specific database, type this query:
USE linuxhint;
SHOW TABLES;
After writing the query execute the query by click on the icon:
Note: You can even select the portion of the query and then press the same icon to execute only the selected query.
Example 2: Execute the Statement Under the Keyboard Cursor in the SQL Query Tab
If the user wants to run a query written in a single line, then place the cursor at the beginning of the line and click on the “Execute the statement under the keyboard cursor” icon.
You can see that a particular line having the cursor in its start is only executed.
Let’s see another example to write the query to show the data of the “time” table:
Place the cursor in that line and press the “Execute the statement under the keyboard cursor” icon:
The output will display the result of this single query no matter how many other queries are variable in the SQL query tab.
You have successfully executed SQL query in MySQL Workbench.
Conclusion
In MySQL Workbench, execute SQL queries by opening the query tab. Type the query and run by clicking on the “Execute the selected portion of the script or everything, if there is no selection” icon or click on the “Execute the statement under the keyboard cursor” icon to run a single query with the cursor. This post demonstrated how to execute SQL queries in MySQL Workbench.