Share via


How to: Create Queries

Queries allow you to retrieve data from the tables and views in your database. You create and work with queries in Query and View Designer, which is composed of four panes: the Diagram Pane, the SQL Pane, the Criteria Pane, and the Results Pane.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To create a new query

  1. In Server Explorer, right-click the Tables node for the database you want to query.

    Depending on the database management system you're connecting to, you may also be able to use functions, synonyms, and local tables.

  2. From the shortcut menu, click New Query.

    In the Add Table dialog box, select the tables you want to query and click Add for each one.

  3. Once you have added all the tables you want to query, click Close.

    To add more tables later, right-click the open space in the Diagram pane and from the shortcut menu click Add Table.

  4. In the Diagram Pane, check the boxes in the table-valued objects for each column you want to query.

  5. From the Query Designer menu, choose Execute SQL to run your query.

    Note

    If you do not see the Diagram, SQL, Criteria, or Results panes, from the Query Designer menu, point to Pane and click the pane you want to open.

To further refine your query, you can change the SQL code in the SQL Pane or choose options such as sort order and column aliases in the Criteria Pane.

See Also

Tasks

How to: Save Queries

Concepts

Expressions in Queries

Other Resources

Types of Queries

Specifying Search Criteria

Summarizing Query Results

Performing Basic Operations with Queries