By: Jeremy Kadlec
At the most basic level, a single column can be accessed from a SELECT statement. In this circumstance all of the values from the column will be returned from the table. From there you can expand into additional capabilities with the SELECT statement. Let's take a look at some examples.
Let's start off by referencing the main components of a SELECT statement:
SELECT -- Column names FROM -- Table or View name WHERE -- Filter criteria GROUP BY -- Logic to roll-up records HAVING -- Criteria for GROUP BY logic ORDER BY – Sort data either ASC (ascending) or DESC (descending) by column name or position
SQL Server SELECT with Single Column
-- Select first name from dbo.Authors table SELECT FirstName FROM dbo.Authors
SQL Server SELECT with Single Column and WHERE Clause
-- Select first name of authors as Jeremy from dbo.Authors table SELECT FirstName FROM dbo.Authors WHERE FirstName = 'Jeremy'
SQL Server SELECT with Single Column, WHERE and ORDER BY Clause
-- Select first name of active authors and return results in ascending order -- from dbo.Authors table SELECT FirstName FROM dbo.Authors WHERE ActiveFlag = 1 ORDER BY FirstName ASC -- or DESC for descending results
SQL Server SELECT with Data Column and Count, WHERE, GROUP BY and ORDER BY Clause
-- Select first name with the associated count of active authors which also includes -- grouping the data by first name to roll-up the data and return results in -- descending order from dbo.Authors table SELECT FirstName, COUNT(*) FROM dbo.Authors WHERE ActiveFlag = 1 GROUP BY FirstName ORDER BY FirstName DESC
This is just the tip of the iceberg, but a good first step to see how to use the SELECT statement in your applications and reporting.