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
- Executing SQL Queries
- Useful Hints for Writing Basic Queries (this blog)
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.
Useful Hints for Writing Basic Queries
Now that you know the basics of writing queries in SQL Server Management Studio here are a couple of techniques that can help speed up the process.
Including all of the Columns from a Table
When you're working with large tables and you want to include all of the columns in a query it can be annoying to have to type out all of the column names. To avoid this you can use the asterisk symbol to show all of the columns in a table:
Replace the normal column names with the asterisk symbol in the SELECT list.
This is a quick and simple way to include all of the table columns, but it does mean that you can't add aliases to the columns and you have no control over the order in which the columns appear.
There are other good reasons not to use the asterisk to select all of the columns from a table, one of which is the performance of your query.
Scripting a Table as a SELECT Statement
Rather than using the asterisk to include all of the columns from a table you can choose to script a table as a query. To do this, right-click on the table and choose the option shown in the diagram below:
Right-click on the table and choose the sequence of options shown here.
You will end up with a new query window and a complete SELECT statement that includes all of the columns from the selected table:
You can now remove any columns you don't want, put the columns in a different order and add your own aliases.