I have a MySQL table containing users, files and file ages as shown below.
Users | Files | File age |
---|---|---|
A | 1 | 42 |
A | 2 | 17 |
A | 3 | 4 |
B | 4 | 85 |
B | 5 | 73 |
B | 6 | 11 |
C | 7 | 6 |
C | 8 | 9 |
C | 9 | 3 |
Now I need to select the names of all files of a specific user having an age larger 10. Users with no or only some files with an age > 10 shall not be selected.
With the example from above the result should be:
Users | Files | File age |
---|---|---|
B | 4 | 85 |
B | 5 | 73 |
B | 6 | 11 |
I assume that the SQL ALL operator together with a sub query is my friend, but I do not get satisfying results here. Would be therefore happy to hear some ideas.
Thanks a lot!
Ben
2
Answers
Many ways to approach this, a couple:
Use a windowed aggregate:
Use an aggregate in a correlated query:
There are several options.
One possibility is a
left join
and then check fornull
:Another possibility is using
not exists
:Another possibility is using
< ALL
(as you mentioned in the question):You can see a Fiddle here.
I’ve edited my answer to use
<=
instead of just<
since I had your boundary condition wrong.