Select Data  «Prev  Next»
Lesson 1

Select Data in SQL Server

In the last module, you learned the basics of queries: how to execute them, and how to incorporate logic, system catalogs, and more. This module dives into selecting data from your databases. What would be the point of having data in your database if you could not get it back out? This module teaches you how to do that, using SQL Server 2025.

A single-table SELECT is the easy case. Real questions almost always require pulling data from more than one table at once, presenting it under more readable names, combining several result sets, filtering on a value that itself has to be calculated first, or summarizing hundreds of rows down into a handful of totals. Those are exactly the skills this module builds, one at a time, on top of the query basics the last module covered.

Learning Objectives

After completing this module, you will be able to:
  1. Construct inner and outer joins
  2. Alias your table and column names
  3. Construct union queries
  4. Write subqueries and understand when they are used
  5. Write aggregated queries
  6. Apply additional keywords that can be used in your queries
  7. Write distributed select statements

SQL Server 2022 Query Performance Tuning

A First Look at Joins

A relational database rarely answers a useful question from a single table. A JOIN combines rows from two or more tables based on a related column between them, most often a foreign key referencing a primary key. The general pattern looks like this:

SELECT columns
FROM FirstTable
JOIN SecondTable
    ON FirstTable.key_column = SecondTable.key_column;
An INNER JOIN returns only the rows that have a match in both tables. An OUTER JOIN, further split into LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN, returns all rows from one side even when there is no matching row on the other, filling in NULL where no match exists. The difference matters in practice: an inner join between an Orders table and a Customers table would silently drop any order whose customer record is missing, while a left outer join would keep that order and simply show no customer name for it.

This module's later lessons on retrieving data from multiple tables walk through both join types in full, with worked examples and sample results. For now, the key idea to carry forward is simple: which join you choose determines whether rows with no match disappear from your results or stay in them with gaps filled by NULL.

What's Ahead in This Module

Each of the remaining learning objectives gets its own dedicated lesson later in this module. A brief preview of what each one covers:
  • Aliasing. Giving a table or column a temporary, shorter name within a query, useful once a query joins several tables and the full names become unwieldy, or when two tables in a join share a column name and SQL needs a way to tell them apart.
  • Union queries. Combining the results of two or more separate SELECT statements into one result set, stacking rows from different queries together rather than joining columns side by side.
  • Subqueries. A query nested inside another query, used when a condition in your main query depends on a value, or a set of values, that itself has to be calculated first.
  • Aggregated queries. Summarizing many rows into fewer: counting, totaling, averaging, or finding minimums and maximums, typically grouped by some column of interest.
  • Additional keywords. SQL Server 2025 keywords and clauses beyond the basics covered so far, letting you filter, sort, and shape result sets with more precision.
  • Distributed select statements. Querying across more than one server or data source in a single statement, when the data you need does not all live in one place.
Taken together, these seven skills are what separate a query that can retrieve a single table's rows from one that can answer a real business question, which usually means pulling together data that lives in several tables, summarizing it, and presenting it in a useful shape.

Summary

A short recap before the next lesson:
  • This module builds on the query basics from the last module, moving from retrieving a single table's rows to answering real questions that span multiple tables.
  • A join combines rows from two or more tables based on a related column; an inner join keeps only matching rows, while an outer join keeps all rows from one side and fills in NULL where no match exists.
  • Aliasing, union queries, subqueries, aggregated queries, additional keywords, and distributed select statements each get their own dedicated lesson later in this module.
  • Examples throughout this module use SQL Server 2025 syntax and features.

The next lesson gives a fuller overview of selecting data, before the module moves into joins, aliasing, unions, aggregates, and subqueries in detail.

SEMrush Software 1 SEMrush Banner 1