查看: 1887| 回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] 请教个SQL的面试题

🔗
匿名用户-ZXDI9  2019-1-14 01:50:35 |倒序浏览

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x


P1.  You are given three separate tables (named trip_initiated, trip_cancel, and trip_complete) of the form:  

trip_initiated | trip_id, rider_id, driver_id, timestamp
trip_cancel | trip_id, rider_id, driver_id, timestamp
trip_complete | trip_id, rider_id, driver_id, timestamp

Each trip_id in these tables will be unique and only appear once, and a trip will only ever result in a single cancel event or it will be completed. Write a query to create a single table with one row per trip event sequence (trip initiated → cancel/complete):

dispatch_events | trip_id, rider_id, driver_id, initiated_ts, cancel_ts, complete_ts

There should only be a single row per trip with a unique trip_id.  

P2.  Write at least one test query to validate the data in the resulting table. Indicate what you would expect the query to return if the data were valid.


P1 如下写法对不对?

SELECT ti.trip_id trip_id, ti.rider_id rider_id, ti.driver_id driver_id, ti.timestamp initiated_ts, tc1.timestamp cancel_ts, tc2.timestamp complete_ts
FROM trip_initiated ti
JOIN trip_cancel tc1
ON t1.trip_id = tc1.trip_id
JOIN trip_complete tc2
ON t1.trip_id = tc2.trip_id


还有请教P2题目是什么意思? 谢谢!

评分

参与人数 1大米 +5 收起 理由
MRlfy + 5 给你点个赞!

查看全部评分


上一篇:CC 189 hints 归类
下一篇:市场均衡
全局:
个人想法,有错欢迎指正。P1不能join, 需要left join两个table, 因为join的话你选择的是两个table都有的。2和3两个都是1的子集,需要left join
回复

使用道具 举报

全局:
p2没见过,我的想法是,验证给的数据是valid的,根据题目,首先2和3没有交集,可以join 2,3看一下有没有交集。然后2,3各自不能有duplicate,可以看一下2,3 count(*)和count(unique id)是不是一样多。
回复

使用道具 举报

🔗
magicsets 2019-1-14 09:19:01 | 只看该作者
全局:
P1可以只做一次join(或者left outer join,如果存在既没有cancel又没有complete的trip的话)

  1. SELECT ti.trip_id, ti.rider_id, ti.driver_id,
  2.        ti.timestamp AS initiated_ts,
  3.        tc.cancel_ts, tc.complete_ts
  4. FROM trip_initiated ti LEFT OUTER JOIN (
  5.        SELECT trip_id, timestamp AS cancel_ts, NULL as complete_ts
  6.        FROM trip_cancel
  7.        UNION ALL
  8.        SELECT trip_id, NULL AS cancel_ts, timestamp as complete_ts
  9.        FROM trip_complete
  10.      ) tc ON ti.trip_id = tc.trip_id;
复制代码

评分

参与人数 2大米 +8 收起 理由
MRlfy + 5 给你点个赞!
cingher + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
little9 2019-1-19 04:10:16 | 只看该作者
全局:
magicsets 发表于 2019-1-14 09:19
P1可以只做一次join(或者left outer join,如果存在既没有cancel又没有complete的trip的话)

[mw_shl_c ...

这题要用union all吗? 如果两个table没有交集是不是用union也可以呢
回复

使用道具 举报

🔗
magicsets 2019-1-19 06:11:12 | 只看该作者
全局:
little9 发表于 2019-1-19 04:10
这题要用union all吗? 如果两个table没有交集是不是用union也可以呢

union等价于union distinct,相当于union all + 去重,换句话说union all是不去重的

参考:https://dev.mysql.com/doc/refman/8.0/en/union.html

所以应该是“一般情况下用union,然后如果两个table没有交集那么用union all也可以”


补充内容 (2019-1-19 06:12):
给的那个链接网页中有这么一句:A DISTINCT union can be produced explicitly by using UNION DISTINCT or implicitly by using UNION with no following DISTINCT or ALL keyword.
回复

使用道具 举报

🔗
xyjxlxs 2019-1-19 09:42:02 | 只看该作者
全局:
1. 两个join都改成left join
2. 在result table里query trip_cancel表中的trip_id, 返回的query中complete_ts应该是null;同理在result table里query trip_complete表中的trip_id, 返回的query中cancel_ts应该是null
个人想法哈 你看对么
回复

使用道具 举报

🔗
carriekyyy 2019-1-19 11:21:07 | 只看该作者
全局:
SQL小白拿第一题练一下手,请各位大神指教,请指出错误的地方,好让我学习学习,谢谢:

SELECT a.trip_id, a.rider_id, a.driver_id,
                a.timestamp as initiated_ts,
                b. timestamp as cancel_ts,
                c.timestamp as complete_ts
FROM  
(SELECT a.trip_id, a.rider_id, a.driver_id,
                  a.timestamp as initiated_ts,
                  b. timestamp as cancel_ts
FROM trip_initiated  a LEFT JOIN trip_cancel b ON a.trip_id = b.trip_id)
LEFT JOIN trip_complete  c ON a.trip_id = c.trip_id
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表