Struggling with a SQL query of mine here.
I have a table:
APPS(id, game)
Where id is the primary key.
What I was trying to do is show all the id’s who use at least all the games that the person with the id “Tim” does.
I have a query that works, and returns what i need it to, but i cobbled it together from forums.
So what I’d really like is a quick and basic rundown of how the query works. Heavy detail isn’t necessary, as i understand most of the terminology, but just what the query does, and how it determines the correct records.
My query is:
SELECT id
FROM APPS X
WHERE NOT EXISTS
(SELECT *
FROM APPS Y
WHERE id='Tim' AND NOT EXISTS
(SELECT *
FROM APPS Z
WHERE Z.id = A.id AND Z.id = Y.id));
Thank you so much for any help!
Edit: The query works fine, but what i was after is how it works and how it returns the records it does.
Right, so you’ve hit the problem called Relational Division. Joe Celko has written a good article about that.
The query that you posted is really strange:
In relational division, the innermost
not existsquery has two equality statements for different columns. For example, from Joe Celko’s post:Comparing
pilot_nameandplane_namemakes sense, where comparingidtwice makes no sense at all.