INSERT, SELECT, UPDATE, DELETE: The Four You Will Use Daily
· 3 min read
You now know how to design tables and pick types. Time to actually put data in and get it back out.
You now know how to design tables and pick types. Time to actually put data in and get it back out. These four commands, INSERT, SELECT, UPDATE, DELETE, are the verbs of SQL. Everything else in this course is detail and refinement around them.
Inserting Rows
INSERT adds a new row:
INSERT INTO customers (name, email) VALUES ('Vikesh Singh', 'vikesh@example.com');
INSERT INTO customers (name, email) VALUES ('Tony Stark', 'tony@starkindustries.com');
Notice we didn't mention the id column. Since it's AUTO_INCREMENT, MySQL assigns it automatically: 1 for the first row, 2 for the second, and so on. You can insert several rows in one statement too, which is faster than running INSERT separately for each:
INSERT INTO customers (name, email) VALUES
('Iron Man', 'ironman@avengers.com'),
('Pepper Potts', 'pepper@starkindustries.com');
Selecting Data Back
SELECT reads data back. The simplest form grabs everything:
SELECT * FROM customers;
The asterisk means "every column." This is fine for quick exploration but a bad habit in real application code, because it fetches columns you might not need and breaks silently if someone adds a column later. Better practice is naming exactly what you want:
SELECT name, email FROM customers;
Updating Rows Safely
UPDATE changes existing rows. This is where beginners get genuinely hurt if they're not careful:
UPDATE customers SET email = 'vikesh.singh@example.com' WHERE id = 1;
The WHERE clause is not optional punctuation, it's the entire safety mechanism. If you run UPDATE customers SET email = 'oops@example.com'; without a WHERE clause, MySQL will happily overwrite the email of every single row in the table, with no warning and no undo. This single mistake has ended real careers in production systems. Always write your WHERE clause first in your head, and when possible, run the equivalent SELECT with the same WHERE condition first to see exactly which rows you're about to affect, before switching it to UPDATE or DELETE.
Deleting Rows Safely
DELETE removes rows, and carries the exact same danger:
DELETE FROM customers WHERE id = 4;
Again, DELETE FROM customers; with no WHERE clause deletes every row in the table. It is not the same as DROP TABLE (which also removes the table structure itself), but the data loss is just as real and just as permanent without a backup.
A practical habit worth building now: before any UPDATE or DELETE on real data, run the matching SELECT first:
SELECT * FROM customers WHERE id = 4;
If that SELECT returns exactly the rows you intended, swap SELECT * FROM for DELETE FROM (or your UPDATE), keeping the WHERE clause identical. This one habit prevents the vast majority of "I deleted the wrong thing" disasters.
Another beginner mistake: assuming INSERT, UPDATE, and DELETE show you the changed data automatically. They don't, they just report how many rows were affected, something like "1 row affected." To see the actual current state, you need a follow-up SELECT.
One more subtlety: MySQL executes these commands immediately against the actual table by default (this is called autocommit mode). There's a safer way to batch changes together and cancel them if something looks wrong, called transactions, which you'll learn properly in a later lesson. For now, just be deliberate: double check your WHERE clause before pressing enter on UPDATE or DELETE.
Try It Yourself
Worth trying right now: create a small products table, insert three rows (feel free to use "Iron Man Mask" as a product name), then write a SELECT to view them, an UPDATE to change one product's price, and a DELETE to remove one product, always checking with SELECT first.
KEEP READING