- 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
A single MySQL select query on two tables is possible?
Yes, it is possible. Following is the syntax −
select * from yourTableName1,yourTableName2;
Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(),(),(); Query OK, 3 rows affected (0.14 sec) Records: 3 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+----+ | Id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec)
Here is the query to create second table −
mysql> create table DemoTable2 -> ( -> FirstName varchar(10) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values('Chris'),('David'),('Sam'); Query OK, 3 rows affected (0.10 sec) Records: 3 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable2;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Chris | | David | | Sam | +-----------+ 3 rows in set (0.00 sec)
Following is the query to perform select query on two tables −
mysql> select Id,FirstName from DemoTable1,DemoTable2;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Chris | | 3 | Chris | | 1 | David | | 2 | David | | 3 | David | | 1 | Sam | | 2 | Sam | | 3 | Sam | +----+-----------+ 9 rows in set (0.00 sec)
- Related Articles
- MySQL SELECT from two tables with a single query
- MySQL select and insert in two tables with a single query
- Select the minimum value from the maximum values of two tables with a single MySQL query?
- How to insert into two tables using a single MySQL query?
- How to count rows from two tables in a single MySQL query?
- A single MySQL query to find the highest and lowest among two tables?
- MySQL query to select a count on two separate conditions?
- Use UNION ALL to insert records in two tables with a single query in MYSQL
- MySQL UNION SELECT and IN clause in a single query
- Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query
- Count NOT NULL values from separate tables in a single MySQL query
- 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?
- How is it possible to enter multiple MySQL statements on a single line?

Advertisements