I recently had this issue with the SPQuery object returning all rows in the list even when I knew that the query was correct.
I Used CAML creator from U2U to construct the query, then remove the <Query> tags around it and then tested it on a windows application. This worked.
Since I needed this to work inside the _layouts directory, I converted the code and placed it in an aspx page in the LAYOUTS directory
Initially it complained about the < and > in the query string, so I decided to escape the string with < and > as per below…
string sQuery =
"<Where><Eq>" +
" <FieldRef Name='Author' /><Value Type='User'>Joe Bloggs</Value></Eq>" +
"</Where>" +
"<OrderBy><FieldRef Name='Author' /><FieldRef Name='ID' Ascending='False' /></OrderBy>";
SPQuery oQuery = new SPQuery();
oQuery.Query = sQuery;
SPListItemCollection items = listLP.GetItems(oQuery);
However, every time we executed the page, the SPListItemCollection returned ALL items in the list… after reading lots of blogs and forums everyone pointed out to an incorrect query string… eventually after a bit of grief we realised that the string should be constructed this way and NOT escaped:
@"<Where><Eq>" +
" <FieldRef Name='Author' /><Value Type='User'>Joe Bloggs</Value></Eq>" +
"</Where>" +
"<OrderBy><FieldRef Name='Author' /><FieldRef Name='ID' Ascending='False' /></OrderBy>";
Once we did this change, the items collection is correctly populated.