SQL Query to Find Number of Employees According to Gender Whose DOB is Between a Given Range
Last Updated :
29 Mar, 2023
Query in SQL is like a statement that performs a task. Here, we need to write a query that will find the number of employees according to gender whose DOB is in the given range.
We will first create a database named “geeks” then we will create a table “department” in that database.
Creating a Database :
Use the below SQL statement to create a database called geeks:
CREATE DATABASE geeks;
Using Database :
USE geeks;
Table Definition:
We have the following department table in our geeks database :
CREATE TABLE department(
ID int,
NAME Varchar(20),
Gender Varchar(5),
DateOfBirth Date);
You can use the below statement to query the description of the created table:
EXEC sp_columns department;
Adding Data to Table:
The date data type uses the format ‘YYYY-MM-DD‘. Use the below statement to add data to the department table:
INSERT INTO department VALUES (1,'Neha','F','1994-06-03');
INSERT INTO department VALUES (2,'Harsh','M','1996-03-12');
INSERT INTO department VALUES (3,'Harsh','M','1995-05-01');
INSERT INTO department VALUES (4,'Rupali','F','1996-11-11');
INSERT INTO department VALUES (5,'Rohan','M','1992-03-08');
To verify the contents of the table use the below statement:
SELECT * FROM department
Get the number of employees according to their gender whose DOB is between a given range. Here, we will assume the DOB range to be from 1995-01-01 to 1996-12-31.
Query:
Now we will use the below syntax to query for the number of employees according to gender whose DOB is between a given range:
Syntax:
SELECT column_name1, count(column_name1) FROM table_name
WHERE column_name2 between value1 and value2 GROUP BY column_name1;
So the query for our table goes as shown below:
SELECT Gender,count(Gender) FROM department
WHERE DateOfBirth between '1995-01-01' and '1996-12-31'
GROUP BY gender;
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...