w3resource

How to Update a Single Record in SQL Based on a Condition?


Update a Single Record

Write a SQL query to update a single record in a table based on a specific condition.

Solution:

-- Update the salary of an employee with EmployeeID = 1.
UPDATE Employees -- Specify the table to update.
SET Salary = 55000 -- Set the new value for the "Salary" column.
WHERE EmployeeID = 1; -- Apply the update only to the employee with ID 1.

Explanation:

    1. Purpose of the Query :

    1. The goal is to update the Salary of a specific employee in the Employees table.
    2. This demonstrates how to use the UPDATE statement to modify existing data in a table based on a condition.

    2. Key Components :

    1. UPDATE Employees : Specifies the table where the update will occur.
    2. SET Salary = 55000 : Defines the new value for the Salary column.
    3. WHERE EmployeeID = 1 : Ensures that the update applies only to the employee with EmployeeID = 1.

    3. Why use UPDATE? :

    1. The UPDATE statement is used to modify existing records in a table, making it essential for maintaining accurate and up-to-date data.
    2. For example, if an employee receives a raise, their salary can be updated in the database using this statement.

    4. Real-World Application :

    1. For example, in a company database, you might use this query to update the salary of an employee with EmployeeID = 1 to reflect a recent promotion or adjustment.

Additional Notes:

  • WHERE clause is critical to ensure that updates are applied only to the intended rows.
  • Scenarios where updating a single record is appropriate, such as:
    • Adjusting an employee's salary after a promotion or raise.
    • Correcting erroneous data in a specific record.
  • Important Considerations :
    • Always include a WHERE clause to avoid unintentionally updating all rows in the table.
    • Verify that the condition in the WHERE clause uniquely identifies the target record.

For more Practice: Solve these Related Problems:

  • Write a SQL query to update a single record in the 'employees' table, setting a column's value based on a subquery that retrieves data from the 'salaries' table.
  • Write a SQL query to update a single record using a CASE expression to conditionally modify a column's value.
  • Write a SQL query to update a single record where the WHERE clause involves multiple conditions combined with AND/OR operators.
  • Write a SQL query to update a single record in one table by joining it with another table to determine the new value.
  • Write a SQL query to update a single record by incrementing a numeric column based on its current value using an arithmetic operation.

Go to:


PREV : Insert Multiple Records
NEXT : Update Multiple Records.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.