Basic SQL Queries
Part three 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 (this blog)
  4. Executing SQL Queries
  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.

Adding Columns to the SELECT List

The SELECT list in a query is used to specify the columns you want to see in the results.  Just as with table names you can drag column names in from the Object Explorer window, use the Intellisense feature to insert them, or simply type them in.

Finding Column Names in the Object Explorer

Before you can drag a column name into a query you'll need to expand the relevant folders, as shown in the diagram below:

Dragging column names

Expand the database you are working in, then the Tables folder, followed by the table you are using and finally the Columns folder.

 

You can now simply drag the column names into the query between the SELECT and FROM keywords:

Columns in a query

The column names must appear after the SELECT keyword and before the FROM keyword.

 

Using Intellisense to List Column Names

It's potentially much quicker to use the Intellisense feature to add column names to a query, especially if you've already added a table name to the FROM clause of the query:

Intellisense column names

Simply start typing the name of a column to make the Intellisense list appear.

 

Listing Multiple Columns

You can add multiple column names to the SELECT list either on a single line or with each column name on a separate line. You must add a comma before each extra column that you are listing:

List of column names

You can add as many column names as you like, just make sure to separate them with commas.

 

If you list your column names on separate lines you can add a comma to either the end of a line or the start:

Adding commas to SQL Commas at the start of lines
You can add commas to the end of a column name on one line... ... or to the start of the name on the following line.

To begin with it might seem a bit odd typing a comma at the start of a line but I think it makes it much easier to spot when you've accidentally missed one out or added too many.

Changing Column Titles

You can alter the name of a column in your query by giving it an alias.  You can do this simply by typing the title you want to use immediately after the column name:

Simple alias

If your alias is a single word, such as Title, you can type it in after the column name as shown here.

 

Although it isn't necessary, you can add the AS keyword to make your aliases more obvious:

Alias with AS

Put the AS keyword between the column name and alias to make the alias stand out.

 

If you want to use an alias that includes two or more separate words you'll need to enclose the alias in either a set of square brackets or a set of single quotes:

Multi word alias

You can use either square brackets or single quotes to enclose a multi-word alias.

 

What's Next?

Now that you've added some column names to the SELECT list you have a complete query that is ready to be executed.  The next part of this blog series explains how to execute your query.

This blog has 0 threads Add post