25 个谷歌 SQL 面试题与答案解析(上)
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">概述</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">谷歌在数据分析面试中经常会考察求职者<span style="color: black;">处理</span> SQL 问题的能力。</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><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>一下谷歌是<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>基本的 SQL问题,如,“什么是 SQL?” “什么是连接?” “什么是主键?”“什么是外键?”等问题,这些基本的 SQL 问题是在测试你对 SQL 的<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>你还不<span style="color: black;">晓得</span>该怎么回答这些问题,<span style="color: black;">能够</span>先看一下《<a style="color: black;">80 个<span style="color: black;">平常</span>的 SQL 面试问题与答案</a>》与《<a style="color: black;">30道SQL面试题助你求职</a>》这两篇<span style="color: black;">文案</span>。</p>
<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>与业务<span style="color: black;">关联</span>的 SQL 问题,例如,围绕客户数据编写各式各样的 SQL <span style="color: black;">查找</span>。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">谷歌面试流程</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>不会<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;">初步<span style="color: black;">tel</span>筛选</strong> - <span style="color: black;">一般</span>会问<span style="color: black;">有些</span>工作背景与工作经验<span style="color: black;">关联</span>的问题。<span style="color: black;">通常</span>由 HR 的人进行初筛,衡量你<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>会问<span style="color: black;">有些</span>基本的 SQL 问题,<span style="color: black;">包含</span><span style="color: black;">有些</span>基本的操作,如 DATE、GROUP BY 和 JOIN。可能还会<span style="color: black;">需求</span>你执行<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;">现场面试</strong> - 最后一轮面试<span style="color: black;">包含</span><span style="color: black;">运用</span> CASE、JOIN、子<span style="color: black;">查找</span>和<span style="color: black;">繁杂</span><span style="color: black;">查找</span>的中高级 SQL 问题。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">谷歌 SQL 面试会问些什么?</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>。</p>
<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><span style="color: black;">平常</span>的基本问题。还会<span style="color: black;">经过</span>案例考察你对 SQL 的实战能力。</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><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;">基本 SQL 面试问题</strong>- 在技术筛选<span style="color: black;">周期</span>,谷歌会考察求职者<span style="color: black;">各样</span> SQL 概念的定义问题。<span style="color: black;">重视</span>,求职者要具备<span style="color: black;">运用</span>浅显易懂的语言解释 SQL 基本概念和定义的能力。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">SQL <span style="color: black;">查找</span>问题</strong> - 测试求职者编写 SQL <span style="color: black;">查找</span>语句的能力。谷歌会<span style="color: black;">供给</span>给求职者一个业务数据集,并<span style="color: black;">需求</span>编写<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;">高级 SQL 问题</strong> - 求职者需要<span style="color: black;">按照</span><span style="color: black;">详细</span>案例问题编写高级 SQL <span style="color: black;">查找</span>。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">谷歌会考察<span style="color: black;">那些</span>岗位 SQL 问题?</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> SQL,但<span style="color: black;">重点</span>在以下岗位的面试中会考察 SQL。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><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> SQL 生成与<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 的基本问题,如 DATE、GROUP BY 和 JOIN,在最后一轮面试中,会考察<span style="color: black;">繁杂</span>的 SQL <span style="color: black;">查找</span>。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">BI 工程师</span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">谷歌的 BI 工程师<span style="color: black;">处在</span><span style="color: black;">制品</span>、数据和业务战略的交汇处。求职者需要利用数据制定业务决策,以改善客户体验。SQL 是谷歌在面试 BI 工程师时测试的重要技能之一。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">数据分析师</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>这些信息。<span style="color: black;">然则</span>在谷歌中,<span style="color: black;">区别</span>团队中的数据分析师的职责会因团队而异。例如,谷歌<span style="color: black;">宣传</span>团队的数据分析师与云盘团队的职责会有很大<span style="color: black;">区别</span>。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">数据<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>方面的技能。虽然面试的重点在统计、算法和<span style="color: black;">设备</span>学习等方向。但仍要做好谷歌考察你 SQL <span style="color: black;">查找</span>能力的准备。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">26 个谷歌 SQL 面试问题与答案</span></h2>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、谷歌搜索频率的中位数</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;">然而,在谷歌的搜索规模太庞大了,<span style="color: black;">查找</span> 2 万亿次搜索的代价太高。<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;">编写 SQL <span style="color: black;">查找</span>计算用户进行搜索的中位数,将中位数四舍五入到小数点后一位。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`search_frequency` 表</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWEuMTckgk1tHg0snukBTBuZSZibmzbSKiadHAibCC5qUTU3tCTfAIddJmw/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`search_frequency` 示例表</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWO0etpVD6zBqYa7IKPa2lYfR0VEDkibQrqvGfpQLgsjGlfjqTsBb2cUQ/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">输出结果示例</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWJNV30eGjxiaoWbpw9zaK4wBTDqbHQ1KL4aC2oSSMZnUfNJTia7Ne0Zog/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">答案</span></h3><span style="color: black;"> 1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WITH searches_expanded AS (</p><span style="color: black;"> 2</span> <span style="color: black;">SELECT</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> searches</p><span style="color: black;"> 3</span> <span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> search_frequency</p><span style="color: black;"> 4</span> <span style="color: black;">GROUP</span> <span style="color: black;">BY</span><span style="color: black;"> 5</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> searches, </p><span style="color: black;"> 6</span> GENERATE_SERIES(<span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, num_users))</p><span style="color: black;"> 7</span><span style="color: black;"> 8</span><span style="color: black;">SELECT</span><span style="color: black;"> 9</span> <span style="color: black;">ROUND</span>(<span style="color: black;">PERCENTILE_CONT</span>(<span style="color: black;">0.50</span>) <span style="color: black;">WITHIN</span> <span style="color: black;">GROUP</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;">ORDER</span> <span style="color: black;">BY</span> searches)::<span style="color: black;">DECIMAL</span>, <span style="color: black;">1</span>) <span style="color: black;">AS</span> <span style="color: black;">median</span><span style="color: black;">11</span><span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> searches_expanded;</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、奇数和偶数<span style="color: black;">测绘</span>值</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>值的表。</p>
<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>值的总和,并将结果<span style="color: black;">表示</span>在两列中。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`measurements` 示例表</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWnBrr7rvwcPZVkmuvI3yma8QNzw66vsWRCmnWZVeTjQuCnWIjzbdpFg/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">输出结果示例</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWZ3cmzoWfhiaWoXBtQ25yw5uqndyv979OPia94R7C8zv0hMeTdpRzichuA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">示例解释</span></h3>
<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;">在 2024 年 7 月 10 日,奇数编号<span style="color: black;">测绘</span>值的和为 2355.75,偶数编号<span style="color: black;">测绘</span>值的和为 1662.74。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在 2024 年 7 月 11 日仅有两个<span style="color: black;">测绘</span>值。奇数编号<span style="color: black;">测绘</span>值的和为 1124.50,偶数编号<span style="color: black;">测绘</span>值的和为 1234.14。</span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">答案</span></h3><span style="color: black;"> 1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WITH ranked_measurements AS (</p><span style="color: black;"> 2</span> <span style="color: black;">SELECT</span><span style="color: black;"> 3</span> <span style="color: black;">CAST</span>(measurement_time <span style="color: black;">AS</span> <span style="color: black;">DATE</span>) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> measurement_day, </p><span style="color: black;"> 4</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">measurement_value,</p><span style="color: black;"> 5</span> ROW_NUMBER() <span style="color: black;">OVER</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;">PARTITION</span> <span style="color: black;">BY</span> <span style="color: black;">CAST</span>(measurement_time <span style="color: black;">AS</span> <span style="color: black;">DATE</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;">ORDER</span> <span style="color: black;">BY</span> measurement_time) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> measurement_num </p><span style="color: black;"> 8</span> <span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> measurements</p><span style="color: black;"> 9</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;">11</span><span style="color: black;">SELECT</span><span style="color: black;">12</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">measurement_day,</p><span style="color: black;">13</span> <span style="color: black;">SUM</span>(measurement_value) FILTER (<span style="color: black;">WHERE</span> measurement_num % <span style="color: black;">2</span> != <span style="color: black;">0</span>) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> odd_sum, </p><span style="color: black;">14</span> <span style="color: black;">SUM</span>(measurement_value) FILTER (<span style="color: black;">WHERE</span> measurement_num % <span style="color: black;">2</span> = <span style="color: black;">0</span>) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> even_sum </p><span style="color: black;">15</span><span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ranked_measurements</p><span style="color: black;">16</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;"> measurement_day;</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、谷歌地图标记的用户生成内容</span></h2>
<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;">编写 SQL <span style="color: black;">查找</span>,<span style="color: black;">查询</span>被标记为 “off-topic” 最多的是哪种场所(place_category)。按 place_category 的升序<span style="color: black;">表示</span><span style="color: black;">查找</span>结果。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`place_info` 示例表</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWD2ic6XaPkowHNwZcOC6ialicA9icXXiaf7QuTQpwWrrSAVo3CmXaBrhOUbA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`maps_ugc_review` 示例表</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWZul3tX5brzmWmY4QSDPy2OrEdhiaAdz5NkKMtzSMnLMg7FkpnGibyuyg/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">输出结果</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWapRB8gY1LP3Rnzd7sa4MBsaWOBlibAadfiapTPGhJ9QliaG4mR9O5NXuQ/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;">共有 2 家餐厅(Baar Baar 和 Rubirosa)被标记为 “off-topic” 。酒吧<span style="color: black;">仅有</span> 1 家。<span style="color: black;">因此呢</span>,输出的结果是 “Restaurant”。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">答案</span></h3><span style="color: black;"> 1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WITH reviews</p><span style="color: black;"> 2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">AS (</p><span style="color: black;"> 3</span> <span style="color: black;">SELECT</span><span style="color: black;"> 4</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> place.place_category,</p><span style="color: black;"> 5</span> <span style="color: black;">COUNT</span>(ugc.content_id) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> content_count</p><span style="color: black;"> 6</span><span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> place_info place</p><span style="color: black;"> 7</span><span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> maps_ugc_review ugc</p><span style="color: black;"> 8</span> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">place.place_id = ugc.place_id</p><span style="color: black;"> 9</span><span style="color: black;">WHERE</span> content_tag = <span style="color: black;">Off-topic</span><span style="color: black;">10</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;"> place_category</p><span style="color: black;">11</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p><span style="color: black;">12</span><span style="color: black;">SELECT</span><span style="color: black;">13</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> place_category,</p><span style="color: black;">14</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> content_count,</p><span style="color: black;">15</span> <span style="color: black;">RANK</span>() <span style="color: black;">OVER</span> (<span style="color: black;">ORDER</span> <span style="color: black;">BY</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">content_count MYSTERY_KEYWORD)</p><span style="color: black;">16</span> <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> top_place</p><span style="color: black;">17</span><span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> reviews;</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">4、确定最受欢迎的谷歌搜索类别</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假设谷歌要分析顶级搜索类别,以优化搜索结果。<span style="color: black;">此刻</span>有 searches 和 categories 两张表。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,searches <span style="color: black;">包括</span>搜索的信息,categories 表中的<span style="color: black;">每一个</span>类别 ID 都对应类别名<span style="color: black;">叫作</span>。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`searches` 示例表</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWvuC6eHoYX4YHuJrsVlyXzfQmm11xnSfXYWvSdcuqqNKGVibeMVpaUicA/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`categories` 示例表</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWEKpibTzdFM3QkA9ibiaww46zyRb0jIH0htqnAg3hp02VzAvWIrSHNj22g/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;">编写 SQL <span style="color: black;">查找</span>,给出 2024 年每月各类中的搜索总数。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">输出结果示例</span></h3>
<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/hTYhvCJkfDfbZDWEfjdf4e4xQBlCAvOWWsAQWdZykaiajTzCAibyRtzxI74cFN5ySmDcxStQfT2vSeZC74rE2SQg/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">答案</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> 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;"> categories.category_name, </p><span style="color: black;"> 3</span> <span style="color: black;">EXTRACT</span>(<span style="color: black;">MONTH</span> <span style="color: black;">FROM</span>searches.search_date)<span style="color: black;">AS</span> <span style="color: black;">month</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;">COUNT</span>(*) <span style="color: black;">OVER</span> (<span style="color: black;">PARTITION</span> <span style="color: black;">BY</span> categories.category_name, <span style="color: black;">EXTRACT</span>(<span style="color: black;">MONTH</span> <span style="color: black;">FROM</span> searches.search_date)) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> total_searches</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;"> searches</p><span style="color: black;"> 7</span><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span><span style="color: black;"> 8</span>categories<span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> categories.category_id = searches.category_id</p><span style="color: black;"> 9</span><span style="color: black;">WHERE</span><span style="color: black;">10</span> <span style="color: black;">EXTRACT</span>(<span style="color: black;">YEAR</span> <span style="color: black;">FROM</span> searches.search_date) = <span style="color: black;">2024</span><span style="color: black;">11</span><span style="color: black;">ORDER</span> <span style="color: black;">BY</span><span style="color: black;">12</span> total_searches <span style="color: black;">DESC</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>连接 searches 和 categories 表,<span style="color: black;">而后</span>按 category_name 和 search_date 的月份对结果进行分区。COUNT(*) OVER() 子句计算各分区中的搜索数量。WHERE 子句<span style="color: black;">保证</span>数据是 2024 年的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,按 total_searches 降序排序,将每月搜索最多的类别排在最前面。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">5、什么是数据库反范式?</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">反范式是修改数据库 schema 的方式,这种方式偏离了典型的范式规则(1NF、2NF、3NF 等)。</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>是当数据库用于报告和分析的目的,而不是用于在线事务处理 (OLTP) 时。</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><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>问题,例如,<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;">6、筛选谷歌<span style="color: black;">宣传</span></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>投放和客户定位。需要从数据库中<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 style="color: black;">宣传</span>的状态(“status”)为 “active”。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">“impressions” 大于 500,000。</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>的最后更新时间(“last_updated”)为 2024 年。</span></p>
<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>以上<span style="color: black;">要求</span>筛选 ads 记录。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`ads` 示例表</span></h3>
<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>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">答案</span></h3><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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ads</p><span style="color: black;">3</span><span style="color: black;">WHERE</span> <span style="color: black;">status</span> = <span style="color: black;">active</span><span style="color: black;">4</span><span style="color: black;">AND</span> impressions > <span style="color: black;">500000</span><span style="color: black;">5</span><span style="color: black;">AND</span> <span style="color: black;">YEAR</span>(last_updated) = <span style="color: black;">2024</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> status 值为 “active” 的记录;</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> “impressions” 超过 500,000 的记录;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,按年份(2024)进行过滤。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">7、存储过程的<span style="color: black;">功效</span>是什么?</span></h2>
<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>接受多个输入参数,并返回多个输出值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如,<span style="color: black;">做为</span>处理 HR 分析项目的谷歌数据分析师,你需要创建一个存储过程以计算某<span style="color: black;">分部</span>的平均工资。</p><span style="color: black;">1</span><span style="color: black;">CREATE</span> <span style="color: black;">FUNCTION</span> get_avg_salary(department_name <span style="color: black;">TEXT</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;">RETURNS</span> <span style="color: black;">NUMERIC</span> <span style="color: black;">AS</span><span style="color: black;">3</span>$<span style="color: black;">BODY</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;">BEGIN</span><span style="color: black;">5</span> <span style="color: black;">RETURN</span> (<span style="color: black;">SELECT</span> <span style="color: black;">AVG</span>(salary) <span style="color: black;">FROM</span> google_employees <span style="color: black;">WHERE</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> department = department_name);</p><span style="color: black;">6</span><span style="color: black;">END</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">7</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$BODY$</p><span style="color: black;">8</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LANGUAGE plpgsql;</p>
<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>数据分析<span style="color: black;">分部</span>的平均工资。</p><span style="color: black;">1</span><span style="color: black;">SELECT</span> get_avg_salary(<span style="color: black;">Data Analytics</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;">8、`EXCEPT` / `MINUS` 操作符的<span style="color: black;">功效</span>是什么,举例说明?</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MINUS / EXCEPT 操作符用于返回<span style="color: black;">第1</span>个 SELECT 语句中未被第二个 SELECT 语句返回的所有行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>,EXCEPT 是在 PostgreSQL 和 SQL Server 中<span style="color: black;">运用</span>的,MINUS 是在 MySQL 和 Oracle 中<span style="color: black;">运用</span>的。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">9、<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;"><span style="color: black;">做为</span>谷歌购物的数据分析师,你需要监控<span style="color: black;">各样</span>谷歌购物<span style="color: black;">宣传</span>的点击率(CTR,click-through rate)和转化率(每次点击实现让用户将商品添加到购物车中的比例)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">给定两个数据表 ad_clicks 和 cart_addition,编写 SQL <span style="color: black;">查找</span><span style="color: black;">评定</span><span style="color: black;">每一个</span><span style="color: black;">宣传</span>的点击率(CTR)和转化率。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`ad_clicks` 示例表</span></h3>
<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>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`cart_addition` 示例表</span></h3>
<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>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">答案</span></h3><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;"> a.ad_id,</p><span style="color: black;"> 3</span> <span style="color: black;">COUNT</span>(<span style="color: black;">DISTINCT</span> a.user_id) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">total_clicks,</p><span style="color: black;"> 4</span> <span style="color: black;">COUNT</span>(<span style="color: black;">DISTINCT</span> c.user_id) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> total_conversions,</p><span style="color: black;"> 5</span> <span style="color: black;">COUNT</span>(<span style="color: black;">DISTINCT</span> c.user_id)*<span style="color: black;">1.0</span> / <span style="color: black;">COUNT</span>(<span style="color: black;">DISTINCT</span> a.user_id) * <span style="color: black;">100.0</span> <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> conversion_rate</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;">ad_clicks a</p><span style="color: black;"> 8</span><span style="color: black;">LEFT</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;"> cart_addition c</p><span style="color: black;">10</span><span style="color: black;">ON</span><span style="color: black;">11</span> a.ad_id = c.ad_id <span style="color: black;">AND</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a.user_id = c.user_id</p><span style="color: black;">12</span><span style="color: black;">GROUP</span> <span style="color: black;">BY</span><span style="color: black;">13</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a.ad_id;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将 ad_clicks 和 cart_addition 表按 ad_id 和 user_id 连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">按 ad_id 分组,计算两个表中 user_id 的<span style="color: black;">独一</span>数量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">total_conversions 除以 total_clicks <span style="color: black;">便是</span>转化率。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><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;"><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>找出上个月每次活动及这些活动中的<span style="color: black;">每一个</span><span style="color: black;">宣传</span>组的平均每次点击的成本(CPC)。CPC 是所有点击的总成本除以点击次数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">宣传</span>点击数据表 ad_clicks 如下。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`ad_clicks` 示例表</span></h3>
<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>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">输出结果示例</span></h3>
<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>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">答案</span></h3><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;"> campaign_id,</p><span style="color: black;">3</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ad_group_id, </p><span style="color: black;">4</span> <span style="color: black;">SUM</span>(<span style="color: black;">cost</span>) / <span style="color: black;">SUM</span>(clicks) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> avg_CPC</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;"> ad_clicks</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;"> campaign_id, </p><span style="color: black;">9</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ad_group_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>将 ad_clicks 表按 campaign_id 和 ad_group_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>给定的公式(即总成本/总点击数)计算平均每次点击<span style="color: black;">花费</span>(avg_CPC)。</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>组的平均点击<span style="color: black;">花费</span>。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">11、在数据库的外键是什么?</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">外键</strong>是表中的一个或一组列,它引用另一个表中的<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>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`google_employees` 示例表</span></h3>
<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;"><span style="color: black;">这里</span>表中,employee_id <span style="color: black;">做为</span>主键,而 manager_id <span style="color: black;">做为</span>外键,<span style="color: black;">由于</span>它连接到员工的 employee_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>员工与其上级经理之间的关系,便于<span style="color: black;">查找</span>某位员工的经理是谁。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">google_employees 表还<span style="color: black;">能够</span>有多个引用其他表主键的外键。例如,<span style="color: black;">能够</span>添加 department_id 和 location_id 外键,用于将<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;">12、分析安卓游戏商店应用的内购数据</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>在游戏商店应用内购买的数据分析。</p>
<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>客户及其<span style="color: black;">近期</span>一次购买的列表。结果应<span style="color: black;">包括</span> customer_id、first name、last name、product 和<span style="color: black;">近期</span>购买日期。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">`Customers` 示例表</span></h3>
<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><span style="color: black;">`Purchases` 示例表</span>
<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><span style="color: black;">答案</span><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;">C.customer_id,</p><span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> C.first_name, </p><span style="color: black;">3</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> C.last_name, </p><span style="color: black;">4</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> C.app, </p><span style="color: black;">5</span> <span style="color: black;">MAX</span>(P.date) <span style="color: black;">AS</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> latest_purchase_date</p><span style="color: black;">6</span><span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Customers C</p><span style="color: black;">7</span><span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Purchases P</p><span style="color: black;">8</span><span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> C.customer_id = P.customer_id</p><span style="color: black;">9</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;">C.customer_id, C.first_name, C.last_name, C.app;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该<span style="color: black;">查找</span>在 customer_id 字段上连接 Customers 和 Google PlayStore Purchases 表。</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>客户<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>了 MAX(P.date) 函数<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;"><strong style="color: blue;">未完待续</strong></span>)</p>
论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。
页:
[1]