mysqldude

Master detail query with no child records

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
JOIN ... USING (original)
original post
✓✓✓✓✓✓✓✓✓
LEFT JOIN keeps customers without orders (original)
original post
✓✓✓✓✓✓✓✓✓
LEFT JOIN ... WHERE order_id IS NULL (original)
original post
✓✓✓✓✓✓✓✓✓
NOT EXISTS (original)
original post
✓✓✓✓✓✓✓✓✓
NOT IN (subquery) returns nothing once orders has a NULL customer_id
version check
✓✓✓✓✓✓✓✓✓
JOIN ... USING (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
LEFT JOIN keeps customers without orders (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
LEFT JOIN ... WHERE order_id IS NULL (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
NOT EXISTS (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
NOT IN (subquery) returns nothing once orders has a NULL customer_id version check
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

The original article

First published 2012-04-26. The text below is the original; the table above shows how it behaves on current versions.

I have seen this requested quite a bit on various forums where there is a master table (customers) and a child table (orders). We need to identify several pieces of information:

  • Customer orders
  • Customer and their orders even if they do not have orders
  • Customer that do not have orders

Firstly lets have a look at the tables:

CREATE TABLE customers (
  customer_id int auto_increment primary key,
  name varchar(100)
);

CREATE TABLE orders (
  order_id int auto_increment primary key,
  customer_id int,
  transdate datetime,
  product varchar(10),
  quantity int
);

With these tables we now want to list off all orders for each customers:

SELECT name, transdate, product, quantity FROM customers JOIN orders USING (customer_id);

This query will display all customers and order details where records exist in both the customer and orders table. In some cases we may want to have each customer despite no record existing in the orders table. For this we will use a LEFT JOIN as follows:

SELECT name, transdate, product, quantity FROM customers LEFT JOIN orders USING (customer_id);

So far so good. But what happens if we want to display all customers that do not have entries in the orders table. There are several ways in which this can be resolved.

The first method is to use what we already have above and modify it. If we LEFT JOIN the orders table, then any values for the order fields will be returned as NULL’s. However, we need to be careful in selecting the right field to perform our check on and the primary key field must always have a NON NULL value.

SELECT name FROM customers LEFT JOIN orders USING (customer_id) WHERE order_id IS NULL;

Another way of writing the same query is using the EXISTS syntax.

SELECT name FROM customers c WHERE NOT EXISTS (
  SELECT order_id FROM orders o WHERE c.customer_id = o.customer_id
);