SQL Server Sample: Basic SELECT Statement

This sample demonstrates the fundamental SELECT statement in SQL Server, used to retrieve data from a database table.

Description

The SELECT statement is the cornerstone of data retrieval in SQL. It allows you to specify which columns you want to retrieve and from which table(s).

Syntax

The basic syntax for a SELECT statement is:

SELECT column1, column2, ...
FROM table_name;

Alternatively, to select all columns from a table, you can use the asterisk (*) wildcard:

SELECT *
FROM table_name;

Example: Retrieving Specific Columns

Let's assume you have a table named Employees with columns like EmployeeID, FirstName, LastName, and Department. To retrieve only the first name and last name of all employees, you would use the following query:

SELECT FirstName, LastName
FROM Employees;

Sample Output

The output might look like this:

FirstName  LastName
---------  ----------
Alice      Smith
Bob        Johnson
Charlie    Williams

Example: Retrieving All Columns

To retrieve all information for every employee, you would use:

SELECT *
FROM Employees;

Sample Output

This would return all columns for each row in the Employees table.

Note: While SELECT * is convenient for exploration, it is generally recommended to specify the exact columns you need in production environments for better performance and maintainability.

Further Options

The SELECT statement can be extended with clauses such as:

Explore the SQL Samples Index for more advanced queries.