Description

Table: SalesPerson

Column NameType
sales_idint
namevarchar
salaryint
commission_rateint
hire_datedate
  • sales_id is the primary key column 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 NameType
com_idint
namevarchar
cityvarchar
  • com_id is the primary key column 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 NameType
order_idint
order_datedate
com_idint
sales_idint
amountint
  • order_id is the primary key column for this table.
  • com_id is a foreign key to com_id from the Company table.
  • sales_id is a foreign key 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 Statement

Write an SQL query to report the names of all the salespersons who did not have any orders related to the company with the name “RED”.

Return the result table in any order. The query result format is in the following example.

Example 1:

Input:

  • SalesPerson table:
sales_idnamesalarycommission_ratehire_date
1John10000064/1/2006
2Amy1200055/1/2010
3Mark650001212/25/2008
4Pam25000251/1/2005
5Alex5000102/3/2007
  • Company table:
com_idnamecity
1REDBoston
2ORANGENew York
3YELLOWBoston
4GREENAustin
  • Orders table:
order_idorder_datecom_idsales_idamount
11/1/20143410000
22/1/2014455000
33/1/20141150000
44/1/20141425000

Output:

name
Amy
Mark
Alex

Explanation:

  • According to orders 3 and 4 in the Orders table, it is easy to tell that only salesperson John and Pam have sales to company RED, so we report all the other names in the table salesperson.

Solution

Here, you need to report salesperson name. In this problem, you first find out all orders with company name “RED”. Then, you find out all salesperson who have not made any orders with company “RED” using NOT IN clause to filter out the salesperson who had any orders with company “RED”.

1SELECT name 
2    FROM SalesPerson 
3    WHERE sales_id NOT IN (
4        SELECT sales_id 
5            FROM Orders
6            LEFT JOIN Company 
7            ON Orders.com_id = Company.com_id
8            WHERE Company.name = 'RED'
9    );