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.

The following are the sections of this article,

  • A glimpse of previous articles
  • Insert Statement
  • Select Statement
  • Points to remember
  • Conclusion

A glimpse of the 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 and 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 the mentioned table.

Syntax

INSERT INTO TABLENAME (column1, column2, ..., columnN)
VALUES (value1, value2, ..., valueN)

The number of Table columns and a number of Table values count should match, otherwise, it will throw an expectation.

Example

INSERT INTO Students.StudentDetails (StudentId, FirstName, LastName)
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, and 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,

INSERT INTO Students.StudentDetails (StudentId, FirstName, LastName)
VALUES
    (2, 'Saravana', 'Kumar'),
    (3, 'Karthik', 'Kanagaraj'),
    (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

SELECT *
FROM TABLENAME

Example

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 the * (asterix) symbol and SELECT, FROM are the keywords to select values from a table.

Statement
Fig1. 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

SELECT Column1, Column2, .... ColumnN
FROM tablename

Example

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 Columns 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 the * (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.


Similar Articles