SQL to find users that the current user hasn't liked/disliked I have this really long query, which in my opinion looks ugly next to all the tidy code in my project. I was wondering if there was a way to optimize it or make it shorter at least:

$query = &lt;&lt;&lt;SQL
    SELECT * FROM users 
    WHERE NOT EXISTS (
        SELECT id
        FROM profile_likes 
        WHERE profile_id = users.id
        AND viewer_id = $my_user-&gt;id
    )
    AND NOT EXISTS (
        SELECT id
        FROM profile_dislikes
        WHERE profile_id = users.id
        AND viewer_id = $my_user-&gt;id
    )
    AND id != $my_user-&gt;id
SQL


This query is a part of an user filter which has the purpose of filtering the users to only those that the currently logged in user ($my_user) hasn't liked/disliked yet.
Personally, I would just stick with what you have already got.

You could try something like this, although too me it doesn't look any tidier. 
You would need to profile it against your database, to see if it performs any better.

SELECT * 
FROM users
left outer join profile_likes on profile_likes.profile_id = users.id AND profile_likes.viewer_id = $my_user-&gt;id
left outer join profile_dislikes on profile_dislikes.profile_id = users.id AND profile_dislikes.viewer_id = $my_user-&gt;id
where users.id &lt;&gt; $my_user-&gt;id
having profile_likes.profile_id is null and profile_dislikes.profile_id is null