How to Update Top 100 Records in SQL Server
Last Updated :
05 Feb, 2024
SQL Server is a Relational database Management system which is developed by Microsoft and is one of the most used databases in the world. It provides the developer with a set of rich functionalities to create tables, insert data in them, and then manipulate and play with them as and when necessary.
In this article, we will be looking at how one can update the top 100 records and understand the various method along with he examples and so on.
Introduction of Update in SQL Server
In SQL Server, the UPDATE
statement is used to modify existing records in a table. It allows us to change the values of one or more columns in a specified set of rows based on a specified condition.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Explanation:
table_name
: The name of the table we want to update.
column1
,
column2
, ...
: The columns we want to update.
value1
,
value2
, ...
: The new values we want to assign to the specified columns.
WHERE
condition
: The condition that specifies which rows to update. If satisfied then all rows in the table will be updated.
Ways to Update Top Records in SQL Server
Let us start by creating a table and adding some sample data to the table. We will create a test table with an id and title field. The following query creates the table:
Query:
CREATE TABLE test(id INTEGER, title VARCHAR(100));
Now that we have the table, let's insert some data into the data. The following query inserts ten records in the test table:
Query:
INSERT INTO test (id, title)
VALUES
(1, 'Title 1'),
(2, 'Title 2'),
(3, 'Title 3'),
(4, 'Title 4'),
(5, 'Title 5'),
(6, 'Title 6'),
(7, 'Title 7'),
(8, 'Title 8'),
(9, 'Title 9'),
(10, 'Title 10');
Output:
Output
Explanation: Now that we have the setup in place, lets now go forward to see how we can update the top 100 records of the table. We are going to have a look in this article at two methods to update the top 100 records of the table.
Method 1: Using TOP clause
The TOP clause is used to restrict the number of records returned by the query. Using this in conjunction with the UPDATE clause we can restrict the update to the first n records only.
The following code updates the top 3 records of the table.
Query:
update top (3) test set id=-10;
Output:
Output
Explanation: As we can see in the above image the id of the top 3 records in the table has been updated to -10. The parenthesis surrounding the number is necessary.
Method 2: Using Common Table Expression (CTE)
We can make use of Common Table Expression (CTE) to select only the top n records from the table and later update them using the UPDATE clause. However, to make this work, we need to provide an ordering using the ORDER BY clause for the SQL engine to understand what records to return.
The following query first creates a CTE of the top 3 records ordered by the id field. The update statement uses the CTE to update the top 3 records as above:
Query:
WITH CTE AS
(
SELECT TOP 3 *
FROM test
ORDER BY id
)
UPDATE CTE SET id=-10;
Output:
Output
Explanation: As we can see in the above image the id of the top 3 records in the table has been updated to -10.
Example of Update top 100 records in SQL Server
Let's now use the concepts we have learned in this article in a technical example.
First let's create the table and insert some data inside it. The following query creates an employee table and inserts nine records in it.
Query:
-- create
CREATE TABLE EMPLOYEE (
empId int,
name varchar(100),
dept varchar(50)
);
-- insert
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (100, 'Clark', 'Engineering');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (5, 'Jill', 'Sales');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (6, 'Ava', 'Marketing');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (2, 'Dave', 'Accounting');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (71, 'Tom', 'Sales');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (4, 'Jake', 'Sales');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (8, 'Ben', 'Marketing');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (9, 'Alice', 'Engineering');
INSERT INTO EMPLOYEE(empId,name,dept) VALUES (11, 'Mike', 'Marketing');
The above query creates an employee table with three fields and inserts the data for nine employees. The following query returns the initial data in the table:
SELECT * FROM EMPLOYEE;
Output:
Output
Explanation: Now lets try to update the department of the top 3 employees ordered by employee id to 'Analytics'. For this we can make use of CTE as we did in method 2 to update the records.
The following query creates a CTE select the top 3 records in order of their employee id and then the UPDATE statement makes use of the CTE to update the record's department to 'Analytics':
WITH CTE AS
(
SELECT TOP 3 *
FROM EMPLOYEE
ORDER BY empId
)
UPDATE CTE SET dept='Analytics';
Now let's see the current data in the table. I will use the following query to get the records ordered by the employee id:
SELECT * FROM EMPLOYEE
ORDER BY empId;
Output:
Output
Explanation: As we can see in the below image the department of Dave, Jake and Jill has been updated to Analytics from Accounting, Sales and Sales respectively.
Conclusion
In this article we covered how we can update the top n records of the table in SQL Server. We had a chance to look at two different methods to go about doing this, first using the TOP clause and the other using CTE. We also how we can use the concepts we learned in this article to a real-life situation through the technical example.
Similar Reads
How to Update Top 100 Records in SQL?
As our systems get more complex and complex, there is often a need to update the underlying data to accommodate the evolution of the system. SQL provides a variety of ways to update the data so that the system developer can manipulate the data in whatever way necessary. In this article, we will be l
5 min read
How to Update Top 100 Records in PL/SQL?
In terms of database management, the ability to update specific subsets of data is crucial for maintaining system integrity and meeting user needs. In this article, we will understand two primary methods for updating top records. Using the ROWNUM function and Using the ORDER BY clause. Each method i
4 min read
How to Update Top 100 Records in PostgreSQL?
PostgreSQL is one of the most advanced general-purpose object-relational database management systems and is open-source. Being an open-source software, its source code is available under the open-sourcePostgreSQL license, a liberal open-source license. Anyone with the right skills is free to use, mo
5 min read
How to Perform Batch Updates in SQL Server
The Update statement is a SQL keyword to update data in the database. We can update all the rows in the database or some values with the help of conditions. The update is a SQL keyword, and it falls under Data Manipulation Language (DML), as the name suggests it is used to manipulate the data. Updat
5 min read
How to Update Top N Records in MySQL
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides a variety of rich features to create databases and tables, insert data in them, and further manipul
6 min read
How to Get Latest Updated Records in SQL?
SQL is a flexible and widely used relational database management system in the software industry. Retrieving the latest updated records is a critical operation for data analysis, debugging, or monitoring changes in a database. In this article, we will explain various SQL techniques to get the latest
4 min read
How to Update a Column in a Table in SQL Server
In the database management area, updating columns in a table is a normal query and it is important software that ensures the accuracy and integrity of data. Whether we are making spelling corrections, editing or altering existing information, or being attentive to changing requirements, carrying out
4 min read
How to Update All Rows in SQL?
Updating records in an SQL database is a fundamental operation used to modify existing data. The UPDATE command is the go-to method for making such modifications, whether for all rows in a table or a subset based on specific conditions. In this article, we will explain how to update all rows in SQL
4 min read
How to Update Table Rows in SQL Server using Subquery ?
Updating table rows in SQL Server using a subquery is a common operation that allows us to modify records based on values derived from another table or query. In this article, we will explain how to update table rows in SQL Server using subquery with the help of examples. Updating Table Rows Using a
3 min read
How to UPDATE and REPLACE Part of a String in SQL Server
In SQLServer, efficient manipulation of strings is crucial for managing databases effectively. Among the fundamental operations are updating and replacing parts of strings within tables. These operations are invaluable for correcting data inconsistencies, enhancing data quality, and transforming tex
4 min read