The SQL SELECT statement is used to retrieve data from the database tables. In this article, we will show how to write a SELECT in SQL Server with an example. The syntax of the SQL Server SELECT statement is:
-- SQL Server SELECT Statement Syntax SELECT [Column Names] FROM Source
Columns: Select the number of columns from the Sql tables. It may be One or more.
Points To Remember in the select statement
- The SELECT Statement returns all the records present in the table. If you want to restrict the Rows, then please use the WHERE Clause or TOP Clause.
- Please avoid using SELECT * for selecting all the column names.
- Use Joins to SELECT from Multiple Tables from a database.
We use this data to explain the SQL Server Select Statement.
SQL Select All Columns Example
In this SQL Server select statement example, We select all the Columns present in the customer’s table using an asterisk (*). In SELECT statement * is the shortcut to represent all the available columns in the source tables.
-- SQL Server SELECT Statement Example USE [SQL Server Tutorials] GO SELECT * FROM [Customer]
Problems with SELECT * FROM
- The SQL Select * from statement will retrieve all the columns from the Table. Most of the time, we do not require all the columns in a table.
- The select * from will select the columns in the default order. Sometimes we may need to change the order of the columns while displaying them.
- If there are any changes made in the underlying table (Reordering Columns, Removing or Adding Columns) will not reflect the Views created using SELECT *.
Please don’t use SQL SELECT * FROM statement in real-time even to retrieve all the columns present in a table. The second approach, Placing all the column names after the SELECT keyword. For instance, the above select statement is the same as:
-- SQL Server SELECT Statement Example USE [SQL Server Tutorials] GO SELECT [FirstName] ,[LastName] ,[YearlyIncome] ,[Education] ,[Occupation] FROM [Customer]
SELECT few Columns
In real-time, selecting all the columns in a table is not the case. So, you can restrict the columns in SQL Server select statement. Because there will be some columns such as Barcodes, rowguid, Photos, etc., which may not require in the select. For this, the Select statement allows us to select few or any specific columns from the table or tables by placing the required Column Names followed by a SELECT keyword.
-- SQL Server SELECT Statement Example USE [SQL Server Tutorials] GO SELECT [FirstName] ,[LastName] ,[Occupation] ,[YearlyIncome] FROM [Customer]
SELECT Statement in Management Studio
Within the Management Studio, Navigate to the required Database and then select the required Table. Right Click on the Table will open the Context Menu with different options
From the above context menu, We have to select the Select Top 1000 Rows Option. This option generates the SQL Server SELECT Statement for us. To see all the records, remove the TOP Clause (Top 1000) from the above statement.