560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
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.
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.
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.
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.
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.
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.
|Parts of this blog|
25 Aytoun Street