Write an SQL query to display the usernames of all customers who have not placed an order yet.
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Write an SQL query to display the usernames of all customers who have not placed an order yet.
The output must contain exactly 1 column named username.
Show answer & explanation
Option A correctly identifies customers without orders by excluding those whose customer_id appears in the orders table. The subquery returns all customer IDs that have placed orders, and NOT IN filters out those records, leaving only inactive customers. Option B joins customers with orders, returning only those WITH orders (opposite of what we need). Option C references a non-existent column and checks for order_count = 1, not zero orders. Option D queries the orders table for NULL customer_id values, which won't return customer usernames and doesn't solve the problem.
Step-by-step Derivation:
Step-by-step logic:
- We need customers who have NOT placed any orders.
- Start with the customers table: SELECT username FROM customers
- Filter using a subquery that collects all customer_ids from the orders table: (SELECT customer_id FROM orders)
- Exclude matches: WHERE customer_id NOT IN (...)
- Result: Returns usernames of all customers whose ID never appears in the orders table.
Why NOT the others:
- B: INNER JOIN returns only matching records (customers WITH orders)
- C: Assumes an order_count column exists and only checks for count=1, not 0
- D: Queries orders table for NULL values; orders.customer_id IS NULL would rarely be true, and this doesn't output usernames anyway