GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  SQL Plus
Go To First  |  Previous Question  |  Next Question 
 SQL Plus  |  Question 101 of 132    Print  
A table has following layout ; CITY, NAME , SEX
How do you write a query to get the count of Male, count of female in a given city XXX.Query result should be in a single row with count of male , count of female as columns?

  
Total Answers and Comments: 18 Last Update: September 29, 2008     Asked by: shireen 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
March 20, 2006 06:20:54   #1  
prafull.vn Member Since: March 2006   Contribution: 10    

RE: A table has following layout ; CITY, NAME , SEX

select emp1.city emp1.male male emp2.female female from
(select city count(sex) as male from emp where sex 'M' group by city) emp1
(select city count(sex) as female from emp where sex 'F' group by city) emp2
where emp1.city emp2.city
and emp1.city 'NGP'

THIS QUERY WILL NOT WORK IF THERE IS NO MALE RECORD OR FEMALE RECORD .. ie there should be at least one male and one female in one city ..

can anybody has other option ... like if in City NGP there are males only then ... Output should be .. NGP 10 0 ... males 10 and NO females..or vice versa NGP 0 10
..........???


 
Is this answer useful? Yes | No
March 23, 2006 12:24:12   #2  
padma        

RE: A table has following layout ; CITY, NAME , SEX
select count(decode(job 'MANAGER' 1)) mgr
count(decode(job 'SALESMAN' 1)) salman from emp where to_char(hiredate 'yy') '81'

 
Is this answer useful? Yes | No
March 23, 2006 23:20:29   #3  
prafull.vn Member Since: March 2006   Contribution: 10    

RE: A table has following layout ; CITY, NAME , SEX
thankx Padma .. So sherren .. here its ur query..

select count(decode(sex 'M' 1)) Males count(decode(sex 'F' 1)) Females from emp where city 'NGP';


 
Is this answer useful? Yes | No
March 29, 2006 14:03:56   #4  
Nabil Essa        

RE: A table has following layout ; CITY, NAME , SEX
select count(*) 'Male' from tablewhere city 'xxx'and sex 'M'union select count(*) 'Femal' from tablewnere city 'xxx'and sex 'F'
 
Is this answer useful? Yes | No
March 31, 2006 00:15:56   #5  
Ashutosh        

RE: A table has following layout ; CITY, NAME , SEX

select (Select count(*) From tab where City 'XXX' and Sex 'Male') as Male_Cnt
(Select count(*) From tab where City 'XXX' and Sex 'Male') as Female_Cnt
From Dual

This will wrk too.


 
Is this answer useful? Yes | No
April 06, 2006 04:39:01   #6  
Shaguna        

RE: A table has following layout ; CITY, NAME , SEX

select City sum(decode(sex 'M' 1 0)) Male sum(decode(sex 'F' 1 0)) Female from <table_name> group by city


 
Is this answer useful? Yes | No
April 27, 2006 00:54:52   #7  
tarun1282 Member Since: April 2006   Contribution: 11    

RE: A table has following layout ; CITY, NAME , SEX
select count(male.name) as malecount count(female.name) as femalecount from (select name from table where sex 'm') male (select name from table where sex 'f') female where city 'XXX';
 
Is this answer useful? Yes | No
June 14, 2006 05:46:03   #8  
Pushpendu        

RE: A table has following layout ; CITY, NAME , SEX

I think prafull's approach is fine.Another simple way of doing it is

SELECT CITY COUNT(MALE) COUNT(FEMALE)
FROM
( SELECT CITY
case WHEN SEX 'M' THEN 'M' ELSE NULL END MALE
case WHEN SEX 'F' THEN 'F' ELSE NULL END FEMALE
FROM TAB_PT
)
GROUP BY CITY


 
Is this answer useful? Yes | No
July 13, 2006 13:38:45   #9  
Adt        

RE: A table has following layout ; CITY, NAME , SEX

select sum(male) male sum(female) female

from(

select count(sex) Male null Female

from test_table

where sex 'M'

union

select null Male count('SEX') Female

from test_table

where sex 'F'

group by sex)


 
Is this answer useful? Yes | No
July 21, 2006 06:28:57   #10  
Shiv Mangal Rahi        

RE: A table has following layout ; CITY, NAME , SEX
declare
v_totalmale binary_integer;
v_totalfemale binary_integer;
begin
select count(sex) into v_totalmale from test where city 'Delhi' and sex 'Male';
select count(sex) into v_totalfemale from test where city 'Dlehi' and sex 'Female';
dbms_output.put_line(' TotalMale' || ' ' || 'TotalFemale');
dbms_output.put_line(' ' || v_totalmale || ' '|| v_totalfemale);
end;

 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape