I have an annoying SQL statement that seem simple but it looks awfull. I want the sql to return a resultset with userdata ordered so that a certain user is the first row in the resultset if that users emailaddress is in the companies table.

I have this SQL that returns what i want but i think it looks awful:

select 1 as o, * 
from Users u
where companyid = 1
and email = (select email from companies where id=1)
select 2 as o, * 
from Users u
where companyid = 1
and email <> (select email from companies where id=1)
order by o

And by the way, the emailaddress from the user table can be in many companies so there cant be a join on the emailaddress :-(

Do you have any ideas how to improve that statement?

Im using Microsoft SQL Server 2000.

Edit: Im using this one:

select *, case when email from companies where Id=1) then 1 else 2 end AS SortMeFirst 
from Users u 
where u.companyId=1 
order by SortMeFirst

Its way more elegant than mine. Thanks Richard L!

+5  A: 

will this work?:

select, * 
from Users u
     LEFT JOIN companies c on =
where companyid = 1
order by desc
-- order by case when is null then 0 else 1 end
Mladen Prajdic
you need to add Users.companyid = Companies.companyid in the JOIN clause
probably true :)
Mladen Prajdic
+1  A: 

I am not sure this is better, but it is an alternative approach

select *, (select count(*) from companies where email = as o 
from users u 
order by o desc

Edit: if there can be many emails across different companies that match, and you are only interested in the given company, this becomes

select *, 
 (select count(*) from companies c where = and = 1) as o 
from users u 
where companyid = 1
order by o desc
+6  A: 

You could do something like this..

        select CASE 
                WHEN exists (select email from companies c where c.Id = u.ID and c.Email = u.Email) THEN 1 
                ELSE 2 END as SortMeFirst,   * 
    From Users u 
    where companyId = 1 
    order by SortMeFirst
Richard L
note that this isn't an optimal solution since your select in the case will run once for every returned row. this can potentialy cause a huge performance problem
Mladen Prajdic