Answer:
The sql query is given below.
Since we need to count of males and females for a particular blood group, we put xxx for the blood group.
SELECT COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "M%") as Male_Donors,
COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "F%") as Female_Donors
FROM DONOR
WHERE BG = xxx;
Explanation:
The clauses in the query are as follows.
1. SELECT: all the columns required in the output are put in this clause.
2. FROM JOIN ON: the table(s) from which the above columns are taken are put in this clause.
3. WHERE: any condition required to filter the output is put in this clause.
The query is explained below.
1. Find the number of male donors. Number of anything can be found using COUNT() function. A query is required since gender is included in deciding the type of donor.
2. The query is defined to find number of male donors as follows.
COUNT( SELECT DID FROM DONOR WHERE GENDER LIKE "M%"; )
3. In the previous query, LIKE operator is used since it is not defined what value is stored for male donors.
4. Similarly, the query to find the number of female donors is formed.
COUNT( SELECT DID FROM DONOR WHERE GENDER LIKE "F%"; )
5. Next, the final query is formed as follows.
SELECT: both COUNT() functions will come here.
FROM: table name
WHERE: specific blood group will be put here
GROUP BY: this clause is optional and is not used in this query.
HAVING: this clause is optional and is not used in this query.
6. The query after putting all clauses is shown below.
SELECT COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "M%"),
COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "F%")
FROM DONOR
WHERE BG = xxx;
7. Alias is used in the above query for each column to get the final query.
SELECT COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "M%") as Male_Donors, COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "F%") as Female_Donors
FROM DONOR
WHERE BG = xxx;