MySQL INSERT SELECT

The INSERT SELECT statement is used to insert data into a table based on the result of a SELECT query from another table.

Example with Tamil Kings

1. Inserting Data Using SELECT

INSERT INTO tamil_kings_auto_increment (king_name, reign_period)
SELECT king_name, reign_period FROM tamil_kings_backup;

Code Explanation: This command inserts data from the tamil_kings_backup table into the tamil_kings_auto_increment table.

Best Practices

  • Ensure that the data types and structure of both tables are compatible.
  • Use filtering conditions in the SELECT query to control which records are inserted.

Key Takeaways

  • The INSERT SELECT statement is useful for copying data between tables.
  • It allows for flexible data insertion based on query results.