In a Structured Query Language (SQL) database, commands are executed via the use of queries, which are specific instruction sets run against the database for performing a multitude of tasks, such as formatting tables, removing or adding data, and executing stored procedures.

Although creating a custom SQL query requires programming skill and a thorough knowledge of the Structured Query Language, running a query through SQL Server Management Studio is easily accomplished.

Step 1

First, you will want to log into SQL Server Management Studio either locally on your server, or via remote connection. For the purposes of this article, we will use an example where the user is logging in locally using Windows Authentication.

How to Run a SQL Query


Step 2

Next, you will want to select from the list of databases the one which you are running the query against. In this example, we will be running it against the ‘master’ database.

(Please note that this is only an example used for the purposes of this article, and the ‘master’ database should be edited with extreme caution, as it is inexorably tied to all other databases hosted on the server, and contains several vital stored procedures.)

Once the database is selected, click on the button in the upper left-hand corner of the window which says, ‘New Query‘.


How to Run a SQL Query



Step 3

A window will then open beside the database list, enabling you to enter your query. Once it has been entered, click the button which says ‘Execute‘ at the top of the window. Management Studio will then run your query against the selected database, and once processed, it will confirm whether or not the query was run successfully.


How to Run a SQL Query



Step 4

Some queries will be pre-compiled in a file with a ‘.sql’ extension. These are easily run, as well. Essentially, once the window in the above picture is open, double-click the ‘.sql‘ file, and it will automatically open within Management Studio. Once opened, click ‘Execute‘.

Please bear in mind that it is always good practice to back up a database prior to running any type of query against it.

Was this answer helpful? 386 Users Found This Useful (384 Votes)