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 (this blog)
- Referring to Tables in the FROM Clause
- Adding Columns to the SELECT List
- 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.
Getting Started with SQL Queries
This blog teaches you how to write the most basic of SQL queries and will introduce you to some good habits that are worth getting into early on in your query-writing career. If you haven't already done so, now is the time to open SQL Server Management Studio and get ready to start querying.
Creating a New Query Window
Before you can start writing a query you need to create a new query window. There are several ways to do this, but whichever method you choose it makes sense to select the database on which you'll be running the query first:
Use the Object Explorer window to select the database you want to use. Here we've clicked on the name of the Movies database.
Once you've selected the database you can create a query window in one of several ways:
- Click the New Query button on the toolbar.
Look for this button at the top left of the screen.
- From the menu choose: File > New > Query with Current Connection.
Choose this option to create a new query window.
- Right-click on the database and choose New Query.
Right-click on the database you want to use and select the option shown here.
- Press CTRL + N on the keyboard.
Whichever method you choose you should see a new query window appear with a flashing text cursor in the top left corner.
You should also see the name of the selected database in the drop down list on the toolbar.
Adding Comments to a Query
You can use comments to explain what your query does to other users, or even yourself when you come back to look at your code later. You can add a comment to any line in your query by typing in two dashes, as shown below:
After typing in two dashes you can start typing your comment.
You can also add comments over multiple lines, as shown in the diagram below:
Use /* symbols to start a multi-line comment and */ symbols to end it.
Although it takes time, adding comments to your SQL code is a good idea, especially when you're first learning the language.
Now that you have a query window with a basic comment you can start writing the actual query! Read the next part of this blog to find out what to write.