WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 481 reviews for our classroom and online training
Basic SQL Queries
Part two 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 (this blog)
  3. Adding Columns to the SELECT List
  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.

Referring to Tables in the FROM Clause

The simplest query that you can write in SQL Server involves two main keywords: SELECT and FROM.  You can start your query by writing these two words on separate lines:

Basic keywords

You don't have to type the words in capital letters, although it's conventional to do so.


At this point, depending on which version of SQL Server Management Studio you're using, you might see part of your code underlined in red.  This is simply because you haven't finished writing a complete statement yet.  To make a complete statement you need to add some information to both the SELECT and FROM parts of your code.

The Order of Things in SQL

The order of words in an SQL query is very important: SELECT must always appear before FROM for instance.  The diagram below shows the order of the six main keywords in a SQL query:


--The list of columns you want to see


--The tables containing the selected columns


--Any criteria the columns must meet


--Any columns you want to use to group the data


--An extra way to write certain types of criteria


--Any sorting you want to add to the results

Although the order in which the words appear is important, the order in which you write the various parts of a query doesn't matter at all.  For this example we're going to start by filling in the FROM part of the statement before returning to the SELECT part later.  There are good reasons for doing this, as you'll see!

There are lots of mnemonics to help you remember the order of the keywords in a SQL query; my favourite is Sweaty Feet Will Give Horrible Odours!

Dragging Table Names from the Object Explorer

The FROM part of the SQL statement is used to list the tables you want to use in your query.  You can see a list of the tables in your database in the Object Explorer window on the left of the screen:

List of tables

You'll need to expand the Databases folder, then the database you are working in and finally, the Tables folder within it.


Once you've discovered the name of the table you want to use you can either type it in to your query, or click and drag it in using the mouse:

Dragging a table name

Click and drag the table from the Object Explorer into the query. Position the cursor after the FROM keyword and release the mouse button.

When you release the mouse button the name of the table will appear in your query:

Copied table name

The dbo. part of the table name is referred to as the schema. It will always be included when you drag a table name into your query, but in most cases you can omit this part.


Using Intellisense to Add a Table to a Query

If you didn't want to drag a table name into your query you can also simply type it in.  Depending on which version of Management Studio you are using you might get some help with typing your table names in the form of the Intellisense feature.

Intellisense for table names

When you start typing in the name of a table the Intellisense list will show all of the tables in the current database.


When you have the name of a table highlighted in the list you can double-click it with the mouse to add it to the query, or press Enter, Tab or Spacebar on the keyboard.

The Intellisense feature will list the tables from the database that is selected in the drop down list at the top of the screen.

Selected database

If you don't see the list of tables you were expecting, make sure that you've selected the correct database from this list.


What's Next?

Now that you've specified the table you want to use in your query the next step is to choose which columns you want to see.  Read the next part of this series to find out how.

This blog has 0 threads Add post