这道题来源于 2025 年真实 Meta 数据/数据工程面试,考察你对 SQL 聚合、JOIN 表、多表处理和排序技巧的熟练度。一起来看看题目全文与解析。
📄 题目原文(原封不动)
You’re working with an e‑commerce system that stores orders in a table named
transactions
. Each order record includes the following fields:
customer_id
(integer) — who made the purchasepayment_type
(string) — how the payment was made (e.g. 'Credit', 'PayPal', etc.)payment_amount
(numeric) — amount paidYour task is to write an SQL query that:
- Calculates the total payment amount per
payment_type
,- Counts the number of unique paying customers for each payment_type,
- Returns the results sorted in descending alphabetical order of payment_type.
Example schema reference:
transactions
├── customer_id
├── payment_type
└── payment_amountWrite the query in a way that handles NULL
payment_amount
(assume treat as zero) and only counts customers who made at least one successful payment.
📘 中文翻译及关键说明
你正在处理一个电商系统中的 transactions
表。每条记录包括:
customer_id
— 用户 IDpayment_type
— 支付方式,如“Credit”、“PayPal”等payment_amount
— 支付金额(可能为 NULL)
任务:
- 按支付方式统计支付总金额(NULL 作 0 处理)
- 计算每种支付方式对应的独立付费客户数量(即
customer_id
去重) - 按支付方式的名称降序(Z→A)排列结果
⚠️ 额外要求:
- NULL 金额需忽略或看作 0
- 只统计有支付行为的客户
✅ 示例查询答案
SELECT
payment_type,
COALESCE(SUM(payment_amount), 0) AS total_sales,
COUNT(DISTINCT customer_id) AS unique_customers
FROM
transactions
WHERE
payment_amount IS NOT NULL
GROUP BY
payment_type
ORDER BY
payment_type DESC;
🧠 解题思路总结
步骤 | 核心操作 |
---|---|
Clarify 问题 | 如何处理 NULL?是否只统计成功交易? |
聚合统计 | SUM(payment_amount) , COUNT(DISTINCT customer_id) |
Null 处理 | COALESCE 或 WHERE payment_amount IS NOT NULL |
排序 | ORDER BY payment_type DESC |
🎓 我们如何帮助客户现场拿下这题?
客户在面试中因紧张卡壳,我们采用了全面协助流程:
- Clarify 引导:推送澄清问题建议,如“是否统计 NULL 金额”“customer_id 唯一性的要求”等。
- 同步推送 SQL 结构:在面试时即时组织,清晰说出 SELECT、GROUP BY、ORDER BY 的逻辑。
- 补充优化与拓展:当面试官问“若 payment_amount 为字符串或存在 refunds,怎么办?”我们支持给出备选方案,展现深度思维。
- 节奏协作:客户只需按我们的结构陈述 SQL,阐述逻辑,我们后台完成文字记录,确保流畅输出。
✨ 为什么这题关键?
- SQL 聚合题在 Meta 的 Data Engineer、BI、Data Analyst 招聘中极常见,【实际统计 + 去重 + 排序】是面试核心考点之一。stratascratch.com+6datalemur.com+6glassdoor.co.uk+6datalemur.comglassdoor.co.in+1glassdoor.co.uktryexponent.com+1
- GDPR/数据治理背景下,统计业务数据时对 NULL 处理尤为关键。
- 对面试语言组织能力也是一次锻炼——实时表达 + 书写 SQL。
题目二:社交推荐奖励系统 — 顾客邀请与支付分析
📄 英文原题全文:
You're working with customer referral and transaction data for a book marketplace.
There are two tables:customers
customer_id
(int)invited_by_customer_id
(int, nullable)transactions
customer_id
(int)book_count
(int)payment_amount
(float)Task:
Write a query to find the top 5 customers who invited others that went on to purchase books.
Specifically, return:
invited_by_customer_id
asinviter_id
- the average payment per book made by all their invitees (sum of payments / sum of books)
Output should be ordered by average payment per book in descending order.
Limit to top 5 results.
📘 中文翻译(原意不变)
你正在分析图书电商平台的用户邀请机制与付费行为。
现有两个表:
customers
表customer_id
:顾客 IDinvited_by_customer_id
:邀请人 ID(可能为空)
transactions
表customer_id
:顾客 IDbook_count
:购买图书数量payment_amount
:支付金额
任务:
找出邀请过其他用户并成功促成交易的前 5 名顾客,
返回字段:
inviter_id
:邀请人 ID(即被邀请人的 invited_by_customer_id)- 其所有被邀请人平均每本书的付款金额(总付款 ÷ 总书本数)
结果按「平均每本书支付金额」降序排列,最多返回前 5 名。
✅ 示例 SQL 解法:
SELECT
c.invited_by_customer_id AS inviter_id,
SUM(t.payment_amount) / SUM(t.book_count) AS avg_payment_per_book
FROM
customers c
JOIN
transactions t ON c.customer_id = t.customer_id
WHERE
c.invited_by_customer_id IS NOT NULL
GROUP BY
c.invited_by_customer_id
ORDER BY
avg_payment_per_book DESC
LIMIT 5;
🧠 解题逻辑精讲
步骤 | 核心操作说明 |
---|---|
1. JOIN 两表 | 连接被邀请人及其交易数据 |
2. WHERE 过滤 | 排除没有邀请人的记录 |
3. GROUP BY | 聚合每个邀请人邀请的所有被邀请人的数据 |
4. 聚合函数 | SUM(payment_amount) / SUM(book_count) |
5. LIMIT 排序 | 按 avg 降序,取前 5 名 |
🎓 我们在面试中如何支持客户应对这道题?
这道题中客户在以下几个地方出现了卡顿:
- 未考虑
invited_by_customer_id IS NOT NULL
过滤条件 - 聚合逻辑语句不熟练,
SUM(a)/SUM(b)
结构写错 - 不知道该聚合的是 invitee 的数据,而不是 inviter 自己
我们做了以下现场辅助:
🟢 实时结构提示:快速语义还原题意——“统计的是被邀请人数据但按邀请人聚合”
🟢 变量同步翻译:中英文字段名称同步标注,防止字段写错
🟢 逻辑口径同步:客户在面试中可以照着我们实时推送的结构表达——聚合表达式、where 条件、order 限制等不漏项
最终,客户稳定写完查询,表达逻辑完整清晰,顺利通过 Meta SQL 面试环节。
✅ 题目难点总结
- 聚合对象与分组对象不在同一表:invitee 的数据需归因于 inviter
- 平均计算结构易错(除法顺序,sum 函数嵌套)
- 必须加 invite 条件筛选
- 真实业务场景贴合推荐系统或社交裂变模型,考察面广
总结
如果你正在备战 Meta、Amazon、Stripe 等公司数据类技术面试,掌握这类 SQL 聚合题与思路表达至关重要。不仅你会写 SQL,更关键的是你能讲清楚,逻辑严密、细节无遗漏。
如需:
- 输出完整代码解题脚本/中英文双语讲稿
- 实时 “直播式” 支持(实战辅助)
- 代面服务(同步讲师作答)
📮 如果你也即将迎来 Uber、Amazon、TikTok、Meta 等技术面试,
不要再独自硬扛,来找我们,一起稳稳走完这场硬仗。
