Knowledge Guide
HomeDatabasesSQL Practice Problems

easy Sales Person

Problem

Table: SalesPerson

+-----------------+---------+
| Column Name     | Type    |
+-----------------+---------+
| sales_id        | int     |
| name            | varchar |
| salary          | int     |
| commission_rate | int     |
| hire_date       | date    |
+-----------------+---------+
sales_id is the primary key (column with unique values) for this table.
Each row of this table indicates the name and the ID of a salesperson alongside their salary, commission rate, and hire date.

Table: Company

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| com_id      | int     |
| name        | varchar |
| city        | varchar |
+-------------+---------+
com_id is the primary key (column with unique values) for this table.
Each row of this table indicates the name and the ID of a company and the city in which the company is located.

Table: Orders

+-------------+------+
| Column Name | Type |
+-------------+------+
| order_id    | int  |
| order_date  | date |
| com_id      | int  |
| sales_id    | int  |
| amount      | int  |
+-------------+------+
order_id is the primary key (column with unique values) for this table.
com_id is a foreign key (reference column) to com_id from the Company table.
sales_id is a foreign key (reference column) to sales_id from the SalesPerson table.
Each row of this table contains information about one order. This includes the ID of the company, the ID of the salesperson, the date of the order, and the amount paid.

Problem Definition

Write a solution to find the names of all the salespersons who did not have any orders related to the company with the name "RED".

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

In order to identify salespeople whose sales transactions are not linked to a company with the name 'RED,' we employ a structured SQL query on the "SalesPerson," "Orders," and "Company" tables.

The query utilizes a subquery to exclude sales IDs associated with the specified company, and the final result comprises the names of salespeople meeting the defined criteria.

SELECT s.name FROM salesperson s WHERE s.sales_id NOT IN (SELECT o.sales_id FROM orders o JOIN company c ON c.com_id = o.com_id WHERE c.name = 'RED')

Let's break down the query step by step:

Step 1: Identify sales_ids associated with orders from the company named 'RED'

SELECT o.sales_id FROM orders o JOIN company c ON c.com_id = o.com_id WHERE c.name = 'RED';

This step involves selecting sales_ids from the Orders table where the associated company has the name 'RED'.

The query uses a JOIN clause to match orders with their respective companies based on the com_id.

The WHERE clause filters the results only to include orders from the company named 'RED'.

Output After Step 1:

+----------+ | sales_id | +----------+ | 4 | | 1 | +----------+

Step 2: Use the sales_ids obtained from Step 1 to filter SalesPerson records

SELECT s.name FROM salesperson s WHERE s.sales_id NOT IN (SELECT o.sales_id FROM orders o JOIN company c ON c.com_id = o.com_id WHERE c.name = 'RED');

This step involves selecting the names of SalesPersons whose sales_ids are not present in the sales_ids obtained from Step 1.

The NOT IN clause is used to filter out SalesPersons whose sales_ids are associated with orders from the company named 'RED'.

Final Output:

+------+ | name | +------+ | Amy | | Mark | | Alex | +------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Sales Person? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Sales Person** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Sales Person** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Sales Person**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Sales Person**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes