The SQL Select statement queries a database to return a result as rows of data. In this article, we will focus our lesson on returning selected columns from a database table.
Here is the general syntax for a SELECT statement:
SELECT column1, column2, column3, ... FROM table
In its simplest form this idea is you can use the SELECT statement to return one or more columns from a database table.
Below is an excerpt from the person table from the AdventureWorks database. The rows represent people, and columns, items that describe them, such as their names.
SELECT ONE or More Columns
We now try selecting people from the table under the person field using the SQL SELECT statement. Use the box below and click on Run Query to try it.
SELECT FirstName, LastName FROM Person.Person
Do you notice something on the SELECT statement? There are two parts to the SELECT statement:
- First part: Columns separated by commas.
- Second part: A FROM clause. This is where we specify which table to query.
Now that we know this, using the sample below, let us extend our query to include the MiddleName.
Can you complete this query?
SELECT FirstName, LastName FROM Person.Person;
SQL SELECT *
Use the * to let the SQL Server know what you want to include in every column in your result rather than listing each column in your select statement.
In particular, we return all columns from a single “Person” field.
/* Answer */
This might seem like a very nice short cut; however, I don’t recommend using *. The best practice is to list the column in your SQL SELECT statement.
You can learn more about the SELECT command in this article – Simple Select Queries. I would suggest that you read first and familiarize yourself with the SELECT command before we delve into this topic here.