This is my database project.
table user_
select id, name from user_;
id | name
----+----------
1 | bartek
2 | bartek_m
3 | bartek_k
4 | bartek_b
table order_
select id, employee_id, user_id from order_;
id | employee_id | user_id
----+-------------+---------
1 | 3 | 1
2 | 4 | 1
3 | 4 | 1
4 | 4 | 1
5 | 3 | 1
Every user has a role(I don’t add table role_ here but it exist).
ID of every user with role employee can be assigned to order_.employee_id
I need obtain user with minimum number of orders
select a.id, min(a.count) from (
select u.id, count(u.id) from user_ u, order_ o
where u.id = o.employee_id
group by u.id
) as a group by a.id, a.count
id | min
----+-----
4 | 3
3 | 2
I think that my query is wrong. Could anyone change query to return
only ID this user which have minimum number of orders?
Like
(if I read you right)?