Menu

How to insert into a table or update if exists in MySQL?

Learn how to insert into a table or update if exists in MySQL and SQL Server. You need to calculate a running total in SQL Server and provide a solution that is also reproducible in MySQL.

Written by Selva Prabhakaran | 2 min read

Problem

You need to calculate a running total in SQL Server and provide a solution that is also reproducible in MySQL.

Input

employee_idfirst_namelast_namesalary
1JohnDoe50000.00
2JaneSmith60000.00
3BobJohnson55000.00

Try Hands-On: Fiddle

Desired Output

On inserting a new row, if the entry already exists, it should update the existing entry.

For example, we want to update the employee_id = 1’s salary to 55000.

employee_idfirst_namelast_namesalary
1JohnDoe55000.00
2JaneSmith60000.00
3BobJohnson55000.00

Solution 1:

You can use the INSERT INTO … ON DUPLICATE KEY UPDATE statement in MySQL to achieve this.

This statement will insert a new row into the table, or if a duplicate key violation occurs (i.e., a row with the same primary or unique key exists), it will update the existing row with the new values. Here’s the SQL query:

sql
INSERT INTO employees (employee_id, first_name, last_name, salary)
VALUES
    (1, 'John', 'Doe', 55000.00)
ON DUPLICATE KEY UPDATE
    first_name = VALUES(first_name),
    last_name = VALUES(last_name),
    salary = VALUES(salary);

Explanation:

In this example, we’re trying to insert a row with employee_id 1, which already exists in the table.

Instead of inserting a new row, this query will update the existing row with the new salary value (55000.00).

You can execute this query to insert or update data into the employees table as needed.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to select only rows with max value on a column?
  2. How to transpose columns to rows in SQL?
  3. How to select first row in each GROUP BY group?
  4. How to concatenate text from multiple rows into a single text string in MySQL?
  5. How to get the rows which have the max value for a column for each group in another column in SQL?
  6. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science