Developing and Designing Queries Using SQL Management Studio - SQL Resources and Help

Send a Message


 

 

 


 
  

Need help with Microsoft SQL Server? Ask our Experts, or simply Email your query.

About Sql Server Programmers

Our clients can be found both locally in the Chicagoland area and throughout the country. We have over 16 years of experience and are extremely proud of our track record of successfully assisting hundreds of our clients to improve their productivity while focusing on cost.

Microsoft Certified Partner

  

Developing Queries

SQL Server Management Studio has created a GUI (Graphical User Interface) called a ‘Query Designer’ that easily allows users to create queries without prior knowledge of SQL.

The Query Designer allows the users to select the Data Sources of the query (which can be tables, views or functions) and select the fields they want returned by clicking the checkbox next to the name of the desired field. Joining tables can be created by clicking and dragging fields in specific tables to fields in other tables. SQL Server Management Studio allows users to view and manipulate the SQL code if desired. Any SQL table can be used in a query.

  

How to Design a Query

To design a Query using the Query Editor in SQL Management Studio, select “New Query”.  This can be selected either right clicking on the database that you want to make a query for, or by pressing the “New Query” button in the toolbar of Management Studio.  This will open a tab on the left portion of the screen.  This will look like a standard text area.  If you use the “New Query” button in the toolbar, you should make sure the desired database is selected by reviewing the drop down at the top of the application.  You will be able to change this drop down, without losing your changes to the query.

New Query

To open the Query Editor, you can right click in the query window and select “Design Query in Editor”.  You will have the same option by selecting the “Query” Menu at the top of Management Studio.

Query Editor Selection

Within the Query Editor, you will have the option of selecting the database tables that you would like to query.

Add Table

The following screen will appear. As you can see in the example below, both the Author and Book tables have been selected and now appear in the top half of the query design window.

Tables Selected

In the Client table field list in the top half of the query design window, click the check box next to the name of the field that you want to select.  I am going to select ClientId, Active and Name.

In the Project table field list in the top half of the query design window, double-click the *. Clicking on the * is the equivalent of selecting all the fields from a particular table. The query design window should appear as in the following example.

Field Selection

Once you have selected all of the fields that you want, you may want to narrow the search with criteria.  For example, you may only want the clients that are Active.  To do this, you would go to the row of the field that you want to select and create a filter on that field.  Please review the sample below.

Where Clause

This will narrow our query to list only clients that are active.  Please notice the bottom of the Query Designer Dialog.  The Query Designer created the SQL script for us.  Once you click the “OK” button, this script will be pasted into the Query window.  To run the script you press “Execute” at the top of Management Studio or press F5.