Use Parentheses Wisely

Jen McCown plays around with the AND and OR operators:

Specifically, how is it evaluated when your where clause says “WHERE This AND That OR Something AND that”, without any clarifying parenthesis?

Let’s play around with this. The simplest test scenario is a SELECT 1. If I get a 1 back, that means my WHERE clause evaluated to true, right? Right.

Parentheses should clarify statements.  If I see an “AND” and an “OR” in a WHERE clause, I want to see parentheses, even if you’ve gotten it right.  It’s too easy to misinterpret precedence.

Related Posts

Matrix Transposition In T-SQL

Phil Factor has some fun transposing a matrix using T-SQL: What I’m doing is simply converting the table into its JSON form, and then using this to create a table using the multi-row VALUES  syntax which paradoxically allows expressions. The expression I’m using is JSON_Value, which allows me do effectively dictate the source within the table, via […]

Read More

Joins And Parentheses

Shane O’Neill walks through different ways of grouping tables in a SQL query: Asker: that’d be awesome if i can inner join two other tables instead of the table mentioned after FROM keyword Me: …wait, what? A: He’s asking t1 left join t12 t1 left join t13 t12 inner join t13 M: em…it’s possible but it’s…iffy […]

Read More

1 Comment

  • Jennifer McCown on 2016-01-04

    I agree entirely. It wasn’t the point of the post, but it’s definitely applicable.

Comments are closed

Categories

January 2016
MTWTFSS
« Dec Feb »
 123
45678910
11121314151617
18192021222324
25262728293031