Basic SQL Queries
Part four of a five-part series of blogs

This blog teaches the absolute basics of writing queries in Microsoft SQL Server Management Studio. You'll learn how to use the SELECT and FROM keywords to create simple queries showing all of the records from a single table.

  1. Getting Started with SQL Queries
  2. Referring to Tables in the FROM Clause
  3. Adding Columns to the SELECT List
  4. Executing SQL Queries (this blog)
  5. Useful Hints for Writing Basic Queries

This blog is part of our complete SQL tutorial.  Alternatively, have a look at our SQL training courses for small groups.

Posted by Andrew Gould on 23 July 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Executing SQL Queries

When you've finished writing a query you need to execute it in order to see the results.

Using the Correct Database

When you execute a query you should make sure that you will be doing so on the correct database.  You can do this by selecting the name of the database you want to use from the drop down list at the top of the screen:

Selecting a database

Use this drop down list to select the database on which you want to execute the query.


To avoid having to select the database from a drop down list you can write a USE statement at the top of your query:

Using USE

Write the keyword USE followed by the name of the database you want to use.


The GO keyword executes a batch of SQL statements.  Certain SQL statements must be the first statement in a batch and so the GO command is used to start a new batch.  In the above example the GO keyword is optional.

Selecting the Query

Before you execute the query you should make sure that you've selected the relevant part of your code; Management Studio will execute whichever bit of text you have selected.  When you have only written a single query on a page you can click anywhere on the page to select it:

Selecting single query

Click anywhere on the page to select this single query.


When you've written multiple queries on a page and you only want to run one of them you must make sure you've selected the entire query that you want to execute:

Multiple queries

This page contains three separate SELECT statements. We've selected the middle one of the three to ensure that only this one will run when we choose to execute it.


Executing a Query

When you've made sure you're using the correct database and you have the appropriate text selected on the page, click the Execute button at the top of the screen to run your query:

Executing query

You could also press F5 on the keyboard to run a query.


By default you should see the results of your query in a grid at the bottom of the screen:

Query with results

The Results grid appears at the bottom of the screen.


You can close the Results panel by pressing CTRL + R on the keyboard.

Changing How Results are Presented

You can change the output style of a query to be presented as text or even exported to a file.  Use the toolbar at the top of the screen to choose the output style of a query:

Changing query output

The three buttons shown here in the red box allow you to change the output style of a query.


Once you've selected one of the three buttons you'll need to execute the query again to see the results.  If you choose Results to Text you'll see the output in the Messages panel at the bottom of the screen:

Results to text

This option isn't much different from the original Results to Grid option.


If you choose Results to File you can choose to save the results when you execute the query:

Saving query results

The default file type is a Crystal Reports report, but you can change this to All Files (*.*) using the drop down list at the bottom of the dialog box and add a .txt extension to the filename.

You can open the output file using Notepad or any other text editor on your computer:

Results in file

Open the file using a text editor to see the results.


Saving and Opening Queries

Queries like the one we've written here aren't saved as part of the database so if you want to be able to reuse it you need to save it.  To do this, from the menu choose: File > Save, or press CTRL + S on the keyboard:

Saving a query

Choose a location and enter a file name, then click Save.

When you want to reuse a query you need to open the file.  To do this, from the menu choose: File > Open > File... or press CTRL + O on the keyboard.

Opening a query

Browse to the location of the saved file and double-click on it or select it and click Open.

What's Next?

The final part of this blog series describes a few neat extra things you can do when writing simple queries, including how to select all of the columns in a table without having to list their names.

This blog has 0 threads Add post