In SQL, which query correctly finds the number of employees grouped by gender whose date of birth is between 1 January 1960 and 31 December 1975, inclusive?

Difficulty: Medium

Correct Answer: SELECT gender, COUNT(*) FROM employees WHERE dob BETWEEN '1960-01-01' AND '1975-12-31' GROUP BY gender;

Explanation:


Introduction / Context:
Grouping and filtering are core skills in SQL. Many business reports need counts of employees by categories such as gender or department, filtered by conditions like date of birth ranges. This question checks your ability to combine a WHERE clause with a BETWEEN condition on dates and a GROUP BY clause on gender. Understanding the correct ordering of clauses and the meaning of BETWEEN is essential for writing accurate queries.


Given Data / Assumptions:

  • There is an employees table with at least two columns: gender and dob (date of birth).
  • Dates are stored in a standard format such as YYYY-MM-DD.
  • The requirement is to include employees born from 1 January 1960 to 31 December 1975, both dates inclusive.


Concept / Approach:
The BETWEEN operator in SQL is inclusive at both ends, so dob BETWEEN '1960-01-01' AND '1975-12-31' selects all rows where dob is on or between these dates. To count employees by gender, we select gender and COUNT(*) and then group by gender so that one row is produced per gender with the corresponding count. The correct query therefore needs SELECT gender, COUNT(*), a WHERE clause with the correct BETWEEN condition and a GROUP BY gender clause.


Step-by-Step Solution:
Step 1: Identify the required columns in the result: gender and count of employees per gender.Step 2: Form the WHERE condition to restrict dob to the inclusive range between 1960-01-01 and 1975-12-31.Step 3: Add GROUP BY gender so that counts are calculated separately for each gender value.Step 4: Option A writes exactly this: SELECT gender, COUNT(*) FROM employees WHERE dob BETWEEN '1960-01-01' AND '1975-12-31' GROUP BY gender;.Step 5: Options B, C and D misuse the date conditions or omit the grouping and counting logic, so option A is correct.


Verification / Alternative check:
To verify, imagine a small sample table with a few rows and manually apply the condition. Any employee born in 1965 should be included, and the same for 1970 or exactly on 1960-01-01 or 1975-12-31. The BETWEEN clause in option A includes those boundary dates. Option B reverses the range and uses strict inequalities, which will select no rows. Option C only includes two exact dates, ignoring the full interval. Option D does not count employees, it just returns multiple rows containing gender values.


Why Other Options Are Wrong:
Option B uses dob greater than 1975-12-31 and less than 1960-01-01 in the same condition, which can never be true for any row, and therefore returns zero results. Option C restricts the selection to employees whose dob matches exactly the two dates, not the full range. Option D selects gender only and does not perform any aggregation, so it does not satisfy the requirement of counting employees by gender.


Common Pitfalls:
Common mistakes include forgetting the GROUP BY clause when using aggregate functions, reversing the order of dates in BETWEEN, or misunderstanding that BETWEEN is inclusive. Some developers also hard code dates in formats that do not match the database settings, causing unexpected results. For exam and interview questions, always write dates in a clear format like YYYY-MM-DD and double check that the WHERE condition matches the verbal requirement.


Final Answer:
SELECT gender, COUNT(*) FROM employees WHERE dob BETWEEN '1960-01-01' AND '1975-12-31' GROUP BY gender;

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion