25 道谷歌 SQL 面试题与答案解析(下)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/bL2iaicTYdZn7gtxSFZlfuCW6AdQib8Q1onbR0U2h9icP1eRO6wH0AcyJmqZ7USD0uOYncCYIH7ZEE8IicAOPxyb9IA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">13、解释一下 SQL 中<span style="color: black;">区别</span>类型的连接</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">JOIN是 SQL 的子句,<span style="color: black;">按照</span>表之间<span style="color: black;">一起</span>的列,<span style="color: black;">能够</span>连接两个或多个表,用于合并表及检索数据。最<span style="color: black;">平常</span>的连接类型如下。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">内连接(Inner Join)</strong> :内连接是最<span style="color: black;">平常</span>的,用于返回两个或多个表中满足 JOIN <span style="color: black;">要求</span>的所有行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">左连接(Left Join)</strong>:这种连接在右表满足 JOIN <span style="color: black;">要求</span>时返回左表的行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">右连接(Right Join)</strong> :类似于左连接,但在左表满足 JOIN <span style="color: black;">要求</span>时返回右表的行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">全连接(Full Join)</strong> :全连接在任何一个表中有匹配时返回左表和右表的所有行。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">14、什么是主键(PRIMARY KEY)?</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">主键是<span style="color: black;">独一</span>标识每条记录的约束<span style="color: black;">要求</span>。主键<span style="color: black;">不可</span>有 NULL 值,并且所有值必须<span style="color: black;">独一</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一个表只能有一个主键,但主键<span style="color: black;">能够</span>由单个或多个列<span style="color: black;">构成</span>。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">15、什么是约束(constraints)?</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SQL 中的约束是<span style="color: black;">能够</span>应用于表中特定数据类型的规则,用于限制特定列中的数据类型。SQL 中<span style="color: black;">平常</span>的约束类型如下。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">NOT NULL</strong> - 禁止列中存储空值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">UNIQUE</strong>- 规定列中的值必须<span style="color: black;">独一</span>。主键<span style="color: black;">运用</span> UNIQUE 约束。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">PRIMARY KEY</strong> - 指定哪个字段是主键。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">FOREIGN KEY</strong> - <span style="color: black;">独一</span>标识另一个表中的一行。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">16、SQL 中的 DELETE 和 TRUNCATE 语句有什么区别?</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DELETE 用于从表中删除特定数据。该语句是 DML 命令,比 TRUNCATE 慢。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">TRUNCATE 是 DDL命令,用于删除表中的所有行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">两者的<span style="color: black;">重点</span>区别是<span style="color: black;">运用</span> DELETE 后<span style="color: black;">能够</span>回滚数据。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">17、什么是<span style="color: black;">查找</span>优化?</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">低效的 SQL <span style="color: black;">查找</span>会耗尽数据库资源,<span style="color: black;">引起</span>性能下降和服务中断。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查找</span>优化是使 SQL <span style="color: black;">查找</span>更<span style="color: black;">有效</span>的过程。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查找</span>的效率越高,输出的速度就越快,并最大限度地减少对数据库的影响。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">18、给定以下表格,<span style="color: black;">选取</span><span style="color: black;">最少</span>有 10 名员工的前三个<span style="color: black;">分部</span>,并<span style="color: black;">按照</span>其员工中年薪超过 10 万美元的百分比进行排名。</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">employees 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9FW1oj3GK7R5ntJOWd2zjvyOkdAyd0Wbg8iaGnqlzfCuToQHOKDuKBicA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">departments 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ96dk3jBbN8eL6QxtNkVEiatF6Oy1GicDbeAJuffYF1w5lic3spJiclvibCEg/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输出结果</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9rRPqaHgnvgekKu03qqgaI1YgGMricIxYsRmnVOrESJZib8RpYCsgAicMQ/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">提示</strong> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将其分解成<span style="color: black;">区别</span>的<span style="color: black;">要求</span>子句。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">前 3 个<span style="color: black;">分部</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">年薪超过 10 万美元的员工百分比</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">分部</span><span style="color: black;">最少</span>有 10 名员工</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span>,<span style="color: black;">运用</span> </span>JOIN<span style="color: black;"> 连接 </span>employees<span style="color: black;"> 和 </span>departments<span style="color: black;"> 表。</span><span style="color: black;">以获取<span style="color: black;">每一个</span>员工的工资和<span style="color: black;">分部</span>。</span></p><span style="color: black;">1</span><span style="color: black;">SELECT</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> *</p><span style="color: black;">2</span><span style="color: black;">FROM</span> departments <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d</p><span style="color: black;">3</span><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> employees <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> e</p><span style="color: black;">4</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d.id = e.department_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>,<span style="color: black;">运用</span> GROUP BY子句对<span style="color: black;">分部</span>进行聚合,并<span style="color: black;">运用</span> HAVING 子句过滤掉员工少于 10 人的<span style="color: black;">分部</span>。</p><span style="color: black;">1</span><span style="color: black;">SELECT</span><span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d.name</p><span style="color: black;">3</span><span style="color: black;">FROM</span> departments <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d</p><span style="color: black;">4</span><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> employees <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> e</p><span style="color: black;">5</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d.id = e.department_id</p><span style="color: black;">6</span><span style="color: black;">GROUP</span> <span style="color: black;">BY</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d.name</p><span style="color: black;">7</span><span style="color: black;">HAVING</span> <span style="color: black;">COUNT</span>(*) >= <span style="color: black;">10</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为何</span><span style="color: black;">运用</span> HAVING 子句而不是 WHERE 子句?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这是<span style="color: black;">由于</span> HAVING <span style="color: black;">能够</span>在 GROUP BY 之后应用过滤,不必将原始<span style="color: black;">查找</span>包装在子<span style="color: black;">查找</span>中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">运用</span> CASE WHEN 子句和AVG 函数计算百分比。</p><span style="color: black;">1</span><span style="color: black;">SELECT</span> <span style="color: black;">AVG</span>(<span style="color: black;">CASE</span> <span style="color: black;">WHEN</span> salary > <span style="color: black;">100000</span><span style="color: black;">2</span> <span style="color: black;">THEN</span> <span style="color: black;">1</span> <span style="color: black;">ELSE</span> <span style="color: black;">0</span> <span style="color: black;">END</span>) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">percentage_over_100k</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">AVG() 函数将表达式的<span style="color: black;">每一个</span>值相加,再将其除以值的总数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CASE WHEN 的表达式<span style="color: black;">针对</span>薪水超过 10 万美元的员工返回 1,<span style="color: black;">针对</span>其他员工返回 0。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">完整代码如下。</p><span style="color: black;"> 1</span><span style="color: black;">SELECT</span> <span style="color: black;">AVG</span>(<span style="color: black;">CASE</span> <span style="color: black;">WHEN</span> salary > <span style="color: black;">100000</span><span style="color: black;"> 2</span> <span style="color: black;">THEN</span> <span style="color: black;">1</span> <span style="color: black;">ELSE</span> <span style="color: black;">0</span> <span style="color: black;">END</span>) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">percentage_over_100k</p><span style="color: black;"> 3</span> , d.name <span style="color: black;">as</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> department_name</p><span style="color: black;"> 4</span> , <span style="color: black;">COUNT</span>(*) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> number_of_employees</p><span style="color: black;"> 5</span><span style="color: black;">FROM</span> departments <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d</p><span style="color: black;"> 6</span><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> employees <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> e</p><span style="color: black;"> 7</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d.id = e.department_id</p><span style="color: black;"> 8</span><span style="color: black;">GROUP</span> <span style="color: black;">BY</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d.name</p><span style="color: black;"> 9</span><span style="color: black;">HAVING</span> <span style="color: black;">COUNT</span>(*) >= <span style="color: black;">10</span><span style="color: black;">10</span><span style="color: black;">ORDER</span> <span style="color: black;">BY</span> <span style="color: black;">1</span> <span style="color: black;">DESC</span><span style="color: black;">11</span><span style="color: black;">LIMIT</span> <span style="color: black;">3</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">19、给定一个用户表,编写 SQL <span style="color: black;">查找</span>,获取<span style="color: black;">每日</span>新增用户的累计数,每月重新<span style="color: black;">起始</span>计数。</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">users 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9aHfd8Xbq2GDxsqulnoTZSEG3RS1lMCeodBtiamdXxiayffPgQNq1zAFw/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输出:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9DhOgNhnKHsK7G0gcabicyxtfWuLt3goYBbxH0b2q7rqb6IbZlfEd8ibQ/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">提示</strong> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个问题乍一看似乎<span style="color: black;">能够</span><span style="color: black;">经过</span> COUNT(*),并按日期分组<span style="color: black;">处理</span>。<span style="color: black;">或</span><span style="color: black;">运用</span>累积分布函数<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">处理</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但<span style="color: black;">实质</span>上,要按特定的月日间隔进行分组,在月底将<strong style="color: blue;">用户的统计数据重置为 0</strong>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这种方式的<span style="color: black;">优良</span>在于<span style="color: black;">能够</span>得到留存率表,用于比较<span style="color: black;">每一个</span>月的累积用户数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong></p><span style="color: black;"> 1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WITH daily_total AS (</p><span style="color: black;"> 2</span> <span style="color: black;">SELECT</span><span style="color: black;"> 3</span> <span style="color: black;">DATE</span>(created_at) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> dt </p><span style="color: black;"> 4</span> , <span style="color: black;">COUNT</span>(*) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> cnt</p><span style="color: black;"> 5</span> <span style="color: black;">FROM</span> <span style="color: black;">users</span><span style="color: black;"> 6</span> <span style="color: black;">GROUP</span> <span style="color: black;">BY</span> <span style="color: black;">1</span><span style="color: black;"> 7</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p><span style="color: black;"> 8</span><span style="color: black;"> 9</span><span style="color: black;">SELECT</span><span style="color: black;">10</span>t.dt<span style="color: black;">AS</span> <span style="color: black;">date</span><span style="color: black;">11</span> , <span style="color: black;">SUM</span>(u.cnt) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> monthly_cumulative</p><span style="color: black;">12</span><span style="color: black;">FROM</span> daily_total <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t</p><span style="color: black;">13</span><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> daily_total <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> u</p><span style="color: black;">14</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t.dt >= u.dt</p><span style="color: black;">15</span> <span style="color: black;">AND</span> <span style="color: black;">MONTH</span>(t.dt) = <span style="color: black;">MONTH</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(u.dt)</p><span style="color: black;">16</span> <span style="color: black;">AND</span> <span style="color: black;">YEAR</span>(t.dt) = <span style="color: black;">YEAR</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(u.dt)</p><span style="color: black;">17</span><span style="color: black;">GROUP</span> <span style="color: black;">BY</span> <span style="color: black;">1</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">20、给定一个<span style="color: black;">包括</span>用户订阅<span style="color: black;">起始</span>和结束日期的表,<span style="color: black;">查找</span><span style="color: black;">每一个</span>用户的订阅日期范围<span style="color: black;">是不是</span>与其他用户重叠</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">subscriptions 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ95cSibD3NSiaguXu0TBVAticDRkQPSSTqEXnSicX5EKUYYECb25S45K4QIw/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">示例</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9L8nhkToyfDCl2a2lBGK0pk222jvic9r2Mp2ethPEtiaNd1K4Gwia163Ag/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输出结果</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9MKN8mIx3Y5SCRyKiaWFLqr77SicOGgXTjSiaZuxicro29zTdwVnPial7VNA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">提示</strong> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">将其视为自连接。</span><span style="color: black;">要连接 </span>user_id<span style="color: black;"> 和 </span>start_date<span style="color: black;"> 字段,但要<span style="color: black;">重视</span>不要让用户与自己匹配。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong> </p><span style="color: black;">1</span><span style="color: black;">SELECT</span><span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">s1.user_id</p><span style="color: black;">3</span> , <span style="color: black;">MAX</span>(<span style="color: black;">CASE</span> <span style="color: black;">WHEN</span> s2.user_id <span style="color: black;">IS</span> <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> <span style="color: black;">THEN</span> <span style="color: black;">1</span> <span style="color: black;">ELSE</span> <span style="color: black;">0</span> <span style="color: black;">END</span>) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> overlap</p><span style="color: black;">4</span><span style="color: black;">FROM</span> subscriptions <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s1</p><span style="color: black;">5</span><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> subscriptions <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s2</p><span style="color: black;">6</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s1.user_id != s2.user_id</p><span style="color: black;">7</span> <span style="color: black;">AND</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">s1.start_date <= s2.end_date</p><span style="color: black;">8</span> <span style="color: black;">AND</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> s1.end_date >= s2.start_date</p><span style="color: black;">9</span><span style="color: black;">GROUP</span> <span style="color: black;">BY</span> <span style="color: black;">1</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">21、给定学生及其 SAT 考试成绩的表格,<span style="color: black;">查找</span>测试成绩最接近的两个学生及其成绩差</span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>有多个学生的最小成绩差相同,<span style="color: black;">选取</span>字母<span style="color: black;">次序</span>靠前的学生组合。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">scores 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9LpficrgQMZfSic3hSlgBCI8w2ZjxtUCJosWicTXb45XUfwyCaqrian5GsA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输入</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9JDfIV5jRvibLxI2UyWnh4CCEylDhg5dTMH4d1jjahXZZqPNVDbhFgVw/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输出结果</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9Om0vMXKdD6xic5Cmib8E9VqsDhyDrel0zEwHiar9eHUTajd1gEqOyGIvA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">提示</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">给定的问题引用了一个仅<span style="color: black;">包括</span>两列的表格,此时,必须对同一个表进行自引用,</span><span style="color: black;"><span style="color: black;">亦</span><span style="color: black;">便是</span>将之视为<span style="color: black;">包括</span>相同值的两个<span style="color: black;">区别</span>表格。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong> </p><span style="color: black;"> 1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WITH ScoreDifferences AS (</p><span style="color: black;"> 2</span> <span style="color: black;">SELECT</span><span style="color: black;"> 3</span> a.student <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> one_student,</p><span style="color: black;"> 4</span> b.student <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> other_student,</p><span style="color: black;"> 5</span> <span style="color: black;">ABS</span>(a.score - b.score)<span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score_diff</p><span style="color: black;"> 6</span> <span style="color: black;">FROM</span><span style="color: black;"> 7</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> scores a</p><span style="color: black;"> 8</span> <span style="color: black;">JOIN</span><span style="color: black;"> 9</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> scores b</p><span style="color: black;">10</span> <span style="color: black;">ON</span><span style="color: black;">11</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a.id < b.id</p><span style="color: black;">12</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p><span style="color: black;">13</span><span style="color: black;">SELECT</span><span style="color: black;">14</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> one_student,</p><span style="color: black;">15</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> other_student,</p><span style="color: black;">16</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score_diff</p><span style="color: black;">17</span><span style="color: black;">FROM</span><span style="color: black;">18</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ScoreDifferences</p><span style="color: black;">19</span><span style="color: black;">ORDER</span> <span style="color: black;">BY</span><span style="color: black;">20</span> score_diff <span style="color: black;">ASC</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p><span style="color: black;">21</span> one_student <span style="color: black;">ASC</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p><span style="color: black;">22</span> other_student <span style="color: black;">ASC</span><span style="color: black;">23</span><span style="color: black;">LIMIT</span> <span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">22、给定两个表,一个<span style="color: black;">包括</span>用户的基本信息及其所在社区,另一个<span style="color: black;">包括</span>社区信息。<span style="color: black;">查找</span><span style="color: black;">无</span>用户的社区</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">users 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9znIQOz1A3qrP4hfwic3CnuRfDOc9nibWBm7LNp8nOnxjCnNibpqc3HiaUA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">neighborhoods 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/hTYhvCJkfDfQs5OnB7zxd4s9z7FdiaDJ9EOE1OKu12gBYZ7sHDma0mF8RnbZcgDy7h15nAua8HaubF77MC8Q5Cg/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输出</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">提示</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">要找到<span style="color: black;">无</span>用户居住的社区,即一个表中的数据在另一个表中不存在。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong> </p><span style="color: black;">1</span><span style="color: black;">SELECT</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> n.id, n.name</p><span style="color: black;">2</span><span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> neighborhoods n</p><span style="color: black;">3</span><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> <span style="color: black;">users</span> u <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> n.id = u.neighborhood_id</p><span style="color: black;">4</span><span style="color: black;">WHERE</span> u.id <span style="color: black;">IS</span> <span style="color: black;">NULL</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">23、给定交易表和<span style="color: black;">制品</span>表,<span style="color: black;">查找</span><span style="color: black;">制品</span> ID、<span style="color: black;">制品</span>价格和<span style="color: black;">制品</span>价格高于所有平均交易价格的<span style="color: black;">制品</span>的平均交易价格</span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">transactions 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">products 表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">提示:</strong> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">需要找到所有交易的平均价格。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">交易总价是商品价格X数量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此呢</span>,需要编写子<span style="color: black;">查找</span>获取所有交易的平均值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong></p><span style="color: black;"> 1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WITH avg_transaction_price AS (</p><span style="color: black;"> 2</span> <span style="color: black;">SELECT</span> <span style="color: black;">AVG</span>(p.price * t.quantity)<span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> avg_price</p><span style="color: black;"> 3</span> <span style="color: black;">FROM</span> transactions <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t</p><span style="color: black;"> 4</span> <span style="color: black;">JOIN</span> products <span style="color: black;">AS</span> p <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t.product_id = p.id</p><span style="color: black;"> 5</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p><span style="color: black;"> 6</span><span style="color: black;">SELECT</span><span style="color: black;"> 7</span> p.id <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> product_id, </p><span style="color: black;"> 8</span> p.price <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> product_price,</p><span style="color: black;"> 9</span> (<span style="color: black;">SELECT</span> <span style="color: black;">AVG</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(p.price * t.quantity)</p><span style="color: black;">10</span> <span style="color: black;">FROM</span> transactions <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t</p><span style="color: black;">11</span> <span style="color: black;">WHERE</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t.product_id = p.id</p><span style="color: black;">12</span> ) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> avg_transaction_price</p><span style="color: black;">13</span><span style="color: black;">FROM</span> products <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> p</p><span style="color: black;">14</span><span style="color: black;">WHERE</span> p.price > (<span style="color: black;">SELECT</span> avg_price <span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">avg_transaction_price);</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">24、给定 `transactions` 和 `products` 两个表。假设 `transactions` 表<span style="color: black;">包括</span>超过十亿行用户购买商品的记录</span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查找</span>同一用户购买的配对商品。例如,酒和开瓶器、薯片和啤酒等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">需求</span><span style="color: black;">查找</span>前 5 对配对商品及其名<span style="color: black;">叫作</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">重视</span></strong> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了满足测试用例,P1 应该是字母<span style="color: black;">次序</span>靠前的商品。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">transactions 表</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">products 表</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">输出结果</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">提示</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">需要将其分成几个<span style="color: black;">过程</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>,要找到用户在同一时间购买两个或<span style="color: black;">更加多</span><span style="color: black;">制品</span>的所有实例。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>,利用 user_id 和 created_at 进行<span style="color: black;">查找</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong> </p><span style="color: black;"> 1</span><span style="color: black;">SELECT</span><span style="color: black;"> 2</span> <span style="color: black;">LEAST</span>(p1.name, p2.name) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> P1,</p><span style="color: black;"> 3</span> <span style="color: black;">GREATEST</span>(p1.name, p2.name) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> P2,</p><span style="color: black;"> 4</span> <span style="color: black;">COUNT</span>(*) <span style="color: black;">AS</span> <span style="color: black;">count</span><span style="color: black;"> 5</span><span style="color: black;">FROM</span><span style="color: black;"> 6</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> transactions t1</p><span style="color: black;"> 7</span><span style="color: black;">JOIN</span><span style="color: black;"> 8</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> transactions t2</p><span style="color: black;"> 9</span> <span style="color: black;">ON</span> t1.user_id = t2.user_id <span style="color: black;">AND</span> t1.created_at = t2.created_at <span style="color: black;">AND</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">t1.id < t2.id</p><span style="color: black;">10</span><span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> products p1</p><span style="color: black;">11</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t1.product_id = p1.id</p><span style="color: black;">12</span><span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> products p2</p><span style="color: black;">13</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> t2.product_id = p2.id</p><span style="color: black;">14</span><span style="color: black;">GROUP</span> <span style="color: black;">BY</span><span style="color: black;">15</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> P1, P2</p><span style="color: black;">16</span><span style="color: black;">ORDER</span> <span style="color: black;">BY</span><span style="color: black;">17</span> <span style="color: black;">count</span> <span style="color: black;">DESC</span><span style="color: black;">18</span><span style="color: black;">LIMIT</span> <span style="color: black;">5</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">25、<span style="color: black;">查找</span><span style="color: black;">每一个</span>用户<span style="color: black;">每日</span>播放的歌曲数量</span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">给定 song_plays 表,记录用户播放歌曲的数据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查找</span><span style="color: black;">每一个</span>用户<span style="color: black;">每日</span>播放的歌曲数量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">重视</span></strong> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>某个用户在同一天播放了同一首歌两次,计数应该是两次。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">答案</strong><span style="color: black;">首要</span>,创建 song_plays 表。</p><span style="color: black;">1</span><span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> song_plays (</p><span style="color: black;">2</span> <span style="color: black;">id</span> <span style="color: black;">INT</span> PRIMARY <span style="color: black;">KEY</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p><span style="color: black;">3</span> user_id <span style="color: black;">INT</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p><span style="color: black;">4</span> song_id <span style="color: black;">INT</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p><span style="color: black;">5</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> played_at DATETIME</p><span style="color: black;">6</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>,<span style="color: black;">插进</span>模拟数据。</p><span style="color: black;"> 1</span><span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> song_plays (<span style="color: black;">id</span>, user_id, song_id, played_at) <span style="color: black;">VALUES</span><span style="color: black;"> 2</span>(<span style="color: black;">1</span>, <span style="color: black;">1</span>, <span style="color: black;">101</span>, <span style="color: black;">2024-07-01 10:00:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;"> 3</span>(<span style="color: black;">2</span>, <span style="color: black;">1</span>, <span style="color: black;">102</span>, <span style="color: black;">2024-07-01 11:00:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;"> 4</span>(<span style="color: black;">3</span>, <span style="color: black;">2</span>, <span style="color: black;">101</span>, <span style="color: black;">2024-07-01 12:00:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;"> 5</span>(<span style="color: black;">4</span>, <span style="color: black;">1</span>, <span style="color: black;">101</span>, <span style="color: black;">2024-07-01 12:30:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;"> 6</span>(<span style="color: black;">5</span>, <span style="color: black;">2</span>, <span style="color: black;">103</span>, <span style="color: black;">2024-07-02 09:00:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;"> 7</span>(<span style="color: black;">6</span>, <span style="color: black;">1</span>, <span style="color: black;">104</span>, <span style="color: black;">2024-07-02 10:00:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;"> 8</span>(<span style="color: black;">7</span>, <span style="color: black;">1</span>,<span style="color: black;">101</span>, <span style="color: black;">2024-07-02 11:00:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;"> 9</span>(<span style="color: black;">8</span>, <span style="color: black;">2</span>, <span style="color: black;">102</span>, <span style="color: black;">2024-07-02 11:30:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p><span style="color: black;">10</span>(<span style="color: black;">9</span>, <span style="color: black;">2</span>, <span style="color: black;">101</span>, <span style="color: black;">2024-07-02 12:00:00</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,SQL <span style="color: black;">查找</span>。</p><span style="color: black;"> 1</span><span style="color: black;">SELECT</span><span style="color: black;"> 2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> user_id,</p><span style="color: black;"> 3</span> <span style="color: black;">DATE</span>(played_at) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> play_date,</p><span style="color: black;"> 4</span> <span style="color: black;">COUNT</span>(*)<span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> song_count</p><span style="color: black;"> 5</span><span style="color: black;">FROM</span><span style="color: black;"> 6</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> song_plays</p><span style="color: black;"> 7</span><span style="color: black;">GROUP</span> <span style="color: black;">BY</span><span style="color: black;"> 8</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> user_id,</p><span style="color: black;"> 9</span> <span style="color: black;">DATE</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(played_at)</p><span style="color: black;">10</span><span style="color: black;">ORDER</span> <span style="color: black;">BY</span><span style="color: black;">11</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> user_id,</p><span style="color: black;">12</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> play_date;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢! 感谢您的精彩评论,为我带来了新的思考角度。 软文发布平台 http://www.fok120.com/ 外链发布论坛学习网络优化SEO。
页:
[1]