- 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
Compare two tables and return missing ids in MySQL?
To compare two tables and return missing ids, you need to use a subquery. The syntax is as follows −
SELECT yourFirstTableName.yourIdColumnName FROM yourFirstTableName WHERE NOT IN(SELECT yourSecondTableName.yourIdColumnName FROM youSecondTableName);
To understand the above syntax, let us create a table with sample fields and then we will insert records. The query to create the first table −
First_Table
mysql> create table First_Table -> ( -> Id int -> ); Query OK, 0 rows affected (0.88 sec)
Now insert some records in the table using insert command. The query is as follows −
mysql> insert into First_Table values(1); Query OK, 1 row affected (0.68 sec) mysql> insert into First_Table values(2); Query OK, 1 row affected (0.29 sec) mysql> insert into First_Table values(3); Query OK, 1 row affected (0.20 sec) mysql> insert into First_Table values(4); Query OK, 1 row affected (0.20 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from First_Table;
The following is the output −
+------+ | Id | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec)
Here is the query to create the second table −
Second_Table
mysql> create table Second_Table -> ( -> Id int -> ); Query OK, 0 rows affected (0.60 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into Second_Table values(2); Query OK, 1 row affected (0.19 sec) mysql> insert into Second_Table values(4); Query OK, 1 row affected (0.20 sec) Display all records from the table using select statement: mysql> select *from Second_Table;
The following is the output −
+------+ | Id | +------+ | 2 | | 4 | +------+ 2 rows in set (0.00 sec)
Here is the query to compare two tables and return missing ids −
mysql> select First_Table.Id from First_Table where -> First_Table.Id NOT IN(select Second_Table.Id from Second_Table);
The following is the output −
+------+ | Id | +------+ | 1 | | 3 | +------+ 2 rows in set (0.00 sec)
- Related Articles
- Compare two tables and return missing ids using MySQL LEFT OUTER JOIN
- How can we compare data in two MySQL tables?
- How to find missing value between two MySQL Tables?
- Compare two columns and add missing values in Excel
- MySQL join two tables?
- Compare two arrays and return the element-wise minimum in Numpy
- Compare two arrays and return the element-wise maximum in Numpy
- How to compare two DataFrames in Python Pandas with missing values
- Compare return inward and return outward
- Compare and return True if two string arrays are equal in Numpy
- Merge two tables with union in MySQL?
- Compare two arrays of single characters and return the difference? JavaScript
- How to set different auto-increment ids for two tables with a user-defined variable?
- Compare two arrays and return the element-wise maximum ignoring NaNs in Numpy
- MySQL select and insert in two tables with a single query

Advertisements