T-SQL SELECT Examples

Examples of using the T-SQL SELECT statement to return data from a database.

Select All Data

This example selects all data from the Artists table.

Return only Certain Columns

This example explicitly specifies which columns to return data from.

Narrow the Criteria

This example uses the WHERE clause to narrow the results to only specific rows that match a certain criteria (in this case, where the ArtistId is equal to 1).

Join another Table

Here we join results from multiple tables that share data. In this case, both tables have an ArtistId column. The Albums.ArtistId column is a foreign key of the Artists.ArtistId column (which is the primary key for that table).

Add a Table Alias

This example uses the same query as the previous example, except that this example uses table aliases in order to make the code shorter and more concise. Aliases are optional — you can choose whether to use them or simply write the full table name.

Add Column Aliases

You can also add aliases for each column when using the SELECT statement.

In this example, the column header (in the results pane) will read Album instead of AlbumName.

Use SELECT ... INTO to Copy Data to a New Table

You can use SELECT ... INTO to copy data into a newly created table. That is, a new table is created to hold the data.

It goes like this:

The Artists_Archive table didn't previously exist. Running this statement creates it, and inserts data from the Artists table.

For more examples for copying data to another table, see INSERT Examples.