Zero To Hero In MS SQL Server - Part Five

Introduction

This is part five of "Zero To Hero In MS SQL Server", and in this article, you will learn about Insert Statement and Select Statement in detail. To read the previous articles under this series, please follow the below-mentioned links,
  • A glimpse of previous articles
  • Insert Statement 
  • Select Statement 
  • Points to remember 
  • Conclusion
A glimpse of previous article series -

In Part Three,
  • In this article, we learned about creating a Schema and Table 
  • The best practice method is to use schema before the table.
  • We learned about creating and dropping a Schema.
  • In part three, we also learned about Creating, Updating SQL tables.
In Part Four,
  • In this article, we learned about Dropping, Deleting and Truncating a SQL Table. 
To learn more details of the above-mentioned SQL concepts, please go through the links which are given above.

Insert Statement

In the earlier series of this article, we have learned about creating and modifying the table. So we have a table now and we need  data inside right? So to insert data into the table, we use INSERT SQL Statement. The INSERT Statement will insert data into a mentioned table.

Syntax
  1. INSERT INTO TABLENAME (column1,column2....columnN)  
  2. VALUES (value1,value2,....valueN)  
The number of Table columns number of Table values count should match, otherwise it will throw an expection. 
 
Example
  1. INSERT INTO Students.StudentDetails (StudentId,FirstName,LastName)  
  2. VALUES (1,'Sundaram','Subramanian')  
In the above-given example, Students is the Schema Name and StudentDetails is the Table Name. The columns are StudentId, FirstName, LastName are the columns in the table. INSERT INTO is the keyword to Insert Values into the table. When this SQL Statement is executed, one new row will be inserted into the table. 
 
We can insert more than one row in a single Insert SQL Statement, 
  1. INSERT INTO Students.StudentDetails (StudentId,FirstName,LastName)  
  2. VALUES (2,'Saravana','Kumar'),  
  3. (3,'Karthik','kanagaraj'),  
  4. (4,'Pushparaj','James')  

When the above SQL Statement is executed,  three new rows will be inserted. Thus we can insert multiple rows in a single SQL Insert Statement.

Select Statement 
 
We have inserted values into the created table. But we need to retrieve the values to view, right? For that, we use SELECT SQL Statement.   
 
Syntax
  1. SELECT * FROM TABLENAME  
Example
  1. SELECT * FROM Students.StudentDetails  

When the above statement is executed, we will get all the data values from the table. To select all the values from the table, we have to use * (asterix) symbol and SELECT, FROM are the keywords to select values from a table. 

 
Fig.1 Selected all the Data from the Table. 
 
We can also specify columns in a Select statement and the data of only those columns will be selected. 
 
Syntax 
  1. SELECT Column1,Column2,....ColumnN FROM tablename  
Example
  1. SELECT FirstName,LastName FROM Students.StudentDetails  
In the above-given example, FirstName and LastName are the columns of the Table StudentDetails. We need values only from those two columns so these columns are specified in the SELECT Statement. 
 
 
Fig.2 Selected Data based on Specified Column in a Table 
 
Points To Remember
  • In Insert Statement, the column count should match the value count or an exception will be raised. 
  • To Select all the Column Values in a table we have to use * (asterisk) symbol. 
  • To Select particular column values, we need to specify the column name in the table. 
Conclusion
 
In this article, we learned about INSERT and SELECT statements in SQL in detail. I hope it was useful. We will learn more about SQL concepts in upcoming articles. Please share your suggestions and knowledge in the comment section.