select
t1.id,
sum(t2.res) as sum_res
from users t1
inner join otvety t2 on t2.id_user = t1.id
inner join voprosy t3 on t3.id = t2.id_vopr
where t3.id_cat = 5
group by t1.id
order by sum_res
select
distinct
t1.user_id
from table t1
left join
(select distinct
user_id
from table
where status_id <> 0
) t2 on t2.user_id = t1.user_id
where t1.status_id = 0
and t2.user_id is null
select
t1.ContactName,
t1.Country,
count(*)
from Customers t1
inner join Orders t2 on t2.CustomerID = t1.CustomerID
group by t1.ContactName, t1.Country
having count(*) > 1