Question: oracle trying to order employees been company for at least 15 years ordered by month

```
SELECT TO_CHAR(hire_date, 'Month') AS "Month", COUNT (*) AS "Number of Hires" 
FROM EMPLOYEES 
WHERE TO_CHAR(SYSDATE,'YYYY') - TO_CHAR(HIRE_DATE,'YYYY') >14
GROUP BY TO_CHAR(hire_date,'Month')
ORDER BY "Month";

```

displays this

```
Month                                Number of Hires
------------------------------------ ---------------
April                                              7
August                                             9
December                                           7
February                                          13
January                                           14
July                                               7
June                                              11
March                                             17
May                                                6
November                                           5
October                                            6

Month                                Number of Hires
------------------------------------ ---------------
September

```

O wanted orders as

```
January
February
March

```

Answer: 
The problem is that `'Month'` is a string -- not good for ordering, well, months.

```
SELECT TO_CHAR(hire_date, 'Month') AS "Month", COUNT(*) AS "Number of Hires" 
FROM EMPLOYEES 
WHERE EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM HIRE_DATE) > 14
GROUP BY TO_CHAR(hire_date, 'Month')
ORDER BY EXTRACT(month FROM MIN(hire_date));

```

Note that you should not be using arithmetic on strings -- that simply isn't an operation for strings and you want to avoid implicit conversion (because it can cause unexpected problems). So, use the built-in functions to extract the year.

Note that your logic doesn't really get "employees been company for at least 15 years". That woudl be:

```
WHERE hire_date < sysdate - interval '15' year

```
The query can be re-phrased as under to get the desired results:

```
select a.* from (
SELECT TO_CHAR(hire_date, 'Month') AS month, COUNT (*) AS "Number of Hires" 
FROM EMPLOYEES 
WHERE TO_CHAR(SYSDATE,'YYYY') - TO_CHAR(HIRE_DATE,'YYYY') >14
GROUP BY TO_CHAR(hire_date,'Month')) a
ORDER BY to_date('01-'||month||'-2018','DD-Month-YYYY');

```