
- 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
MySQL select query to select rows from a table that are not in another table?
For our example, we will create two tables and apply Natural Left Join to get the rows from a table not present in the second table.
Creating the first table.
mysql> create table FirstTableDemo -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.48 sec)
Inserting records into first table.
mysql> insert into FirstTableDemo values(1,'Bob'),(2,'John'),(3,'Carol'); Query OK, 3 rows affected (0.13 sec) Records: 3 Duplicates: 0 Warnings: 0
To display all records.
mysql> select *from FirstTableDemo;
The following is the output.
+------+-------+ | id | name | +------+-------+ | 1 | Bob | | 2 | John | | 3 | Carol | +------+-------+ 3 rows in set (0.00 sec)
Creating second table.
mysql> create table SecondTableDemo -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.62 sec)
Inserting records into the second table.
mysql> insert into SecondTableDemo values(1,'Bob'),(2,'John'); Query OK, 2 rows affected (0.12 sec) Records: 2 Duplicates: 0 Warnings: 0
To display all records.
mysql> select *from SecondTableDemo;
The following is the output.
+------+------+ | id | name | +------+------+ | 1 | Bob | | 2 | John | +------+------+
The following is the syntax to select the rows which are not in the second table.
mysql> SELECT tbl1.* -> FROM FirstTableDemo tbl1 -> NATURAL LEFT JOIN SecondTableDemo tbl2 -> where tbl2.name IS NULL;
The following is the output that displays the rows which are in the first table, but not in the second table i.e. “Carol”.
+------+-------+ | id | name | +------+-------+ | 3 | Carol | +------+-------+ 1 row in set (0.03 sec)
- Related Articles
- How to select from MySQL table A that does not exist in table B?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL SELECT from table A that does not exist in table B using JOINS?
- Select rows from a MySQL table and display using IN()
- Randomly SELECT distinct rows in a MySQL table?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- MySQL query to select average from distinct column of table?
- How to select all rows from a table except the last one in MySQL?
- Select the table name as a column in a UNION select query with MySQL?
- How to SELECT fields from one table and INSERT to another in MySQL?
- Select from table where value does not exist with MySQL?
- How to select only 3 ordered rows on a MySQL table?
- Select random row that exists in a MySQL table?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- A single MySQL query to select value from first table and insert in the second?

Advertisements