SQL SELECT Statement
The SQL SELECT Statement
The SQL Select statement is used to query a database and return rows of data as a result set.
Here is the general syntax for a select statement:
SELECT column1, column 2, ..., column n
Below is an excerpt of the persons table from the AdventureWorks database. Each person is listed as a row. The fields or attributes, are columns. You’ll see the column names listed as headers.
Let’s now try selecting people from the persons table using the SQL SELECT statement. You can use the box below. Click on Run Query to try it.
-- Answer SELECT FirstName, LastName FROM Person.Person;
Notice there are two parts to the SELECT statement.
- First are the columns. Each column is separated by a comma.
- Second is FROM clause. Here we specify which table to query.
Now that we know this. Using the example below, lets extend our query to include the MiddleName.
The query is partially written, now it’s up to you to complete it.
-- Answer SELECT FirstName, LastName, MiddleName FROM Person.Person;
SQL SELECT *
Rather than listing each column in your select statement, as a short cut, you can use * to let SQL Server know you want to include every column in your result.
Here we return all columns from Person.
/* Answer */
Though it is a great short cut, I don’t recommend using *. Best practice is to list the column in your SQL SELECT statement.
If you’re looking to learn more about the SELECT command, the I would recommend you read our article Simple Select Queries.