- 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
MySQL SELECT from two tables with a single query
Use UNION to select from two tables. Let us first create a table −
mysql> create table DemoTable1 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) ); Query OK, 0 rows affected (0.90 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1(FirstName) values('Chris') ; Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1(FirstName) values('Adam'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1(FirstName) values('Sam'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Adam | | 3 | Sam | +----+-----------+ 3 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) ); Query OK, 0 rows affected (1.75 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2(FirstName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2(FirstName) values('Tom'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2(FirstName) values('Bob'); Query OK, 1 row affected (0.50 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Tom | | 3 | Bob | +----+-----------+ 3 rows in set (0.00 sec)
Following is the query to select from two tables using MySQL UNION −
mysql> (select *from DemoTable1) union (select *from DemoTable2) order by FirstName;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 2 | Adam | | 3 | Bob | | 1 | Chris | | 1 | John | | 3 | Sam | | 2 | Tom | +----+-----------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL select and insert in two tables with a single query
- A single MySQL select query on two tables is possible?
- Select the minimum value from the maximum values of two tables with a single MySQL query?
- How to count rows from two tables in a single MySQL query?
- How to insert into two tables using a single MySQL query?
- Use UNION ALL to insert records in two tables with a single query in MYSQL
- Count NOT NULL values from separate tables in a single MySQL query
- A single MySQL query to find the highest and lowest among two tables?
- Update two columns with a single MySQL query
- How can I get the output of multiple MySQL tables from a single query?
- A single query to get the sum of count from different tables in MySQL?
- MySQL query to select a record with two exact values?
- Insert values in two tables with a single stored procedure call in MySQL
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- MySQL UNION SELECT and IN clause in a single query

Advertisements