How can I find the percentage of my users whose birth date is between 1980 and 1996 in MySQL?


Let us first create a table −

mysql> create table DemoTable
   -> (
   -> DateOfBirth varchar(100)
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019/01/31');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('1980/02/01');
Query OK, 1 row affected (0.25 sec)

mysql> insert into DemoTable values('1985/04/10');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('1995/06/04');
Query OK, 1 row affected (0.28 sec)

mysql> insert into DemoTable values('1990/12/24');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('1996/11/04');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+-------------+
| DateOfBirth |
+-------------+
| 2019/01/31  |
| 1980/02/01  |
| 1985/04/10  |
| 1995/06/04  |
| 1990/12/24  |
| 1996/11/04  |
+-------------+
6 rows in set (0.00 sec)

Following is the query to find the percentage of users with birth date between 1980 and 1996 −

mysql> SELECT Sum(DateOfBirth BETWEEN '1980/01/01' AND '1996/12/31') * 100 / Count(*) as Percentage from DemoTable;

Output

+------------+
| Percentage |
+------------+
| 83.3333    |
+------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements