How to Write SQL Queries

SQL queries are used to fetch data from the database and we can provide various functions, operators and clauses for viewing and manipulating the data. Writing SQL queries is very easy; all we have to do is follow a step-by-step approach to fetch data from the table or multiple tables.
In this tutorial we will learn how to write SQL Queries.

Step 1 – Database Server
First of all, we will have to connect to the Database Server. In this tutorial we will use our own PC as the server, and therefore will specify “local” in the server name field. For the authentication, we will choose the “Windows Authentication” option and click on Connect below.

Connecting to Database Engine

Step 2 – Navigating Database
With that done, the Microsoft SQL Server Management Studio will open up, displaying the databases on the left side of the screen, under the Object Explorer.
Before writing SQL queries, we have to open up the query editor of that particular database for which queries have to be written. This is because if we have multiple Databases, this will ensure that the query runs only on that particular database.
To open up a query editor, simply right click on the database and select the “New Query” option from the menu.
Opening Query Editor

Step 3 – Writing Query
With that done, the Query Editor Window will open up.
Usually SQL queries start with the “Select” keyword which is combined with different operators.
So let us start with a very basic query which will display the entire records from the employee table.
The structure of our query would be “Select”, followed by ‘*’, the asterisk sign. This sign is used to fetch all the fields from the selected table.
After that comes the name of the table. Over here, the name of the employee table is “EMP”.
Actual query would be like:
Select * from EMP
Writing Query

Step 4 – Execution
Now we have to execute it by clicking on the Execute button located on top. We can even press F5 on our keyboard for the same action.
After that, our query would be executed and we can see that the query is executed successfully. The results are shown below the query editor.
Results of the Query

And that is how to write SQL queries.