SQL-Query Basics  «Prev  Next»
Lesson 2 Executing queries
Objective Describe how to execute your queries in SQL Server 2022.

Executing SQL Server 2022 Queries

Most of this course focuses on designing and writing T-SQL queries. In practice, however, your query is not useful until you know how to execute it against a SQL Server instance, review the results, and iterate quickly.

In a modern environment, you will typically execute queries against:

  • On-premises SQL Server 2019 / 2022 instances
  • Azure SQL Database or Azure SQL Managed Instance
  • Development or test servers running in virtual machines or containers

The primary tool for interactive query execution is SQL Server Management Studio (SSMS), which can connect to all of these environments. You can also use Azure Data Studio, command-line tools, and application code, but SSMS is the best starting point for learning how to execute and analyze queries.

SSMS connected to on-premises and Azure SQL Server instances
SSMS in an enterprise environment, managing on-premises and Azure SQL deployments

Installing and launching SSMS (quick overview)

SQL Server Management Studio is installed separately from the SQL Server engine. The basic process is:

  1. Download the latest SSMS installer from the official Microsoft link (for example, https://aka.ms/ssms).
  2. Run the SSMS-Setup-*.exe file and accept the default installation options.
  3. Restart your machine if prompted.

After installation, launch SSMS from the Start menu, then connect to:

  • Server type: Database Engine
  • Server name: localhost, a server name, or an Azure SQL endpoint
  • Authentication: Windows Authentication or SQL Server Authentication

Once connected, you are ready to execute T-SQL queries against your chosen database.

Executing a query interactively in SSMS

The most common workflow in SSMS is to write and run queries in a Query Editor window. Here is the typical sequence:

  1. Open a new query window
    • In Object Explorer, right-click the database engine connection and choose New Query, or
    • Click the New Query button on the toolbar.
  2. Select the target database
    • Use the database drop-down in the toolbar to select the database you want to work with, or
    • Run USE DatabaseName; at the top of your script.
  3. Type your T-SQL statement
    • Enter a SELECT, INSERT, UPDATE, or DELETE statement.
    
    SELECT TOP (10) CustomerID, CompanyName
    FROM Sales.Customers
    ORDER BY CustomerID;
    
  4. Execute the query
    • Click the Execute button, or
    • Press F5 on your keyboard.
  5. Review the results and messages
    • The Results tab shows result sets returned by the query.
    • The Messages tab shows rows affected, warnings, and any error messages.
  6. Save the script (optional)
    • Use File > Save (or Ctrl+S) to store your script in source control or a local folder for reuse.

This basic pattern—connect, select a database, write T-SQL, execute, review—forms the foundation for all query work in SQL Server 2022.

Executing batches and multi-statement scripts

SSMS allows you to execute multiple statements at once as a batch. Batches are important when you need to:

  • Declare and use variables across multiple statements
  • Create temporary tables and then query them
  • Wrap multiple statements in a single transaction

In a script, the GO keyword is interpreted by SSMS (not the SQL Server engine) as a batch separator:


USE SalesDB;
GO

DECLARE @MinAmount money = 100.00;

SELECT OrderID, OrderTotal
FROM Sales.Orders
WHERE OrderTotal >= @MinAmount;
GO

When you click Execute, SSMS sends each batch (the statements between GO lines) to SQL Server separately, in sequence. Understanding how batches are segmented helps you reason about variable scope and compilation boundaries.

Other ways to execute queries

While SSMS is the primary teaching tool in this course, you should be aware of other ways to execute queries in SQL Server 2022:

  • Azure Data Studio – Cross-platform tool focused on modern experiences and notebooks.
  • sqlcmd / PowerShell – Command-line tools for automation, scripting, and scheduled jobs.
  • Application code – Queries executed via ADO.NET, Entity Framework Core, Dapper, or other ORMs.

Regardless of the tool, the underlying process is the same: connect to the database engine, send T-SQL statements, and handle the results (or errors).

Viewing execution plans while executing queries

Executing queries is not just about getting the right answer—it is also about understanding how SQL Server produced that answer. Execution plans show how the Database Engine navigates tables, uses indexes, and joins data.

In SSMS, you can:

  1. Display an estimated execution plan (without running the query)
    • Click Display Estimated Execution Plan, or press Ctrl+L.
  2. Display the actual execution plan (for executed queries)
    • Click Include Actual Execution Plan, then execute the query.
    • Review the Execution Plan tab to analyze operators, estimated vs actual rows, and warnings.
  3. Use T-SQL options such as SET STATISTICS IO, SET STATISTICS TIME, or SET SHOWPLAN_XML to output plan and performance data in text or XML form.

In later modules you will examine execution plans in more detail, but for now it is enough to know that: SSMS lets you execute a query and immediately see how the engine processed it, which is essential for performance tuning.

Key terms

[1] Executing: Sending one or more T-SQL statements to SQL Server for processing and returning results. This is synonymous with issuing a query.
[2] Issuing: Another term for executing—submitting a statement or batch to the database engine.
[3] Optimized: Describes a query that is executed using an efficient plan chosen by the SQL Server optimizer, often after analyzing indexes, statistics, and previous executions.

SEMrush Software 2 SEMrush Banner 2