CRUD Operations of Data in MS SQL Server

Last updated on Dec 20 2021
Amit Warghade

Table of Contents

CRUD Operations of Data in MS SQL Server

SQL Server Insert Data

In SQL Server database, INSERT statement is employed to insert one record or multiple records into a table.

Syntax:

  1. INSERT INTO [database_name].[dbo].[table_name]
    (column1, column2, ... )
    VALUES
    (expression1, expression2, ... ),
    (expression1, expression2, ... ),
    ...;

Example:

Insert some records in “STUDENT” table:

  1. INSERT INTO [tecklearn].[dbo].[STUDENT]
    ([ID],[NAME]
    ,[AGE],[SALARY])
    VALUES(1,'Ajeet',27,20000),
    (2,'Backon',29,28000),
    (3,'Chris',17,25000);

You can directly use the above command during a New Query or follow the management studio screenshots given below:

Page 2 Image 1 5
New Query
Page 3 Image 2 6
view

View Inserted Data:

Page 4 Image 3 5
select

Output:

Page 5 Image 4 4
ID

You can see that each one three records are inserted now within the “Student” table.

 

SQL Server Update Data

In SQL Server database, UPDATE statement is employed to update or modify the already inserted records into a table.

Syntax:

  1. UPDATE [tecklearn].[dbo].[STUDENT]
    SET [ID] =
    ,[NAME] =
    ,[AGE] =
    ,[SALARY] =
    WHERE GO

Use the SQL Server management Studio:

Follow the management studio screenshots given below:

Page 6 Image 5 4
update to

After clicking on “New Query Editor Window”, you’ll see the subsequent query page.

Page 7 Image 6
subsequent query

Use the subsequent command:

  1. UPDATE [tecklearn].[dbo].[STUDENT]
    SET [ID] = 4,[NAME] = 'Malvika',[AGE] = 18,[SALARY] = 22000
    WHERE ID =2;
    GO
Page 8 Image 7
subsequent command

You can see that 1 row is affected. you’ll verify it:

Page 9 Image 8
affected

Output:

Page 10 Image 9
updated records

You can see that the above records are now updated.

SQL Server DELETE Data

In SQL Server database, DELETE statement is employed to delete records from the table.

Page 11 Image 10
DELETE

Syntax:

  1. SELECT TOP 1000 [ID]
    ,[NAME],[AGE],[SALARY]
    FROM [tecklearn].[dbo].[STUDENT]

Use the SQL Server management Studio:

Follow the management studio screenshots given below:

Page 12 Image 11
follow

Use the subsequent command:

  1. DELETE FROM [tecklearn].[dbo].[STUDENT]
    WHERE ID =3;
Page 13 Image 12
delete from

Here one row is deleted where ID=3. you’ll verify it:

Output:

Page 14 Image 13
deleted

SQL Server DELETE Top Statement

In SQL Server, DELETE TOP statement is employed to delete the records from a table and limit the amount of records deleted regarding a hard and fast value or percentage.

Syntax:

  1. DELETE TOP (top_value) [ PERCENT ]
    FROM [database_name].[dbo].[table_name]
    [WHERE conditions];

Parameter explanation

table: It specifies a table that you simply want to delete records from.

WHERE conditions: it’s optional. The conditions that has got to be met for the records to be deleted.

TOP (top_value): it’s wont to delete the highest number of rows within the result set supported top_value. for instance , TOP(10) would delete the highest 10 rows matching the delete criteria.

PERCENT: it’s optional. it’s wont to delete the share of of top rows. for instance , TOP(10) PERCENT would delete the highest 10% of the records matching the delete criteria.

DELETE using TOP keyword

TOP keyword is employed with DELETE statement to delete the required top rows of a table.

Example:

We have a table “Employees”, having the subsequent data.

Page 15 Image 14
Employee

Delete top 2 rows from the table “Employees” where salary is bigger than or adequate to 20000.

Syntax:

  1. DELETE TOP(2)
    FROM [tecklearn].[dbo].[Employees]
    WHERE salary >= 20000;
Page 16 Image 15
bigger salary

You can see that 2 rows are affected. Now see the output:

Output:

Page 17 Image 16
deleted

DELETE using TOP Percent Keyword

TOP percent keyword is employed to delete the rows consistent with the share .

Example:

Delete TOP 25% rows from the table where salary is bigger than 20000.

  1. DELETE TOP(25) PERCENT
    FROM [tecklearn].[dbo].[Employees]
    WHERE salary > 20000;
Page 18 Image 17
top percent

You can see that 3 rows are affected. 3 is strictly 25% of 12 which is that the number of inserted rows.

Output:

Page 19 Image 18
finish

So, this brings us to the end of blog. This Tecklearn ‘CRUD Operations of Data in MS SQL Server’ blog helps you with commonly asked questions if you are looking out for a job in MS-SQL Server. If you wish to learn MS-SQL Server and build a career in Database domain, then check out our interactive, MS-SQL Server Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/ms-sql-server-training-and-certification-course/

MS-SQL Server Training

About the Course

Microsoft SQL Server training course is an online classroom training meant for developers to master the descriptive language to work for relational databases. Important topics included in this SQL course are SQL introduction, relational databases, queries, subqueries, joins, and unions. After completing this course, you will also become proficient in the theory of optimization, transactions, indexing, installing SQL Server, database administration, clustering, implementing programmability objects, designing and implementing database objects, managing database concurrency, and optimizing SQL infrastructure and database objects.

Why Should you take MS-SQL Server Training?

  • The average salary for Sql server developer ranges from approximately $88,666 per year for Database Developer to $115,811 per year for Senior SQL Developer. – Indeed.com
  • Wells Fargo, UPS, US Bank, Perspecta, Silicon Valley Bank, Mindtree Wipro, Infosys & many other MNC’s worldwide use MS SQL Server for their Database deployments.
  • According to Gartner, MS SQL Server is the market leader in the Relational Database with 18.5% market share globally.

What you will Learn in this Course?

Introduction to SQL

  • Various types of databases
  • Introduction to SQL
  • Installation Steps of MS-SQL Server
  • Overview of Microsoft SQL Server Management Studio
  • SQL architecture, client/server relation
  • Database types

Introduction to relational databases, basic concepts of relational tables

  • Working with rows and columns
  • Various operators used like logical and relational
  • Constraints, Primary Key and Foreign Key
  • Insert, Update, Delete, Alter

Working with SQL: Join, Tables, and Variables

  • SQL Operators and Queries
  • SQL functions
  • Creation of Table
  • Retrieval of Data from tables
  • Combining rows from tables using joins
  • Operators such as intersect, except, union, temporary table creation, set operator rules, table variables etc

Deep Dive into SQL Functions

  • Data Retrieval Language
  • Functions
  • Operators
  • Clauses
  • Sub-Queries
  • Correlated Sub-Queries

SQL Views, Functions

  • Sub-queries
  • Views
  • Indexes

Managing Data with Transact-SQL

  • Transact-SQL queries
  • Implementing functions and aggregating data
  • Determining the results of DDL statements on supplied tables and data

Cursor Management and Triggers

  • Cursors
  • Triggers
  • Types of Triggers

Stored Procedures and User Defined functions

  • Understanding of Stored Procedures
  • Key benefits of Stored Procedures
  • Studying user-defined functions

Deep Dive into User-defined Functions

  • Detailed study of user-defined functions
  • Various types of UDFs like Scalar, Inline Table Value, multi-statement Table
  • What is Rank Function? Triggers, when to execute Triggers?

SQL Optimization and Performance

  • Concepts of Concurrency and Locking behaviour
  • Usage of memory-optimized tables to solve issues
  • Examining and Troubleshooting of query plans
  • Performance management of Database instances
  • SQL server Performance Monitoring

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "CRUD Operations of Data in MS SQL Server"

Leave a Message

Your email address will not be published. Required fields are marked *