BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
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.
- Getting Started with SQL Queries
- Referring to Tables in the FROM Clause
- Adding Columns to the SELECT List (this blog)
- Executing SQL Queries
- Useful Hints for Writing Basic Queries
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:
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:
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:
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:
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:
|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:
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:
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:
You can use either square brackets or single quotes to enclose a multi-word alias.
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.