
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How do I select data from one table only where column values from that table match the column values of another table in MySQL?
For this, you can use subquery along with EXISTS. Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int, -> SubjectName varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(111,'MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(112,'MongoDB'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(113,'Java'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values(114,'C'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1 values(115,'MySQL'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+------+-------------+ | Id | SubjectName | +------+-------------+ | 111 | MySQL | | 112 | MongoDB | | 113 | Java | | 114 | C | | 115 | MySQL | +------+-------------+ 5 rows in set (0.00 sec)
Here is the query to create second table −
mysql> create table DemoTable2 -> ( -> FirstName varchar(20), -> StudentSubject varchar(20) -> ); Query OK, 0 rows affected (0.73 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values('Chris','MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2 values('Bob','MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2 values('Sam','MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2 values('Carol','C'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2;
This will produce the following output −
+-----------+----------------+ | FirstName | StudentSubject | +-----------+----------------+ | Chris | MySQL | | Bob | MySQL | | Sam | MySQL | | Carol | C | +-----------+----------------+ 4 rows in set (0.00 sec)
Here is the query to select data from one table only where column values from that table match the column values of another table −
mysql> select Id from DemoTable1 -> where exists -> ( -> select 1 from DemoTable2 -> where SubjectName=StudentSubject -> );
This will produce the following output −
+------+ | Id | +------+ | 111 | | 114 | | 115 | +------+ 3 rows in set (0.00 sec)
- Related Articles
- Copy column values from one table into another matching IDs in MySQL
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Updating a MySQL table with values from another table?
- Update data in one table from data in another table in MySQL?
- How do we count the records from MySQL table where column holds duplicate/triplicates data?
- How can we update the values in one MySQL table by using the values of another MySQL table?
- How to repeat the values stored in a data column of MySQL table?
- Can we add a column to a table from another table in MySQL?
- MySQL select query to select rows from a table that are not in another table?
- How to select data from a table where the table name has blank spaces in MYSQL?
- MySQL query to select average from distinct column of table?
- Insert data from one table to another in MySQL?
- How to insert values from one table into another in PostgreSQL?
- MySQL select distinct dates from datetime column in a table?

Advertisements