Get the older age persons in sql server?

Posted by Mandlaa under Sql Server on 5/18/2015 | Points: 10 | Views : 421 | Status : [Member] | Replies : 7
HI,
I have two tables like male,female

Male Table:
Id Name Age
1 aaa 20
2 bbb 30
3 ccc 23

Female table
Id Name Age
1 sss 78
2 fff 32
3 ggg 14

NO i want to get old age persons using both tables
Can you give me the query




Responses

Posted by: Jayakumars on: 5/18/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi
Try this code
--Create Table MaleTable
--Create Table FemaleTable
--(
--Id int primary key identity(1,1),
--Name varchar(40),
--age int
--)

--Insert into MaleTable values('aaa',20)
--Insert into MaleTable values('bbb',30)
--Insert into MaleTable values('ccc',23)


--Insert into FemaleTable values('sss',78)
--Insert into FemaleTable values('fff',32)
--Insert into FemaleTable values('ggg',14)

Answer is
============

Select name,age from MaleTable where age=(Select min(age) from MaleTable) --20
Select name,age from FemaleTable where age=(Select min(age) from FemaleTable) --14

Mark as Answer if its helpful to you

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 5/18/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi

If you need merge the data means use this query

Select name,age from MaleTable where age=(Select min(age) from MaleTable) --20
union all
Select name,age from FemaleTable where age=(Select min(age) from FemaleTable) --14

Mark as Answer if its helpful to you

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 5/19/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down

--Old age person from Male table
SELECT top 1 * FROM MaleTable Order by age desc

--Old age person from Female table
SELECT top 1 * FROM FeMaleTable Order by age desc


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 5/19/2015 [Member] Starter | Points: 25

Up
0
Down
using both tables get the value


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 5/19/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi
mandala
this code using both tables get the values try and let me know.

try this code


Select name,age from MaleTable where age=(Select min(age) from MaleTable) --20
union all
Select name,age from FemaleTable where age=(Select min(age) from FemaleTable) --14



Mark as Answer if its helpful to you

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 5/19/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down
-- If you want to display olf age persons from both tables together 
SELECT top 2 * FROM (
SELECT * FROM MaleTable
union all
SELECT * FROM FeMaleTable
) TempTable
ORDER BY age DESC

Output:
Id Name age
1 sss 78
2 fff 32

--if you want old age Male and Old age Female
SELECT * FROM MaleTable WHERE Age = ( SELECT MAX(age) FROM MaleTable) -- old age means maximum aged person
union all
SELECT * FROM FeMaleTable WHERE Age = ( SELECT MAX(age) FROM FeMaleTable)

Output:
Id Name age
2 bbb 30
1 sss 78


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 5/20/2015 [Member] Bronze | Points: 25

Up
0
Down
Good Deeds... We got many Concepts here

Feel free to share informations 9731764134
Thanks

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response