I need to get orders data directly from the database. I have got all orders (about 18,000) since January 2020. I need to do a join to customer_entity however, I realized that about 300 records were missing.

By investigating further, I found out that the customer_id value is null for most of the records. Some records do have a customer_id column set but shipping address id fields are null. These latter occur for orders in processing status.

Most curious is the null value in the customer_id column of some orders, even in orders in complete status.

For the query, I need to join the customer's table so it is not an option to get all data from the sales_order table.

Any help? Thanks


  • You can also get the customer from email saved in the quote table corresponding to the order. Please check if you have that corresponding to the order having null ids in order table. Aug 16, 2021 at 10:33

5 Answers 5


Check whether Guest Orders are enabled for your store. Guest orders usually won't have customer Id set in order table.


customer_id have foreign key. Technically, customers can be deleted. While customer deletion, all his orders sets customer_id to NULL.
You can check column customer_is_guest in sales_order to determine guest orders. You can use customer_email as alternative ID.


Why don't you join sales_order_address - one time for address_type="shipping" and one more join with address_type="billing"?

You can pull all customer information from there, regardless whether customer was signed in or this was a guest order.

Alternatively, you can take joined information from sales_order_grid table.


There are 2 reason of this things.

  1. The Order which you are refer is the Guest User Order.

  2. The Order which you are refer, those customer are delete from the system.

So, no required to perform any join query, because if data is available then it on the same table.


@jstuardo Yes It's Magento 2 issue in the guest order.

Please create the patch and apply

Here is a commit: https://github.com/magento/magento2/commit/e3aea78c3ac3eef0adb0b797949e4b0999d653f5

patch https://github.com/magento/magento2/files/10825539/magento-module-quote-model-quotemanagement-php.patch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.