TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Advanced SQL window function quiz

163 点作者 andy_boot超过 7 年前

18 条评论

gravypod超过 7 年前
Why are they recalculating the sum of the weights in the Q0 query?<p>Their answer:<p><pre><code> select age, sum(weight) as total_weight from cats group by age having sum(weight) &gt; 12; </code></pre> Which is equivalent to:<p><pre><code> select age, sum(weight) as total_weight from cats group by age having total_weight &gt; 12;</code></pre>
评论 #16337364 未加载
评论 #16338774 未加载
评论 #16338212 未加载
评论 #16339153 未加载
zer超过 7 年前
Q6: Ranking has an error, or I misunderstood. It says &quot;Order by: ranking, name&quot; but in truth one should not order by name!<p>Anyway, overall very nice!
评论 #16339713 未加载
emmelaich超过 7 年前
See also Markus Winand&#x27;s blog and <a href="http:&#x2F;&#x2F;use-the-index-luke.com&#x2F;sql&#x2F;partial-results&#x2F;window-functions" rel="nofollow">http:&#x2F;&#x2F;use-the-index-luke.com&#x2F;sql&#x2F;partial-results&#x2F;window-fun...</a><p>and Joe Celko <a href="https:&#x2F;&#x2F;www.red-gate.com&#x2F;simple-talk&#x2F;sql&#x2F;t-sql-programming&#x2F;window-functions-in-sql&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.red-gate.com&#x2F;simple-talk&#x2F;sql&#x2F;t-sql-programming&#x2F;w...</a>
评论 #16339570 未加载
nothrabannosir超过 7 年前
Lovely website, I&#x27;m finally learning about window functions as I do this. Thanks a bunch.<p>One question &#x2F; bug report? Q7 rejects this:<p>select name, weight, weight - (min(weight) over (order by weight asc rows between 1 preceding and current row) ) as weight_to_lose from cats<p>is that much worse than lag() or is it just non-idiomatic?<p>(edit: and Q10 asks about &quot;window&quot;, but it was quite hard to google. if you could provide another one of those links to the relevant docs, that could help a bunch :D)
评论 #16339708 未加载
drraid0超过 7 年前
Meanwhile I&#x27;m stuck with mysql 5.4 without foreign keys, cuz, you know, performance. (The main db for a billion dollar security company)
评论 #16339356 未加载
评论 #16338565 未加载
评论 #16358574 未加载
pzxc超过 7 年前
I really really like this creation of yours, and I&#x27;ve already learned a couple things from it.<p>However, you really really need to be careful about SQL injection. I can see that you tried to lock it down as much as possible, as far as I can tell the account the queries are running under only has SELECT permission (no update&#x2F;delete&#x2F;etc). However I was still able to get some data you probably don&#x27;t want me to have...<p>This query returns a list of all tables in your database:<p>SELECT c.relname||&#x27;&#x27; FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace<p>And this query returns a list of users and their privileges:<p>SELECT usename||&#x27;&#x27;, usecreatedb||&#x27;&#x27;, usesuper||&#x27;&#x27;, usecatupd||&#x27;&#x27; FROM pg_user<p>I guess your name is Andy based on your HN username, but who is Rusty? :) And that&#x27;s just what I&#x27;ve been able to get in 5 minutes of trying, let alone a determined attacker.
评论 #16344042 未加载
评论 #16348258 未加载
评论 #16348595 未加载
karmakaze超过 7 年前
I&#x27;ve used a few of these and it&#x27;s great to practice using more!<p>Q7 includes output:<p><pre><code> Misty 5.7 0.2 Oscar 6.1 0.4 Smokey 6.1 0.0 </code></pre> How would one show 0.4 for both Oscar and Smokey?<p><a href="http:&#x2F;&#x2F;www.windowfunctions.com&#x2F;questions&#x2F;7" rel="nofollow">http:&#x2F;&#x2F;www.windowfunctions.com&#x2F;questions&#x2F;7</a>
评论 #16344406 未加载
andy_boot超过 7 年前
Author here - I&#x27;m scrolling thru the logs and it is interesting to see that on the first question lots of people try to write &quot;select name, total_weight&quot; despite there only being a weight column which you have to use an aggregate on to get the total weight.<p>I guess I need to rethink my design a little.
评论 #16339079 未加载
评论 #16338574 未加载
评论 #16338471 未加载
评论 #16338683 未加载
评论 #16338446 未加载
评论 #16338472 未加载
评论 #16339495 未加载
masto超过 7 年前
My feedback after answering the first question:<p>1. Q0&#x27;s success messages says &quot;Group by and Having are commonly used aggregate functions&quot;. This terminology is incorrect. SUM() is an aggregate function. GROUP BY and HAVING are clauses. 2. Without an ORDER BY clause, there&#x27;s no guarantee of the order of the result, but the quiz appears to require the output rows to be in the same (in the case of Q0, undefined) order as the expected rows. While this is unlikely to spontaneously change, it&#x27;s like depending on the order of an unordered hash map. As long as you&#x27;re teaching people things, it&#x27;s good to instill the habit of always ordering their results.
评论 #16340194 未加载
评论 #16340373 未加载
Twisell超过 7 年前
Great job!<p>And here is a small suggestion, you might want to change the name of output column &quot;unique&quot; in Q3 because.<p><pre><code> *expression* as unique --does work and is recommended answer *expression* unique --fail because of use of a restricted word *expression* &quot;unique&quot; --work because of double quote </code></pre> Using a restricted word that need quotation in some context is globally discouraged and in this context can keep someone stuck independently of the problem at stack.<p>PS: also apply to &quot;position&quot; in Q4 maybe thats intentional?
评论 #16341945 未加载
einhverfr超过 7 年前
I guess window functions are advanced features, but I found it a little bit basic. Some things you could do to make it more advanced:<p><pre><code> * add questions about range mode * have questions which discuss default behavior regarding ordering and partitioning * Have questions which rely on things like RANGE 3 PRECEDING or other constructs</code></pre>
评论 #16358600 未加载
picardo超过 7 年前
I&#x27;m stuck on the first question. I&#x27;m not sure why this is wrong. Can anyone help me?<p>&gt; select name, sum(weight) OVER (PARTITION BY name) as running_total_weight from cats order by name<p><a href="http:&#x2F;&#x2F;www.windowfunctions.com&#x2F;questions&#x2F;1" rel="nofollow">http:&#x2F;&#x2F;www.windowfunctions.com&#x2F;questions&#x2F;1</a>
评论 #16340088 未加载
Eremotherium超过 7 年前
Thank you for doing this.<p>Sidenote: either HN killed your backend or I did with a recursive CTE. If so: sorry :)
评论 #16340691 未加载
dbkaplun超过 7 年前
Source:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;bootandy&#x2F;window_funcs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bootandy&#x2F;window_funcs</a>
rattray超过 7 年前
This is cool! How many questions long is it?
评论 #16338303 未加载
tofflos超过 7 年前
Nice. I rarely use window functions and find them a bit arcane. This was really accessible.
postila超过 7 年前
select version();<p>PostgreSQL 9.3.20 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4, 64-bi<p>I wonder why so old version is being used. I will be not supported by the community pretty soon.
评论 #16348621 未加载
thom超过 7 年前
There are some big flaws in window functions I&#x27;d love to see fixed. The main one being the very limited ways in which you can refer to the &#x27;current&#x27; row in window and filter expressions. The second being... you&#x27;re almost always better off just doing all this work in code, in a programming language with dependable performance characteristics.
评论 #16337137 未加载
评论 #16337898 未加载
评论 #16337530 未加载
评论 #16338086 未加载
评论 #16339362 未加载
评论 #16337957 未加载