
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select from MySQL Table A That Does Not Exist in Table B
You can use IN operator to select from one table that does not exist in another. To understand the above syntax, let us create a table.
The first table name is A and second table name is B. The query to create a table is as follows
mysql> create table A -> ( -> Value int -> ); Query OK, 0 rows affected (0.56 sec)
Now you can insert some records in the table using insert command.
The query is as follows
mysql> insert into A values(10); Query OK, 1 row affected (0.23 sec) mysql> insert into A values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into A values(30); Query OK, 1 row affected (0.11 sec) mysql> insert into A values(50); Query OK, 1 row affected (0.10 sec) mysql> insert into A values(80); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from A;
The following is the output
+-------+ | Value | +-------+ | 10 | | 20 | | 30 | | 50 | | 80 | +-------+ 5 rows in set (0.00 sec)
Here is the query to create second table with the name B
mysql> create table B -> ( -> Value2 int -> ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into B values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into B values(50); Query OK, 1 row affected (0.15 sec)
Now you can display all records from the table using select statement.
The query is as follows
mysql> select *from B;
The following is the output
+--------+ | Value2 | +--------+ | 20 | | 50 | +--------+ 2 rows in set (0.00 sec)
Here is the query to select from table A which does not exist in table B
mysql> SELECT * FROM A WHERE Value NOT IN (SELECT Value2 FROM B);
The following is the output
+-------+ | Value | +-------+ | 10 | | 30 | | 80 | +-------+ 3 rows in set (0.00 sec)