thanks for the reply, but where should i give the uid = 1
Main Topics
Browse All TopicsHi,
I need some help with a SQL query.
I have two tables apps and user_apps
apps table stores list of applications
user_apps store what all apllications an user has
These are the fields of apps table
app_id
app_name
These are the fields of user_apps table
id - autonumber
uid
app_id (contains app_id from apps table)
Issue
I cannot get the correct SQL query to get the list of applications an user(say uid = 1) DOES NOT have.
I tried joins but cannot get it done.
Hope its clear. Any help will be really helpful. Thanks in advance
Kiranvj
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I assume you have a users table also?
You could try this ...
SELECT
u.user_id
u.user_name,
a.app_id,
a.app_name
FROM
users u
inner join
user_apps ua on u.user_id = ua.uid
full outer join apps a on ua.app_id = a.app_id
WHERE
ua.app_id is null
The idea is for each user, attach all the known apps to the user and filter out those that are not null, leaving only the null user_apps rows.
Say there are 3 users and 3 apps and each user has the app of their id.
User 1 has app 1, user 2 has app 2 and user 3 has app 3.
The data would be ...
Users id/name
1 Richard
2 Sally
3 Jane
Apps
1 Word
2 Excel
3 Outlook
User_apps id/uid/app_id
1 1 1
2 2 2
3 3 3
So, the without the filter the output would be ...
user_id, user_name, app_id, app_name
1 Richard 1 Word
1 Richard 2 Excel
1 Richard 3 Outlook
2 Sally 1 Word
2 Sally 2 Excel
2 Sally 3 Outlook
3 Jane 1 Word
3 Jane 2 Excel
3 Jane 3 Outlook
With the filter we won't show where there IS a user <-> app relationship, so ...
user_id, user_name, app_id, app_name
1 Richard 2 Excel
1 Richard 3 Outlook
2 Sally 1 Word
2 Sally 3 Outlook
3 Jane 1 Word
3 Jane 2 Excel
I hope that's the result you want.
If you wanted to filter this further for a single user, then use ...
WHERE
ua.app_id is null and u.user_id = 1
sort of thing.
UNTESTED.
Regards,
Richard Quadling.
Business Accounts
Answer for Membership
by: pzozulkaPosted on 2008-07-19 at 12:35:03ID: 22043467
Try this:
Select user_apps.id, user_apps.uid, user_apps.app_id, apps.app_name
From user_apps UA LEFT OUTER JOIN apps A ON A.app_id = UA.app_id
WHERE UA.app_id NOT IN (Select app_id from apps)