Skip to content
Quantoros

Return the names of customers with no rows in logins (logins.userid matches customers.customerid). Table definitions…

SQLLevel: IntermediateAnti-joins and missing data

Return the names of customers with no rows in logins (logins.user_id matches customers.customer_id).

Table definitions (from the source exercise):

CREATE TABLE customers(customer_id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT, credit_score INTEGER);
CREATE TABLE logins(login_id INTEGER PRIMARY KEY, user_id INTEGER, login_date TEXT);
Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: Anti-join customers to logins.

SELECT c.name
FROM customers c
LEFT JOIN logins l ON l.user_id = c.customer_id
WHERE l.login_id IS NULL;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions