SQL Server: Tips and Tricks - 2 (2017)

How to quickly write Select Query in SQL Server

1) Select all the columns from a table/ View by using *


You can always use Select * from SchemaName.TableName to display data for all the columns from a table or view. When you use * in Select query, it return you all the columns from table or view.

Let's say that if I want to select all the columns from dbo.TotalSale table, I can use below query

https://3.bp.blogspot.com/-ogFLSTjzY6Y/V1GrPVBvOjI/AAAAAAAAfBI/Z58-OhK0Q1gKV7B49hsd5h3SEIwJcYFVACLcB/s400/Capture.PNG

2) Drag the Columns Tab to new query

If you would like to get all the column names for your select query, you will open new query window. Then type Select and Drag the Columns Tab under the table to query window. All the columns will be displayed. If you would like to remove few, you can remove.

https://4.bp.blogspot.com/-5aNH9nCghP0/V1G6L9yOU4I/AAAAAAAAfBY/hPt9x7jS2X0WzMBCJ_siA0aO_1-Ob_I7QCLcB/s640/Untitled1

After dragging the Columns Tab, you will see the column names are added to select.

https://2.bp.blogspot.com/-dcn8bF9lkDg/V1G6WLmTqhI/AAAAAAAAfBg/Htb5T4HuGns9qBwpJpx5m8DM88QLVPgcwCLcB/s640/Capture2.PNG

3) Select Top X Row

Third and easy way to write your select query is, Right click on Table name and then choose Select Top X Rows ( X can be different as per your SSMS option settings). Once you choose it will generate the select query with all the column. You can remove the top x part from select and modify as required.

https://2.bp.blogspot.com/-tmwT2PWSa6Q/V1G6yzmBV3I/AAAAAAAAfBo/eMzfECFEnHIIN-c7zb6lCg1FHESUEorvACLcB/s400/Untitled