The Query tool is a powerful, feature-rich environment that allows you to execute arbitrary SQL commands and review the result set. If you access the Query tool via the *Query Tool* menu selection from the *Tools* menu, you can:
* Issue ad-hoc SQL queries.
* Execute arbitrary SQL commands.
* Save the data displayed in the output panel to a CSV file.
* Review the execution plan of a SQL statement in either a text or a graphical format.
* View analytical information about a SQL statement.
If you open the Query tool via the *View Data* context-menu selection, the Query tool acts as a data editor, allowing you to:
* View or modify the data that is stored in a table.
* Filter the result set.
* Save the data displayed in the output panel to a CSV file.
* Review the execution plan of a SQL statement in either a text or a graphical format.
* View analytical information about a SQL statement.
..image:: images/query_tool.png
The Query tool features a toolbar with frequently used options, and a work environment divided into two panels:
* The upper panel of the Query tool contains the *SQL Editor*. You can use the panel to manually enter a query, or review the query that generated the result set displayed in the lower panel.
* The lower panel of the Query tool contains the *Data Output* panel. The output panel displays the result of a query, or information about a query's execution plan.
pgAdmin allows you to open multiple copies of the Query tool (in individual tabs) simultaneously. For example, if you select *Query tool* from the *Tools* menu, the Query tool opens in a tab labeled *Query-1*; if you open the Query tool again (without closing *Query-1*), a second copy will open in *Query-2*. To close a copy of the Query tool, click the *X* in the upper-right hand corner of the tab bar.
**The Query tool Toolbar**
The *Query tool* toolbar uses context-sensitive icons that provide shortcuts to frequently performed tasks. If an icon is highlighted, the option is enabled; if the icon is grayed-out, the task is disabled.
..image:: images/query_toolbar.png
Hover over an icon to display a tooltip that describes the icon's functionality.
The *SQL editor* panel contains a workspace for entering commands; you can read a query from a file, or type a query. It features syntax coloring and autocompletion to help you write queries.
..image:: images/query_sql_editor.png
To use autocomplete, start typing your query, and press the Control+Space shortcut to see a list of possible object names to insert. For example, type "\*SELECT \* FROM\* " (without the quotes, but with the trailing space), and then press the Control+Space key combination to select from a popup menu of autocomplete options.
..image:: images/query_autocomplete.png
After entering a query, select the *Execute/Refresh* icon from the toolbar. The complete contents of the SQL editor panel will be sent to the database server for execution. To execute a section of the code that is displayed in the SQL editor, highlight the text that you want the server to execute, and click the *Execute/Refresh* icon.
..image:: images/query_execute_section.png
The message returned by the server is displayed on the *Messages* tab of the output panel. If the command is successful, the *Messages* tab displays execution details.
..image:: images/query_tool_message.png
The editor also offers several features that help with code formatting:
* The auto-indent feature will automatically indent text to the same depth as the previous line when you press the Return key.
* Block indent text by selecting two or more lines and pressing the Tab key.
* Block outdent text that has been indented using the Shift+Tab key combination.
* Comment out SQL by selecting some text and pressing Control+K.
* Uncomment SQL by selecting some text starting with a comment and pressing Control+Shift+K.
* Shift the selected text to upper case by pressing Control+U.
* Shift the selected text to lower case by pressing Control+Shift+U.
**The Data Output Panel**
Use the *Data Output* panel of the *Query tool* to view data and information generated by a query in the *SQL editor*, or to *View Data* for an object currently selected in the *pgAdmin* tree control.
The *Data Output* panel organizes output through the following tabs: *Data Output*, *Explain*, *Messages*, and *History*.
..image:: images/query_output_data.png
If the Query tool is opened through the *Query tool* menu option, you can use the *Data Output* tab to view the results of an arbitrary query in a table format. If the Query tool is opened through a *View Data* menu option, you can use the *Data Output* tab to review the data stored in the selected table.
* Click a column header to sort data by that column. Click again to reverse the sorting order.
* If enabled, use the *Filter* options from the Query tool toolbar to refine the result set displayed on the *Data Output* tab.
* If enabled, use the *No limit* drop-down to specify how many rows to display on the *Data Output* tab.
* If enabled, use the *Execute/Refresh* options to retrieve query execution information and set query execution options.
* Use the *Download as CSV* icon to download the content of the *Data Output* tab as a comma-delimited file.
All rowsets from previous queries or commands that are displayed in the *Data Output* panel will be discarded when you invoke another query; open another query tool from the menu bar or context-menu to keep results available.
Use the *Explain* tab to view a graphical representation of a query:
..image:: images/query_output_explain.png
To generate a graphical explain diagram, navigate to the *Explain* tab, and select *Explain*, *Explain Analyze*, or one or more options from the *Explain options* menu on the *Execute/Refresh* drop-down. Please note that *EXPLAIN VERBOSE* cannot be displayed graphically. Hover over an icon on the *Explain* tab to review information about that item; a popup window will display information about the selected object:
..image:: images/query_output_explain_details.png
Note that the *QUERY PLAN* that accompanies the *Explain analyze* is available in the *Data Output* tab.
Use the *Messages* tab to view information about the last-executed query; if the server returns an error, the error message will be displayed on the *Messages* tab:
..image:: images/query_output_error.png
If a query succeeds, the *Messages* tab displays how long the query took to complete and how many rows were retrieved:
..image:: images/query_output_messages.png
Use the *History* tab to review activity for the current session:
..image:: images/query_output_history.png
To erase the content of the *History* tab, select *Clear history* from the *Edit* drop-down menu.