Sql not exists and not in statements

121 views Asked by At

Good day guys. For this problem i need to get the user1 from table "follows" that follows user2 if user2 follows user1. It's kinda tricky. I did it with not exists and not in but I get all the names from user1 column.

For example:

Andreas follows Katerina but Katerina doesn't follow Andreas. So Andreas is not in what I want. But Aris follows Anna and Anna follows Aris. So we get Aris as result.

The thing is that I don't get such results and I get all the names.

Follows table:

                           user1_name    user2_name
insert into follows values('Andreas',   'Katerina');
insert into follows values('Aris',  'Andreas');
insert into follows values('Aris',  'Anna');
insert into follows values('Aris',  'Kostas');
insert into follows values('Aris',  'Panos');
insert into follows values('Aris',  'Yannis');
insert into follows values('Anna'   ,'Aris');
insert into follows values('Anna',  'Maria');
insert into follows values('Anna',  'Panos' );
insert into follows values('Dimitris',  'Maria');
insert into follows values('Dimitris',  'Anna');
insert into follows values('Kostas' ,'Andreas');
insert into follows values('Kostas',    'Panos');
insert into follows values('Kostas',    'Katerina');
insert into follows values('Maria', 'Yannis');
insert into follows values('Maria', 'Kostas');
insert into follows values('Maria', 'Anna');
insert into follows values('Maria', 'Aris');
insert into follows values('Maria', 'Panos');
insert into follows values('Panos','Andreas');
insert into follows values('Panos', 'Aris');
insert into follows values('Petros',    'Andreas');
insert into follows values('Yannis',    'Aris');
insert into follows values('Yannis',    'Andreas');

User information table:

insert into user_inf values('Andreas',  'Martiou 25',   '1990-12-02');
insert into user_inf values('Aris', 'Papandreou 10',    '1987-03-12');
insert into user_inf values('Anna', 'Aiakidon 20',  '1989-07-15');
insert into user_inf values('Dimitris', 'Dodonis 3',    '1992-09-07');
insert into user_inf values('Katerina', '28 Oktobriou 4',   '1993-01-09');
insert into user_inf values('Kostas','Kasioumi 3',  '1992-12-12');
insert into user_inf values('Maria',    'Kalari 8', '1993-08-31');
insert into user_inf values('Petros',   'Panepistimiou 9',  '1992-04-15');
insert into user_inf values('Panos',    'Aneksartisias 13', '1991-05-27');
insert into user_inf values('Yannis',   'Ithakis 20',   '1993-07-03');

The results I want to get are the following:

user1
------
Aris
Aris
Aris(because 3 of the people that he follows, follow him too)
Anna
Maria
Panos
Yannis

My code:

/*  not exists.*/
select distinct Fol.user1_name
from follows as Fol,user_inf as Usr
where not exists
                        (select distinct F.user2_name,F.user1_name
                          from follows as F, user_inf as U
                          where Fol.user2_name = F.user1_name AND Fol.user1_name = F.user2_name AND F.user1_name = Usr.name AND Fol.user1_name = U.name );

/*  not in.*/
select distinct Fol.user2_name
from follows as Fol,user_inf as Usr
where Fol.user1_name not in
                        (select distinct F.user1_name,F.user2_name
                          from follows as F, user_inf as U
                          where Fol.user2_name = F.user1_name AND Fol.user1_name <> F.user2_name AND F.user1_name = Usr.name AND Fol.user2_name <> U.name);

Thanks in advance!

2

There are 2 answers

1
Gordon Linoff On

If you want pairs that following each other, how about:

select f1.*
from follows f1 join
     follows f2
     on f1.user1_name = f2.user2_name and
        f1.user2_name = f1.user1_name and
        f1.user1_name < f1.user2_name;

The last condition just removes duplicates, so a pair of names only appears once in the result set.

0
xQbert On

What about a self join?

This should result in a dataset whereby the users follow each other and the user information of the source user.

SELECT F.user1_name, F.user2_name, u.*
FROM follows F
INNER JOIN follows F2
  on F.user2_name = F2.User1_name
INNER JOIn user_inf U
  on F.user1.name U.name

Think of relational data in terms of sets you want the intersection of data where set 1 and set to are followers of eachother.

I'm making the assumption that people don't follow themselves. user1_name is unique

Now if you need to remove the set so that relationship is only listed once... add to the first inner join

on F.user1_name > F.user1_name