This is my table :
id name age role
---------------------------------------
1 Rahul 20 1
2 Karan 35 0
3 Vivek 47 0
4 Shubham 40 1
5 Paras 87 1
6 Arjun 18 1
7 Ayush 28 0
I want to select only those two rows which are having the sum of their ages equal to 60 like we have
1st and 4th row in above table. What is the SQL query for that??
I have tried this
select * from form_data where age=(select age from form_data having sum(age)=60);
3
Answers
One way to do it is to treat the same table as a second table and join them
This will give the two names and the two ages that match. However this will also give you Rahul+Shubam and Shubam+Rahul.
One way of getting around that is to use the id
This is a use case for a
CROSS JOIN
to get every combination of rows from 2 tables, using theform_data
table twice:Using that table:
I’ve returned the data with thit query: