Whether you’re just getting started or need a quick refresher, this guide covers the most essential MySQL commands used in day-to-day development. We’ll go through the basics — selecting, inserting, updating, and deleting data — and touch on creating databases and tables, as well as using joins.
1. SELECT
– Fetch Data from a Table
This is the most used command. It retrieves data from one or more tables.
-- Get all rows and columns SELECT * FROM users; -- Get specific columns SELECT id, name FROM users; -- Filter with WHERE SELECT * FROM users WHERE age > 30; -- Order results SELECT * FROM users ORDER BY created_at DESC; -- Limit number of rows SELECT * FROM users LIMIT 10;
2. INSERT
– Add Data into a Table
Used to insert new records.
</p> -- Insert a single row INSERT INTO users (name, email, age) VALUES ('Alice', '[email protected]', 25); -- Insert multiple rows INSERT INTO users (name, email, age) VALUES ('Bob', '[email protected]', 30), ('Charlie', '[email protected]', 28); <p data-start="951" data-end="978">
3. UPDATE
– Modify Existing Records
Use this to update data in the table.
</p> -- Update a user's email UPDATE users SET email = '[email protected]' WHERE id = 1; -- Increase age for all users older than 30 UPDATE users SET age = age + 1 WHERE age > 30; <p data-start="1291" data-end="1328">
Always use
WHERE
to avoid updating the entire table!
4. DELETE
– Remove Records
Deletes rows from a table.
</p> -- Delete one user DELETE FROM users WHERE id = 5; -- Delete users under 18 DELETE FROM users WHERE age < 18; <p data-start="1624" data-end="1650">
Like
UPDATE
, always useWHERE
unless you intend to clear the table.
5. CREATE DATABASE
– Make a New Database
</p> CREATE DATABASE my_app_db; -- Use the new database USE my_app_db; <p data-start="1624" data-end="1650">
6. CREATE TABLE
– Define a New Table
</p> <p data-start="1624" data-end="1650">CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) UNIQUE, age INT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );</p> <p data-start="1624" data-end="1650">
7. JOIN
– Combine Rows from Multiple Tables
MySQL joins are key for relational data.
</p> -- INNER JOIN: Only matching rows SELECT orders.id, users.name FROM orders INNER JOIN users ON orders.user_id = users.id; -- LEFT JOIN: All users, even if no order SELECT users.name, orders.id FROM users LEFT JOIN orders ON users.id = orders.user_id; <p data-start="2279" data-end="2319">
8. Bonus Commands
DROP
– Delete Tables or Databases
</p> -- Drop table DROP TABLE users; -- Drop database DROP DATABASE my_app_db; <p data-start="2279" data-end="2319">
ALTER
– Modify Table Structure
</p> -- Add a new column ALTER TABLE users ADD COLUMN phone VARCHAR(20); -- Rename a column ALTER TABLE users RENAME COLUMN phone TO phone_number; -- Delete a column ALTER TABLE users DROP COLUMN age; <p data-start="2279" data-end="2319">
Final Thoughts
These commands form the foundation of working with MySQL. As you build more complex apps, you’ll add things like indexing, stored procedures, and transactions — but these basics will take you 80% of the way.
If you’re a developer working with relational data, mastering these commands is a must.