Just ran into this.
{if logged_out}
{redirect="404"}
{/if}
{if !segment_3}
{redirect="404"}
{/if}
{if logged_in}
{exp:query sql="SELECT thing FROM table WHERE something = {segment_3}"}
{thing}
{/exp:query}
{/if}
So, when there is no {segment_3} or if I am logged out, EE should redirect me to 404, but instead I'm getting SQL syntax error.