หลังจากได้คำแนะนำเรื่อง 5 เคล็ดลับสู่การทำงานในบริษัทเทค (เตรียมตัวอย่างไรให้ได้งาน) จาก พี่เอิร์ล Professor Chanin ได้พูดถึงเรื่องการแสดงผลงานของตัวเองเพื่อให้ได้รับการพิจารณาจากบริษัทเทคฯ ที่เราได้ยื่นสมัครไป วิธี 1 ในนั้นคือ เตรียมความพร้อมด้านการเขียนโค้ด เช่น LeetCode แล้วเราก็ลองเอามาเขียนดู

หลังจากที่สมัคร User ใดแล้ว ก็ให้ไปที่แท็บ Problem แล้วก็เลือกจิ้มปัญหา SQL มาข้อหนึ่ง —ที่คิดว่าตัวเองเชี่ยวแล้ว ฮ่าๆๆ เดี๋ยวรู้

capture จากเว็บ leetcode/problemset

ได้ปัญหานี้มาจ้า → 607. Sales Person

เขาจะให้รายละเอียดปัญหาแล้วก็ความต้องการ (ทางฝั่งซ้ายของจอ) แล้วให้เราใส่โค้ดที่เราแก้ปัญหาได้ทางขวา แล้วกด Run ให้ได้ผลลัพธ์ตรงกับโจทย์ที่ให้ ไปดูรายละเอียดในข้อนี้กันค้าบบ 🤔


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.
- ตารางนี้ sales_id เป็น PK แต่ละ row แสดงถึงชื่อและไอดีของเซลล์แต่ละคน รวมถึงเงินเดือน ค่าคอมฯ และวันที่เริ่มจ้างงาน

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.
- ตารางนี้ com_id เป็น PK แต่ละ row แสดงถึงชื่อและไอดีของแต่ละบริษัท และมีที่ตั้งของบริษัทด้วย

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.
- ตารางนี้ order_id เป็น PK แต่ละ row แสดงข้อมูลการสั่งซื้อแต่ละครั้ง รวมถึง ID ของบริษัท, ID ของเซลล์, วันที่สั่งซื้อ, จำนวนที่สั่งซื้อ
- ตารางนี้มี com_id เป็น FK เชื่อมโยงไปยังตาราง Company 
- ตารางนี้มี sales_id เป็น FK เชื่อมโยงไปยังตาราง SalesPerson 
อันนี้ Query schema เราก๊อบมา insert ลง DB ของเราเองอะ <click>
Create table If Not Exists SalesPerson (sales_id int, name varchar(255), salary int, commission_rate int, hire_date date);
Create table If Not Exists Company (com_id int, name varchar(255), city varchar(255));
Create table If Not Exists Orders (order_id int, order_date date, com_id int, sales_id int, amount int);

Truncate table SalesPerson;
insert into SalesPerson (sales_id, name, salary, commission_rate, hire_date) 
values 
('1', 'John', '100000', '6', '2006-04-01')
('2', 'Amy', '12000', '5', '2010-05-01')
('3', 'Mark', '65000', '12', '2008-12-25')
('4', 'Pam', '25000', '25', '2005-01-01')
('5', 'Alex', '5000', '10', '2007-02-03');

Truncate table Company;
insert into Company (com_id, name, city) 
values 
('1', 'RED', 'Boston'),
('2', 'ORANGE', 'New York'),
('3', 'YELLOW', 'Boston'),
('4', 'GREEN', 'Austin');

Truncate table Orders;
insert into Orders (order_id, order_date, com_id, sales_id, amount) 
values 
('1', '2014-01-01', '3', '4', '10000'),
('2', '2014-02-01', '4', '5', '5000'),
('3', '2014-03-01', '1', '1', '50000'),
('4', '2014-04-01', '1', '4', '25000');

เข้าสู่โจทย์กัน

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”. (Return the result table in any order.)

ให้แก้ปัญหาโดยแสดง “ชื่อของเซลล์” คนที่ไม่ได้สั่งซื้อของจากบริษัท RED (ผลลัพธ์จะเรียงลำดับยังไงก็ได้)

The result format is in the following example. – และเค้าต้องการผลลัพธ์แบบนี้ (output) ข้างล่าง

Input:
SalesPerson table:
+----------+------+--------+-----------------+------------+
| sales_id | name | salary | commission_rate | hire_date  |
+----------+------+--------+-----------------+------------+
| 1        | John | 100000 | 6               | 4/1/2006   |
| 2        | Amy  | 12000  | 5               | 5/1/2010   |
| 3        | Mark | 65000  | 12              | 12/25/2008 |
| 4        | Pam  | 25000  | 25              | 1/1/2005   |
| 5        | Alex | 5000   | 10              | 2/3/2007   |
+----------+------+--------+-----------------+------------+
Company table:
+--------+--------+----------+
| com_id | name   | city     |
+--------+--------+----------+
| 1      | RED    | Boston   |
| 2      | ORANGE | New York |
| 3      | YELLOW | Boston   |
| 4      | GREEN  | Austin   |
+--------+--------+----------+
Orders table:
+----------+------------+--------+----------+--------+
| order_id | order_date | com_id | sales_id | amount |
+----------+------------+--------+----------+--------+
| 1        | 1/1/2014   | 3      | 4        | 10000  |
| 2        | 2/1/2014   | 4      | 5        | 5000   |
| 3        | 3/1/2014   | 1      | 1        | 50000  |
| 4        | 4/1/2014   | 1      | 4        | 25000  |
+----------+------------+--------+----------+--------+
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.

- จาก order ที่ 3, 4 ในตาราง Orders มันง่ายมากที่จะบอกว่า John และ Pam ได้สั่งซื้อจากบริษัท RED ดังนั้น จึงต้องการชื่อของซลล์ที่ไม่ได้สั่งซื้อจากบริษัทนี้ 

เราก็มาคิดว่าจะแก้โจทย์นี้ยังไง คิวรี่ข้างล่างที่เราคิดได้ ฮ่าๆๆ คิดเยอะไป 🤪 ม่ายน้าาาาาา

จาก sub-query ด้านล่างเรา inner join ทุกตารางเลยเพื่อให้ได้ sales_id ที่ขายให้กับบริษัท RED ออกมาก่อน แล้วค่อยมา where not in ออกจากตาราง salepersons ก็จะได้ ชื่อของเซลล์ที่ไม่ได้สั่งซื้อที่บริษัท RED

select 
	s.name  
from salesperson s 
where s.sales_id not in (
	select s.sales_id 
	from salesperson s 
	inner join orders o on s.sales_id  = o.sales_id 
	inner join company c on c.com_id  = o.com_id 
	where c.name = 'RED'
)

ผลที่ได้คือ Runtime 3,237ms ใช้เวลานานไป แงๆๆ 🥲 ก็เลยไปส่อง user ที่ใช้เวลาในการดึงน้อยๆดู (ที่ 1) ว่าเค้าคิวรี่ด้วยวิธีไหน คิวรี่กันยังไง — ปรากฎคือ very simple ผลคือ Runtime: 1,108ms. 😱 OMG 😱

select s.name 
from Salesperson 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'
);

🗝️ Key Takeaways

อย่างแรกที่ได้ “เขียนคิวรี่ให้ simple ไว้ก่อน” พื้นฐานสำคัญที่สุด อย่างที่สองมีวิธีการมากมายที่เราสามารถทำได้ เพื่อให้ได้ผลลัพธ์เดียวกัน อาจจะยากบ้าง ง่ายบ้าง อันนี้แล้วแต่ทางเลือกของบุคคล แต่อย่าลืมว่าเรามี “เวลา” ที่ต้องจ่ายด้วยน้า ⌚


ฮ่าๆ ก็เป็นครั้งแรกที่ไปเยี่ยมชม LeetCode นะครับ ได้ความรู้ดีๆ เลยมาลองรีวิวผลลัพธ์ให้ดูกัน

เขียนดี ไม่ดียังไงติชมได้ครับ + แปลผิดแปลถูก + มี typo ขออภัยครับ 🙏 พยายามอยู่

.

อนาคตว่าจะทำเป็นซีรี่ย์ดูครับ ยังไงฝากติดตามด้วยน้า ขอบคุณครับ

Search

About

Feasible เว็บไซต์ที่นำเสนออาชีพปัจจุบันที่เรา (เจ้าของเว็บ) กำลังทำ ไม่ว่าจะเป็น นักวิเคราะห์ข้อมูล นักเรียน นักอ่าน นักฟาร์ม และอีกหลากหลายมุมมอง เรียกได้ว่าเป็น ‘แกงโฮะ’ เลยล่ะ ฮ่าๆๆ ติดตาม Content ที่จะทำออกมาได้เรื่อยๆ นะครับ ขอบคุณที่เข้ามาเยี่ยมกัน 😁✌️

Social Icons