Skip to main content

Command Palette

Search for a command to run...

LeetCode 607: Sales Person

LeetCode 607: Step-by-Step Sales Person Solution

Published
2 min read
LeetCode 607: Sales Person
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: January 29, 2026
Category: SQL
Time Taken: 10 minute
Difficulty: Easy


Problem Statement

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".

Link: Sales Person


Approach 1 (CTE):

  • First, we can create a CTE to store all orders where the company name is "RED".

  • Then, we will perform a Left Join between SalesPerson and the CTE using their sales_id.

  • The Left Join ensures that any sales_id in the CTE that doesn't match a SalesPerson sales_id will be null. We will use a filter condition to select the names where the CTE sales_id is NULL.

Solution Code:

WITH red_orders AS (
    SELECT o.sales_id
    FROM Orders o
    JOIN Company c ON o.com_id = c.com_id
    WHERE c.name = 'RED'
)

SELECT s.name
FROM SalesPerson s
LEFT JOIN red_orders r ON s.sales_id = r.sales_id
WHERE r.sales_id IS NULL

Approach 2 (NOT EXISTS):

Solution Code:

  • In this approach, we will use the NOT EXISTS clause to select all values that don't meet the condition.

  • In the condition, we will select companies with the name "RED" and match them with the SalesPerson sales_id.

  • This will return the name from SalesPerson for whom the sales_id does not exist.

SELECT s.name
FROM SalesPerson AS s
WHERE NOT EXISTS (
    SELECT 1
    FROM Orders AS o
    JOIN Company AS c
    ON o.com_id = c.com_id
    WHERE c.name = "RED" AND s.sales_id = o.sales_id
)

Key Takeaway: How to use NOT EXISTS properly, still unsure but got better understanding.

Pattern: Aggregation

Mistakes I Made: Many, was not able to solve without help.

Series: 90 Days of Data Engineering Progress: 26/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 27 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 620: Not Boring Movies

Solving LeetCode 620: Not Boring Movies Made Easy