The SQL SELECT statement is used to retrieve data from the database tables. In this article, we will show you, How to write a SELECT statement in SQL Server with an example.
SQL SELECT Statement Syntax
The SELECT Statement in SQL Server can be written as:
-- SQL Server SELECT Statement Syntax SELECT [Column Names] FROM Source
- Columns: It allows us to choose the number of columns from the tables. It may be One or more.
- Source: One or more tables present in the Database. JOINS are used to join multiple tables.
We are going to use the below-shown data to explain the SELECT Statement in SQL Server with an example.
SQL SELECT Statement – Selecting All Columns
In this example, We are going to 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 *
- It will retrieve all the columns from the Table. Most of the times we don’t require all the column in a table.
- It will select the columns in default order. Sometimes we may need to change the columns order 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 *.
NOTE: Please don’t use SELECT * FROM statement in real-time even if you want 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 statement is written as:
-- SQL Server SELECT Statement Example USE [SQL Server Tutorials] GO SELECT [FirstName] ,[LastName] ,[YearlyIncome] ,[Education] ,[Occupation] FROM [Customer]
SQL SELECT Statement – Selecting Few Columns
In real-time, selecting all the columns in a table is not the case because there will be some columns such as Barcodes, rowguid, Photos etc. which are not required in the select. For this, 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]
NOTE: If you forget the comma between the columns then SQL Server assumes the second column as the Alias Column for the first column. So, Please be careful
SQL SELECT Statement – Management Studio
If you can access the Management Studio then you can use the Intellisense to write the SELECT Statement for us. To do so First, 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 will generate the SELECT Statement for us.
NOTE: If you want to see, all the records then remove TOP Clause (Top 1000) from the above statement.
Points To Remember
- The SQL SELECT Statement return all the records present in the table. If you want to restrict the Rows then please use WHERE Clause or TOP Clause.
- Please avoid using SELECT * for selecting all the columns names.
- Use Joins to SELECT from Multiple Tables.
Thank You for Visiting Our Blog