- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert values from the first table to the second table using two SELECT statements in a single MySQL query
To insert values from the first table to another table using two SELECT statements, use SUBQUERY. This will allow you to use only a single MySQL query to get the result in the second table. Let us first create a table −
mysql> create table DemoTable1 ( Name varchar(100), Score int ); Query OK, 0 rows affected (1.30 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values('Chris',45); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values('Bob',78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values('David',98); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values('Carol',89); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | Chris | 45 | | Bob | 78 | | David | 98 | | Carol | 89 | +-------+-------+ 4 rows in set (0.00 sec)
Following is the query to create the second table.
mysql> create table DemoTable2 ( StudentName varchar(100), StudentScore int ); Query OK, 0 rows affected (0.58 sec)
Let us now write a MySQL query to insert values from the first table to the second table using two SELECT statements −
mysql> insert into DemoTable2(StudentName,StudentScore) values((select Name from DemoTable1 where Score=98),(select Score from DemoTable1 where Name='David')); Query OK, 1 row affected (0.30 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2; +-------------+--------------+ | StudentName | StudentScore | +-------------+--------------+ | David | 98 | +-------------+--------------+ 1 row in set (0.00 sec)
- Related Articles
- A single MySQL query to select value from first table and insert in the second?
- A single MySQL query to insert records (not all) in the second table from the first table
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Implement MySQL trigger in the first table to insert records in the second table?
- MySQL query for INSERT INTO using values from another table?
- Insert multiple values in a temporary table with a single MySQL query?
- MySQL query to get first two highest column values from a table?
- Insert all the values in a table with a single MySQL query separating records by comma
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- MySQL select and insert in two tables with a single query
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- MySQL select query to select rows from a table that are not in another table?
- Find second max in a table using MySQL query?
- How to insert an array of values in a MySQL table with a single INSERT?
- Select and insert values with preceding zeros in a MySQL table

Advertisements