Tutorials Logic, IN info@tutorialslogic.com

MySQL INSERT Add Rows: Tutorial, Examples, FAQs & Interview Tips

MySQL INSERT Add Rows

MySQL in MySQL is best learned by connecting the rule to an order-management database. Start with the smallest SQL statement, observe the output, and then add one realistic constraint so the concept becomes practical.

The key habit for this lesson is to watch table rows, constraints, and indexes as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.

INSERT INTO ... VALUES

The INSERT INTO statement adds new rows to a table. You can specify the column list explicitly (recommended) or rely on column order. Always list columns explicitly to make your code resilient to future schema changes.

INSERT Single and Multiple Rows

INSERT Single and Multiple Rows
-- Insert a single row (explicit column list)
INSERT INTO customers (first_name, last_name, email, phone, active)
VALUES ('Alice', 'Johnson', 'alice@example.com', '555-0101', 1);

-- Insert using SET syntax (alternative, column = value pairs)
INSERT INTO customers SET
    first_name = 'Bob',
    last_name  = 'Smith',
    email      = 'bob@example.com',
    active     = 1;

-- Insert multiple rows in a single statement (much faster than multiple INSERTs)
INSERT INTO customers (first_name, last_name, email, active)
VALUES
    ('Carol', 'White',  'carol@example.com',  1),
    ('Dave',  'Brown',  'dave@example.com',   1),
    ('Eve',   'Davis',  'eve@example.com',    0),
    ('Frank', 'Wilson', 'frank@example.com',  1);

-- Get the AUTO_INCREMENT ID of the last inserted row
SELECT LAST_INSERT_ID();

INSERT INTO ... SELECT

You can insert rows from the result of a SELECT query. This is useful for copying data between tables, creating summary tables, or archiving records.

INSERT INTO ... SELECT and INSERT IGNORE

INSERT INTO ... SELECT and INSERT IGNORE
-- Copy active customers to an archive table
INSERT INTO customers_archive (first_name, last_name, email, active)
SELECT first_name, last_name, email, active
FROM customers
WHERE active = 1;

-- INSERT IGNORE: skip rows that would cause a duplicate key error
-- (useful when importing data that may have duplicates)
INSERT IGNORE INTO customers (first_name, last_name, email, active)
VALUES ('Alice', 'Johnson', 'alice@example.com', 1);
-- If alice@example.com already exists (UNIQUE key), this row is silently skipped

-- Insert products into the shop
INSERT INTO products (name, description, price, stock, category)
VALUES
    ('Laptop Pro 15',   'High-performance laptop',  1299.99, 50,  'Electronics'),
    ('Wireless Mouse',  'Ergonomic wireless mouse',   29.99, 200, 'Electronics'),
    ('USB-C Hub',       '7-in-1 USB-C hub',           49.99, 150, 'Electronics'),
    ('Desk Lamp',       'LED adjustable desk lamp',   39.99, 100, 'Office'),
    ('Notebook A5',     'Premium lined notebook',      9.99, 500, 'Stationery');

INSERT ... ON DUPLICATE KEY UPDATE

This statement inserts a row if no duplicate key exists, or updates the existing row if a duplicate is found. It's an efficient "upsert" pattern that avoids a separate SELECT check.

ON DUPLICATE KEY UPDATE (Upsert)

ON DUPLICATE KEY UPDATE (Upsert)
-- Assume email has a UNIQUE constraint on customers
-- If the email already exists, update the name and active status instead
INSERT INTO customers (first_name, last_name, email, active)
VALUES ('Alice', 'Johnson-Updated', 'alice@example.com', 1)
ON DUPLICATE KEY UPDATE
    first_name = VALUES(first_name),
    last_name  = VALUES(last_name),
    active     = VALUES(active);

-- MySQL 8.0+ syntax using aliases (cleaner)
INSERT INTO customers (first_name, last_name, email, active)
VALUES ('Alice', 'Johnson-Updated', 'alice@example.com', 1) AS new_row
ON DUPLICATE KEY UPDATE
    first_name = new_row.first_name,
    last_name  = new_row.last_name,
    active     = new_row.active;

-- Track stock changes: increment stock on duplicate product name
INSERT INTO products (name, price, stock, category)
VALUES ('Wireless Mouse', 29.99, 50, 'Electronics')
ON DUPLICATE KEY UPDATE
    stock = stock + VALUES(stock);

Applied guide for MySQL

Use MySQL when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real MySQL task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.

A reliable practice flow is: create the smallest working SQL statement, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with result set and EXPLAIN output. If the result surprises you, reduce the code until the behavior is visible again.

The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why MySQL is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.

  • Identify the exact problem solved by MySQL.
  • Trace table rows, constraints, and indexes before and after the main operation.
  • Keep one intentionally broken version and explain the fix.
  • Connect the example to an order-management database so the idea feels concrete.
Key Takeaways
  • I can explain where MySQL fits inside an order-management database.
  • I can point to the exact table rows, constraints, and indexes affected by this topic.
  • I tested a normal case and an edge case involving missing, repeated, empty, or boundary input.
  • I verified the result with result set and EXPLAIN output instead of assuming it worked.
  • I can describe the main mistake: copying the syntax before understanding the behavior.
Common Mistakes to Avoid
WRONG Copying the syntax before understanding the behavior.
RIGHT Write the expected behavior first, then make the example prove it.
A one-line expectation turns the code from copied syntax into a testable idea.
WRONG Practicing only the perfect input.
RIGHT Also test missing, repeated, empty, or boundary input before considering the lesson complete.
The edge case is where most interview follow-up questions begin.
WRONG Looking only at the final output.
RIGHT Trace table rows, constraints, and indexes through each important step.
Tracing makes debugging faster because you can see the first incorrect state.

Practice Tasks

  • Build one small SQL statement that demonstrates MySQL in an order-management database.
  • Change the example to include missing, repeated, empty, or boundary input and record the difference.
  • Break the example by deliberately copying the syntax before understanding the behavior, then write the corrected version.
  • Explain the finished example in five bullet points: input, operation, output, failure case, and verification.

Frequently Asked Questions

Use it when the problem matches the behavior shown in the example and when the result can be verified through result set and EXPLAIN output.

Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.

Trace table rows, constraints, and indexes, predict the result, run the example, and compare your prediction with the actual output.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.