# Repeated conditions in sql queries

**URL:** <https://www.ruby-forum.com/t/repeated-conditions-in-sql-queries/171565>\
**Category:** Rails\
**Created:** [July 5, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/repeated-conditions-in-sql-queries/171565 "2009-07-05T18:58:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![barunio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b487fb/32.png) [@barunio](https://www.ruby-forum.com/u/barunio)\
**Post date:** [July 5, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/repeated-conditions-in-sql-queries/171565/1 "2009-07-05T18:58:05Z")

</div>

This is a minor question, mostly for curiosity: using rails 2.3, does  
anybody else notice repeated conditions in sql queries?

consider the following app with two models linked through a join  
table:

class User \< ActiveRecord::Base  
has\_many :boxes\_users  
has\_many :boxes, :through =\> :boxes\_users  
end

class Box \< ActiveRecord::Base  
named\_scope :brown, :conditions =\> {:color =\> ‘brown’}  
end

class BoxesUser \< ActiveRecord::Base  
belongs\_to :box  
belongs\_to :user  
end

If I run this query:

> > User.find(1).boxes.brown

The following SQL is generated:

SELECT “boxes”.\* FROM “boxes” INNER JOIN “boxes\_users” ON “boxes”.id =  
“boxes\_users”.box\_id WHERE ((“boxes\_users”.user\_id = 1)) AND  
((“boxes”.“color” = ‘brown’) AND ((“boxes\_users”.user\_id = 1)))

Note that the (“boxes\_users”.user\_id = 1) condition is repeated in the  
sql query. Think does not impact the query results (though it does  
makes parsing the development log a bit more tedious). Just curious  
if anyone knows if this could potentially lead to less efficient  
database queries in some circumstances, or have other secondary  
effects?

---

<div class="post-metadata">

**Author:** ![barunio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b487fb/32.png) [@barunio](https://www.ruby-forum.com/u/barunio)\
**Post date:** [July 5, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/repeated-conditions-in-sql-queries/171565/2 "2009-07-05T19:24:13Z")

</div>

Barun S. wrote:  
[…]

> consider the following app with two models linked through a join  
> table:
> 
> class User \< ActiveRecord::Base  
> has\_many :boxes\_users  
> has\_many :boxes, :through =\> :boxes\_users  
> end
> 
> class Box \< ActiveRecord::Base  
> named\_scope :brown, :conditions =\> {:color =\> ‘brown’}  
> end
> 
> class BoxesUser \< ActiveRecord::Base  
> belongs\_to :box  
> belongs\_to :user  
> end

Off the main topic for a second: if the join table needs a separate  
model, change its name to something more descriptive. “BoxesUser” is  
not a terribly descriptive name. “Assignment” or “BoxAssignment” might  
be clearer.

> If I run this query:
> 
> > > User.find(1).boxes.brown
> 
> The following SQL is generated:
> 
> SELECT “boxes”.\* FROM “boxes” INNER JOIN “boxes\_users” ON “boxes”.id =  
> “boxes\_users”.box\_id WHERE ((“boxes\_users”.user\_id = 1)) AND  
> ((“boxes”.“color” = ‘brown’) AND ((“boxes\_users”.user\_id = 1)))
> 
> Note that the (“boxes\_users”.user\_id = 1) condition is repeated in the  
> sql query. Think does not impact the query results (though it does  
> makes parsing the development log a bit more tedious). Just curious  
> if anyone knows if this could potentially lead to less efficient  
> database queries in some circumstances, or have other secondary  
> effects?

I doubt it. I would guess that the DB’s query optimizer would notice  
the duplicate condition right away and remove it.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
