Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers 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
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
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:
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 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:
SELECT
--The list of columns you want to see
FROM
--The tables containing the selected columns
WHERE
--Any criteria the columns must meet
GROUP BY
--Any columns you want to use to group the data
HAVING
--An extra way to write certain types of criteria
ORDER BY
--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!
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:
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:
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:
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.
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.
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.
If you don't see the list of tables you were expecting, make sure that you've selected the correct database from this list.
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.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.