Description

Given three tables: salespersoncompanyorders.
Output all the names in the table salesperson, who didn’t have sales to company 'RED'.

Example
Input

Table: salesperson

+----------+------+--------+-----------------+-----------+
| sales_id | name | salary | commission_rate | hire_date |
+----------+------+--------+-----------------+-----------+
| 1 | John | 100000 | 6 | 4/1/2006 |
| 2 | Amy | 120000 | 5 | 5/1/2010 |
| 3 | Mark | 65000 | 12 | 12/25/2008|
| 4 | Pam | 25000 | 25 | 1/1/2005 |
| 5 | Alex | 50000 | 10 | 2/3/2007 |
+----------+------+--------+-----------------+-----------+

The table salesperson holds the salesperson information. Every salesperson has a sales_id and a name.

Table: company

+---------+--------+------------+
| com_id | name | city |
+---------+--------+------------+
| 1 | RED | Boston |
| 2 | ORANGE | New York |
| 3 | YELLOW | Boston |
| 4 | GREEN | Austin |
+---------+--------+------------+

The table company holds the company information. Every company has a com_id and a name.

Table: orders

+----------+----------+---------+----------+--------+
| order_id | date | com_id | sales_id | amount |
+----------+----------+---------+----------+--------+
| 1 | 1/1/2014 | 3 | 4 | 100000 |
| 2 | 2/1/2014 | 4 | 5 | 5000 |
| 3 | 3/1/2014 | 1 | 1 | 50000 |
| 4 | 4/1/2014 | 1 | 4 | 25000 |
+----------+----------+---------+----------+--------+

The table orders holds the sales record information, salesperson and customer company are represented by sales_id and com_id.

output

+------+
| name |
+------+
| Amy |
| Mark |
| Alex |
+------+

Explanation

According to order '3' and '4' in table orders, it is easy to tell only salesperson 'John' and 'Alex' have sales to company 'RED',
so we need to output all the other names in table salesperson.

Code
naive
SELECT s.name FROM salesperson as s WHERE s.name NOT IN (SELECT DISTINCT s1.name FROM salesperson as s1 JOIN company as c JOIN orders as o WHERE s1.sales_id = o.sales_id AND c.name = 'RED' AND o.com_id = c.com_id)

Better

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

[LeetCode] 607. Sales Person_Easy tag: SQL的更多相关文章

  1. [LeetCode] 182. Duplicate Emails_Easy tag: SQL

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  2. [LeetCode] 197. Rising Temperature_Easy tag: SQL

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  3. [LeetCode] 595. Big Countries_Easy tag: SQL

    There is a table World +-----------------+------------+------------+--------------+---------------+ ...

  4. [LeetCode] 610. Triangle Judgement_Easy tag: SQL

    A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. Ho ...

  5. [LeetCode] 577. Employee Bonus_Easy tag: SQL

    Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...

  6. [LeetCode] 627. Swap Salary_Easy tag: SQL

    Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m v ...

  7. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  9. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

随机推荐

  1. <target>.ID 和 <source>.ID 的属性冲突: DataType 属性不匹配

    ORACLE数据库主键的数据类型为number ,如果不指定长度会有 Merge报 <target>.ID 和 <source>.ID 的属性冲突: DataType 属性不匹 ...

  2. li下的ul----多级列表

    <ul id="ul_Style1"> <li>第1级第1行</li> <li> <ul id="ul_Style2 ...

  3. db2 reorg table failed处理

    概要: 当对一个表的结构进行改变后,需要reorg表,重新组织其目录统计信息,为数据库的访问计划提供信息,以便高效执行对其的操作. 对表作reorg操作,发生错误,通过db2 "? sql0 ...

  4. 【PPT大放送】MPD软件工作坊北京站圆满落幕 深圳站即将开幕!

    MPD工作坊深圳站体验票开启啦!文末有彩蛋哦! 7月14日至15日,由麦思博(msup)有限公司举办的第40届MPD软件工作坊在北京国家会议中心举行. 麦思博(msup)有限公司一直专注于软件研发中心 ...

  5. TOP100summit:【分享实录-QQ空间】10亿级直播背后的技术优化

    本篇文章内容来自2016年TOP100summit QQ空间客户端研发总监王辉的案例分享.编辑:Cynthia 王辉:腾讯SNG社交平台部研发总监.腾讯QQ空间移动客户端技术负责人高级工程师.09年起 ...

  6. Java NIO 读取文件、写入文件、读取写入混合

    前言 Java NIO(new/inputstream outputstream)使用通道.缓冲来操作流,所以要深刻理解这些概念,尤其是,缓冲中的数据结构(当前位置(position).限制(limi ...

  7. 用CountDownLatch提升请求处理速度

    countdownlatch是java多线程包concurrent里的一个常见工具类,通过使用它可以借助线程能力极大提升处理响应速度,且实现方式非常优雅.今天我们用一个实际案例和大家来讲解一下如何使用 ...

  8. spark分组统计及二次排序案例一枚

    组织数据形式: aa 11 bb 11 cc 34 aa 22 bb 67 cc 29 aa 36 bb 33 cc 30 aa 42 bb 44 cc 49 需求: 1.对上述数据按key值进行分组 ...

  9. 机器学习入门之使用numpy和matplotlib绘制图形

    机器学习当中能深入浅出的方法第一步就是先学会用numpy了.numpy是一个第三方的开源python库,他提供了许多科学的数值计算工具,尤其是大型矩阵计算,但使用配置非常简单,结合matplotlib ...

  10. a buzzword to refer to modern Web technologies

    https://html.spec.whatwg.org/multipage/introduction.html#is-this-html5? HTML Living Standard — Last ...