2023-01-15:销售员。编写一个SQL查询,报告没有任何与名为 “RED” 的公司相关的订单的所有销售人员的姓名。以 任意顺序 返回结果表。 DROP TABLE IF EXISTS `com
2023-01-15:销售员。编写一个SQL查询,报告没有任何与名为 “RED” 的公司相关的订单的所有销售人员的姓名。以 任意顺序 返回结果表。
DROP TABLE IF EXISTS `company`;
CREATE TABLE `company` (
`com_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
PRIMARY KEY (`com_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `company` VALUES ('1', 'RED', 'Boston');
INSERT INTO `company` VALUES ('2', 'ORANGE', 'New York');
INSERT INTO `company` VALUES ('3', 'YELLOW', 'Boston');
INSERT INTO `company` VALUES ('4', 'GREEN', 'Austin');
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`order_id` int(11) NOT NULL,
`order_date` date NOT NULL,
`com_id` int(11) NOT NULL,
`sales_id` int(11) NOT NULL,
`amount` int(11) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `orders` VALUES ('1', '2014-01-01', '3', '4', '10000');
INSERT INTO `orders` VALUES ('2', '2014-02-01', '4', '5', '5000');
INSERT INTO `orders` VALUES ('3', '2014-03-01', '1', '1', '50000');
INSERT INTO `orders` VALUES ('4', '2014-04-01', '1', '4', '25000');
DROP TABLE IF EXISTS `sales_person`;
CREATE TABLE `sales_person` (
`sales_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`salary` int(11) NOT NULL,
`commission_rate` int(11) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`sales_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `sales_person` VALUES ('1', 'John', '100000', '6', '2006-04-01');
INSERT INTO `sales_person` VALUES ('2', 'Amy', '12000', '5', '2010-05-01');
INSERT INTO `sales_person` VALUES ('3', 'Mark', '65000', '12', '2008-12-25');
INSERT INTO `sales_person` VALUES ('4', 'Pam', '25000', '25', '2005-01-01');
INSERT INTO `sales_person` VALUES ('5', 'Alex', '5000', '10', '2005-02-03');
答案2023-01-15:
sql语句如下:
SELECT
s.name
FROM
sales_person s
WHERE
s.sales_id NOT IN (SELECT
o.sales_id
FROM
orders o
LEFT JOIN
company c ON o.com_id = c.com_id
WHERE
c.name = 'RED')
公众号:福大大架构师每日一题