Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Unmatched Records Report | Core Join Types and Their Applications
SQL Joins in Depth

bookChallenge: Unmatched Records Report

Task

Swipe to start coding

Write a SQL query to produce a report of all customers and all orders, showing unmatched records and matched pairs using a FULL OUTER JOIN.

Follow these steps:

  • Use a FULL OUTER JOIN to join the customers and orders tables on the customer_id field;
  • In your SELECT clause, include:
    • The customer_id from the customers table;
    • The name column from the customers table, but alias it as customer_name;
    • The order_id from the orders table;
    • The order_date from the orders table;
  • Add a column named match_status that uses a CASE statement with the following logic:
    • If order_id is NULL, set the value to 'Customer without orders';
    • If customer_id is NULL, set the value to 'Order without customer';
    • Otherwise, set the value to 'Matched';
  • Order the results so that all unmatched records (where either customer_id or order_id is NULL) appear before the matched records;

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain that in more detail?

What are the main benefits or drawbacks?

Can you give me an example?

close

bookChallenge: Unmatched Records Report

Swipe to show menu

Task

Swipe to start coding

Write a SQL query to produce a report of all customers and all orders, showing unmatched records and matched pairs using a FULL OUTER JOIN.

Follow these steps:

  • Use a FULL OUTER JOIN to join the customers and orders tables on the customer_id field;
  • In your SELECT clause, include:
    • The customer_id from the customers table;
    • The name column from the customers table, but alias it as customer_name;
    • The order_id from the orders table;
    • The order_date from the orders table;
  • Add a column named match_status that uses a CASE statement with the following logic:
    • If order_id is NULL, set the value to 'Customer without orders';
    • If customer_id is NULL, set the value to 'Order without customer';
    • Otherwise, set the value to 'Matched';
  • Order the results so that all unmatched records (where either customer_id or order_id is NULL) appear before the matched records;

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
single

single

some-alt