SQL Server SELECT statement retrieves data from one or more tables, views, and other queryable sources in a Microsoft SQL Server database. It is the foundation of reporting, application development, data analysis, and everyday database administration. You can use SELECT to choose specific columns, filter rows, combine related tables, calculate values, summarize records, and control how results are sorted or paged.
In this tutorial, you will learn the SELECT statement syntax step by step, with practical T-SQL examples and explanations of common clauses. The examples use familiar tables such as Employees, Products, and Orders; adjust table and column names to match your database.
SQL Server SELECT Statement Syntax
The basic syntax for retrieving data from a table is:
SELECT [DISTINCT] column1, column2, ...
FROM schema_name.table_name
WHERE condition
GROUP BY column1, column2, ...
HAVING group_condition
ORDER BY column1 [ASC | DESC];
Only SELECT and FROM are required for a simple query. The other clauses are optional and should be added when your task needs filtering, grouping, or sorting. SQL Server also supports options such as TOP, joins, set operators, and pagination.
Select All Columns from a Table
Use the asterisk (*) to return every column from a table. This is useful when exploring a table or checking sample data.
SELECT *
FROM dbo.Employees;
This query returns all columns and rows from the Employees table. In production applications, selecting every column is usually discouraged. It can transfer unnecessary data, make applications dependent on the table’s full structure, and increase the impact of future schema changes. Prefer listing only the columns your application actually needs.
Select Specific Columns
To retrieve only selected fields, write their names after the SELECT keyword. Separate multiple column names with commas.
SELECT EmployeeID, FirstName, LastName, DepartmentID
FROM dbo.Employees;
The result contains only the four requested columns. Explicit column selection improves readability and can reduce network traffic, especially when a table contains large text, binary, or JSON fields.
Use Column Aliases
An alias gives a result column a more readable name. Use AS to assign an alias to a column or calculated expression.
SELECT
FirstName + ' ' + LastName AS FullName,
MonthlySalary AS Salary
FROM dbo.Employees;
Aliases affect the column headings in the result set; they do not rename the underlying table columns. You can also use bracketed aliases when a label contains spaces, such as AS [Employee Name].
Filter Rows with the WHERE Clause
The WHERE clause returns only rows that satisfy a condition. SQL Server supports comparison operators such as =, <>, >, <, >=, and <=, as well as logical operators such as AND, OR, and NOT.
SELECT ProductID, ProductName, UnitPrice
FROM dbo.Products
WHERE UnitPrice >= 50;
You can combine conditions to narrow the result:
SELECT ProductName, UnitPrice, CategoryID
FROM dbo.Products
WHERE UnitPrice BETWEEN 10 AND 100
AND CategoryID IN (1, 2, 5);
Use LIKE for pattern matching. The percent sign (%) represents zero or more characters, while the underscore (_) represents one character.
SELECT ProductName
FROM dbo.Products
WHERE ProductName LIKE 'C%';
Remember that comparisons with NULL require IS NULL or IS NOT NULL; an equality comparison such as ColumnName = NULL does not work as expected.
Sort Results with ORDER BY
Use ORDER BY to arrange rows. ASC sorts in ascending order and is the default; DESC sorts in descending order. You can sort by more than one column.
SELECT ProductName, UnitPrice
FROM dbo.Products
ORDER BY UnitPrice DESC, ProductName ASC;
This query places the most expensive products first. Products with the same price are then sorted alphabetically by name. For repeatable pagination, include a unique column such as a primary key in the sort order.
Return a Limited Number of Rows with TOP
SQL Server’s TOP clause limits the number or percentage of rows returned. Combine it with ORDER BY when you need a meaningful top or bottom result.
SELECT TOP (5) ProductName, UnitPrice
FROM dbo.Products
ORDER BY UnitPrice DESC;
Without an ORDER BY clause, SQL Server is free to return any qualifying rows; there is no guaranteed order. You can also use TOP (10) PERCENT to return approximately ten percent of the qualifying rows.
Remove Duplicate Rows with DISTINCT
The DISTINCT keyword removes duplicate combinations from the selected columns.
SELECT DISTINCT DepartmentID
FROM dbo.Employees;
This returns each department identifier once. When DISTINCT is applied to multiple columns, SQL Server removes rows only when the complete selected combination is duplicated. Use it when unique values are required, not as a substitute for understanding why a join produces duplicate rows.
Calculate Values in a SELECT List
A SELECT list can contain arithmetic expressions, string operations, and built-in functions. The following example calculates an annual salary from a monthly salary:
SELECT
EmployeeID,
MonthlySalary,
MonthlySalary * 12 AS AnnualSalary
FROM dbo.Employees;
You can also use functions such as GETDATE(), UPPER(), LOWER(), ROUND(), and COALESCE(). Choose expressions carefully and consider data types, NULL values, and possible divide-by-zero errors.
Group Rows with GROUP BY and HAVING
Aggregate functions summarize values across multiple rows. Common functions include COUNT(), SUM(), AVG(), MIN(), and MAX(). When a SELECT list contains a non-aggregated column alongside an aggregate, that column generally needs to appear in the GROUP BY clause.
SELECT
CategoryID,
COUNT(*) AS ProductCount,
AVG(UnitPrice) AS AveragePrice
FROM dbo.Products
GROUP BY CategoryID;
Use HAVING to filter groups after aggregation. In contrast, WHERE filters individual rows before grouping.
SELECT CategoryID, COUNT(*) AS ProductCount
FROM dbo.Products
GROUP BY CategoryID
HAVING COUNT(*) >= 5;
Retrieve Data from Multiple Tables with JOIN
A SELECT query can combine related records from multiple tables. An INNER JOIN returns matching rows, while a LEFT JOIN keeps every row from the left table and adds matching data from the right table when available.
SELECT
p.ProductName,
c.CategoryName
FROM dbo.Products AS p
INNER JOIN dbo.Categories AS c
ON p.CategoryID = c.CategoryID;
Use clear table aliases and qualify columns with their table aliases when names may be ambiguous. Always check the join condition: an incorrect or missing condition can produce far more rows than expected.
Create a New Table with SELECT INTO
SELECT INTO creates a new table and inserts the query results into it. It can be useful for staging data or creating a quick working copy.
SELECT EmployeeID, FirstName, LastName
INTO dbo.EmployeeArchive
FROM dbo.Employees
WHERE IsActive = 0;
The destination table must not already exist. SELECT INTO copies the selected columns and data, but it does not automatically copy every index, constraint, trigger, or permission from the source table. For a permanent production archive, plan those objects separately.
Paginate Results with OFFSET and FETCH
For page-based results, SQL Server supports OFFSET and FETCH NEXT after ORDER BY. The following query skips the first twenty rows and returns the next ten:
SELECT ProductID, ProductName, UnitPrice
FROM dbo.Products
ORDER BY ProductID
OFFSET 20 ROWS
FETCH NEXT 10 ROWS ONLY;
OFFSET starts at zero, so OFFSET 0 ROWS returns the first page. Use a stable, unique ordering column to reduce inconsistent results when users move between pages. For very large datasets, keyset pagination may perform better than skipping a large number of rows.
Understand the Logical Processing Order
Although SELECT appears at the beginning of a query, SQL Server’s logical processing order is different from the written order. A simplified order is:
FROMand JOIN: identify and combine source rows.WHERE: filter individual rows.GROUP BY: create groups.HAVING: filter groups.SELECT: calculate and return the requested expressions.DISTINCT: remove duplicate result rows.ORDER BY: sort the output.TOPor OFFSET/FETCH: limit the returned rows.
This order explains why a SELECT-list alias generally cannot be referenced in WHERE: WHERE is logically processed before SELECT. Understanding the order makes complex queries easier to write and troubleshoot.