The SELECT statement is used to retrieve the useful data from one or more tables in a database. The basic use of SELECT statement is as follows:
SELECT * FROM TableName –To select all columns
SELECT col1, col2,colN FROM TableName –To select specific columns
Refer below query to select specific columns from CreditCard table,
SELECT [CreditCardID] ,[CardType] ,[CardNumber] ,[ExpMonth] ,[ExpYear] ,[ModifiedDate] FROM [Sales].[CreditCard];
Refer Figure-1, to select specific columns from CreditCard table.
The asterisk (*) is a shortcut to SELECT all columns in a table. Here we are selecting all columns from CreditCard table.
SELECT * FROM [Sales].[CreditCard];
Refer Figure-2, to select all columns from CreditCard table:
Note on using SELECT (*)
Using (*) is considered a bad practices for several reasons:
- Often you do not need all the columns, but with the use of (*) you display them all, which will increase load on database server and impact the performance.
- If you make a change to a table, adding or removing columns, this change does not reflect the views created with the use of (*).
- You can not change the order of selected columns.
Select Specific Columns
In normal case, you do not really need all the columns for that, we can display only the columns you need, in this example we will show only two columns:
SELECT [CreditCardID ,[CardType] FROM [Sales].[CreditCard];
Refer Figure-3, to select two columns from CreditCard table:
Use SQL Server Management Studio to generate SELECT Statement.
Navigate to database and then select the Table. Right click on the table will open the Context Menu with different options. Refer below path.
(Right click on table –> Script Table as –> SELECT To –> New Query Editor Window)
Now you can see select query in query editor window as follows.
Refer Figure-4 to after generating scripts from SQL Server Management Studio (SSMS).