How to Insert With Select in MySQL
MySQL is an open-source database that is freely available and used in large and small applications. Its use case can get found in school-college applications, enterprise applications, e-commerce platforms, and many more.
Relational Databases are easy to understand and interpret. Hence, the database seeks attention from a wide variety of folks.
The database is created, maintained, and managed by the MySQL team. It holds the ownership of its releases and brings new features.
CRUD operations like create, read, update, and delete are the basic operations needed by the program each time with every DB to perform some manipulations. The language used to query the MySQL relational database is called a query language.
The query language is a rich and predefined protocol that helps insert and manipulate data.
INSERT in MySQL
The syntax to insert data in Database in MySQL is as below.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Let’s understand the insert syntax below.
INSERT INTOkeyword gets its place at the initial first position, intimating MySQL database that inserts operation gets performed.- Next is the
table_namethat is the table’s name that specifies in which table the data needs to get inserted. - The
column namesare the optional content in braces stating the column names are optional entities. There is a privilege to insert values without providing column names. But in that case, all the values to the existing columns must be given in the desired sequence. Valueskeywords specify that the next section will be the set of values inserted in the column. These are again optional, based on the number of columns. If all values are to provide, the sequence of the columns should get maintained along with the braces.;is the terminal operator used to specify that the query has ended.- Example:
INSERT INTO student (id,name,age) values(1,"John","18");
SELECT in MySQL
The syntax to select data in Database in MySQL is as below. Select statement gets used to select the specific lines and data from the table in the MySQL database.
SELECT column1, column2, ...FROM table_name where condition;
Let’s understand the select syntax.
Selectkeyword specifies and intimates the DB that the query triggered to DB is a select statement.Column1andColumn2specify the data that needs to be retrieved from the table.FROMkeyword points to the table that needs to get retrieved from the database.table_namegives the exact name of the table from which one needs retrieval.wherecondition provides a condition in which selection is made based on some criteria.- Example1:
Select * from student;The statement will select all the rows of the student table. - Example2:
Select id, name, age from the student;The statement will select all the rows ofid,name, andagefrom the student table.
INSERT INTO SELECT in MySQL
Now Insert Into Select statement copies data from a selected table and inserts the data in another table. The query that copies selected data in another table is:
INSERT INTO table2 SELECT * FROM table1 WHERE condition;
The prerequisites and a must condition to insert data in another table is the column data type must be the same for the tables.
Let’s understand the insert into select query in detail.
Insert Intostatement intimates MySQL database that insertion will happen after this keyword in the query statement.Table2is the table name wherein the data needs to get copied or the destination table.Selectkeyword will act as a subquery that says the selection will happen on the source table.*or column specifies which column name is to get selected.FROM table1is the source table where the copy of data is required.- The
WHEREcondition is the extra element to get specified data selection based on given criteria. It works as a filter that provides a filter on specified columns instead of all the columns getting selected. - Examples:
Insert into student_copy select * from student where age=28;The statement will copy the rows of student table instudent_copywhere age is28.
Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.
LinkedIn