I'm currently working on the project which has some mysql query. All of mysql query in the project has been done by another developer. Anyway, I'm a bit confusing with the query that he has done below:
SELECT MIN(s_date) AS s_date,
client_id
FROM tb1
WHERE flag = 1
AND client_id NOT IN (
SELECT DISTINCT client_id
FROM tb1
WHERE flag = 0
)
GROUP BY client_id;
The first part of the query is checking if flag = 1 and the second part is checking NOT IN ( flag = 0). I think it's kind of redundant since the flag = 1, it can't be 0. I don't understand the logic of that query. And also I think that NOT IN is a bit slow (it takes 2 sec for my database).
Please explain me what does it mean by that query and how can I simplify and improve it.
idas the primary key, andclient_idwould be a foreign key