SQL to find mutual friends from the same table [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to find the mutual friends with SQL from a single table
I have a simple table with 2 fields user, friends as int. Data values as are as below
Expected Output
user friend mutual friends
1 2 2
1 3 1
This is the query i tried
select ex1.user,ex2.friend, count(distinct ex3.friend) from (select distinct user from exer1) ex1
join exer1 ex2, exer1 ex3
where ex1.user=ex2.user and
ex2.user<>ex3.user and ex2.friend<>ex3.friend
group by ex1.user,ex2.friend order by 1,2
The output i got was
enter image description here
The desired output I am looking for is
enter image description here

You can use a self-join and aggregation:
select f1.user, f2.user, count(*)
from friends f1 join
friends f2
on f1.friend = f2.friend and
f1.user < f2.user
group by f1.user, f2.user;

Related

how do I select records that are missing a particular type of row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a payment table. Type 1 is new/first payment. Type 11 is renew payment. So each user should have one record of type 1. But due to bad coding, right now, some records have only renew records. I want to find all the records that are missing the type 1 record. In the example below, it should return user 3.
user type amount
1 1 10
1 11 10
2 1 10
3 11 10
How should I write the query?
One way to find all users in the table that don't have a record of Type 1 is a NOT EXISTS using a subquery, as follows:
SELECT p.[user]
FROM payment p
WHERE NOT EXISTS (
SELECT 1
FROM payment
WHERE `user` = p.[user]
AND type = 1)
Alternatively, this can be done with a LEFT JOIN and only selecting the non-matching rows:
SELECT p.[user]
FROM payment p
LEFT JOIN payment p2
ON p2.[user] = p.[user]
AND p2.type = 1
WHERE p2.[user] IS NULL

MYSQL how to increment column count if different combination row exist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
MYSQL how to increment column count if different combination row exist
for example if table have
count user certNo
0 a 001
0 b 886
if same user with different certNo comes then i need to increment count by 1,
like
count user certNo
0 a 001
0 b 886
1 a 673
For legacy MySQL version you can use next query:
select
t.*,
count(t1.certNo) as count
from test t
left join test t1 on t1.user = t.user and t1.certNo < t.certNo
group by t.user, t.certNo;
Test it on SQLize.online
You can use row number():
select row_number() over (partition by user order by certNo) - 1 as count,
user, certNo
from t;

sql statement to select all from another table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
lists table
id user_id list_name
1 1 test
2 1 test2
items table
id list_id item_name price_item item_checked
1 1 apple 2 0
2 2 orange 2 0
result should be
id item_name price_item item_checked
1 Apple 2 0
How would i accomplish to select all from list 1
Your question is not clear but I will try my best to answer it.
To get the results you have posted:
select *
from Items_Table
Title says select all from a different table. I assume lists is the first table and items is the "different" table you are referring to.
Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id
Now, lets assume you were looking for specific items, say where Id is 1 in the lists table. Then you would have the following:
Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id
where Lists.id = 1
You can replace Lists.id in the where statement with any other column and set it equal to value you are looking for.
I hope this was helpful. Please try to be a bit more clear about what you are looking for!
Good Luck!

SQL Display student record having maximum marks from other table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
student(sID,sNAME,sCLASS);
result(sID,subMARKS);
Actually, in MS-ACCESS, i am trying to this with equi join but getting wrong result. i'm writting my query like
SELECT stud.sID
, stud.sNAME
, stud.sCLASS
, result.sID
FROM student
, result
WHERE(SELECT MAX(subMARKS) FROM result)
It should display Ali record only because he is having maximum marks. but i am getting this kind of output as shown in picture below.
sID sNAME sCLASS
1 Ali BSC
2 Ahmad FSC
3 Asgar ICS
4 Akram BSC
SELECT T1.SID, T1.sname FROM student T1
LEFT JOIN resultT2 ON t1.sid=t2.sid
WHERE t2.submarks = (SELECT Max(submarks) FROM result);

select dependent on counting two columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this kind of table selected from database:
user project role
XXX p1 admin
XXX p2 developer
xxx p1 developer
yyy p3 tester
yyy p1 developer
yyy p1 admin
zzz p2 developer
and I need to add to this query filter: if user has at lease two roles in one project.
How can I do that? What MySQL functions can help me in this case?
GROUP BY project and add an HAVING clause to restrict to those with COUNT > 1
SELECT user, project, count(*) AS number_of_roles
FROM thetable
GROUP BY project
HAVING count(*) > 1
Bonus: a column with all roles aggregated
SELECT user, project, count(*) AS number_of_roles, GROUP_CONCAT(role) AS roles
FROM thetable
GROUP BY project
HAVING count(*) > 1