1. First let me know, whether the output is like this,-
#Option :- 1
Initial Table:- (Table: 1)
Roll Name Gender Age
XXXXXXX XXXXXXX F XX
XXXXXXXX XXXXXXX M XX
The Desired Result:- (Table: 2)
Roll Name Male Female Age
XXXXX XXXXXX NULL F XX
XXXXX XXXXXX M NULL XX
The type of output that U want is converting the table into its “DENORMALISED” state, which is not
desired as U may face problem while writing other queries based upon the “Gender” criteria.
.................................................................................................................................................................
#Option:- 2
To check the Table from being Denormalised but at the same time the Gender column is sorted, i.e.
either U’ll get in Ur table first all the Female students details, and then the Male student details or
vice versa.
Example:
Table: 3
Roll Name Gender Age
XXXXXXX XXXXXXX F XX
XXXXXXXX XXXXXXX M XX
M
F
F
Table: 4
Roll Name Gender Age
XXXXXXX XXXXXXX F XX
XXXXXXXX XXXXXXX F XX
F
M
M
Then the SQL query for this desired output is,-
2. SELECT * FROM Table 1 where gender=’F’
UNION ALL
SELECT * FROM Table 1 where gender=’M’
.................................................................................................................................................................
#Option:- 3
From Table: 1 if U want to create a table containing details of students who are male then the SQL
query is,- ( I guess U know dis query)
Select *
From Table 1
Where gender=’M’
........................................................................................................................................................
#Option:- 4
Initial Table: (Table: 5)
student gender class result
------- ------ ----- ------
Bob M Math A+
Mary F Math A+
Peter M Math A+
Jane F Math B
Desired Output: (Table: 6)
males females class result
----- ------- ----- ------
2 1 Math A+
0 1 Math B
The code for this sort of result is,-
SELECT CLASS, RESULT,
SUM(CASE WHEN GENDER ='M' THEN 1 ELSE 0 END) MALE ,
SUM(CASE WHEN GENDER ='F' THEN 1 ELSE 0 END) FEMALE
FROM TABLE 5
GROUP BY CLASS,RESULT
..................................................................................................................................................................................