| Author |
Topic |
|
Cat84
Starting Member
4 Posts |
Posted - 2011-11-30 : 07:19:41
|
| Hello everyone,I have a question and i was hoping you could help meI have 2 tables, User: UserId, UserName, and Images: ImageId, UserId. Each user can have multiple images (like in facebook), and there are users that don't have any image at all. I need to return those, but not by using NOT IN or EXISTS, but with some kind of join.For whatever reason i can't resolve this.. Thanks in advance:) |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-30 : 07:27:44
|
| Why do you have to use a join?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
sureshkk
Starting Member
21 Posts |
Posted - 2011-11-30 : 07:35:54
|
| DECLARE @Users TABLE( UserID INT, UserName VARCHAR(100))DECLARE @Images TABLE( ImageID INT, UserID INT) INSERT INTO @UsersSELECT 1,'User1'UNION ALLSELECT 2,'User2'UNION ALLSELECT 3,'User3'UNION ALLSELECT 4,'USer4'INSERT INTO @ImagesSELECT 1,1UNION ALLSELECT 2,1UNION ALLSELECT 3,2UNION ALLSELECT 4,4SELECT * FROM @UsersSELECT * FROM @ImagesSELECT u.* FROM @Users u LEFT JOIN @Images iON u.UserID=i.UserIDWHERE i.UserID IS NULL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-30 : 10:51:48
|
quote: Originally posted by Cat84 Hello everyone,I have a question and i was hoping you could help meI have 2 tables, User: UserId, UserName, and Images: ImageId, UserId. Each user can have multiple images (like in facebook), and there are users that don't have any image at all. I need to return those, but not by using NOT IN or EXISTS, but with some kind of join.For whatever reason i can't resolve this.. Thanks in advance:)
why is it that you dont want to use NOT IN or EXISTS?is it a special instruction from your teacher? or is the concepts yet to be covered?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Cat84
Starting Member
4 Posts |
Posted - 2011-11-30 : 11:29:31
|
| I was asked this question by an interviewer over the phone, and immediately answered NOT IN, but he wanted other ways, and specifically join.sureshkk, thank you so much! That's the solution.. and i told him left join, and he asked "and what else?.." and i just froze, i forgot the "is null" part..Looks like i'm not going to get the interview but at least now i know:) |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-30 : 11:57:36
|
| You can also use EXCEPT which is closer to relational theory - that could have been what they were looking for.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Cat84
Starting Member
4 Posts |
Posted - 2011-11-30 : 13:35:43
|
| X002548, your query doesn't work. I didn't understand what are you counting? It returns all usersI guess there are a lot of ways to solve this, but he particularly asked for join |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-30 : 13:50:01
|
| S.b.select User_IDfrom(SELECT User_Id FROM UserUNION ALLSELECT User_Id FROM Images) aGROUP BY User_IdHAVING COUNT(*) = 1==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-30 : 13:51:09
|
quote: Originally posted by X002548
quote: Originally posted by nigelrivett You can also use EXCEPT which is closer to relational theory - that could have been what they were looking for.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
But isn't that in reality a join under the covers?
It's a type of join and that is what is wanted.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-11-30 : 14:40:30
|
quote: Originally posted by nigelrivett S.b.select User_IDfrom(SELECT User_Id FROM UserUNION ALLSELECT User_Id FROM Images) aGROUP BY User_IdHAVING COUNT(*) = 1==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
That's me just being sloppy...I just used this to tests a count of rows loaded to table against a single row loaded control fileI believe this might be the fastest method thoughBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
|