SQL Exercise: Employees joined with a as the 2nd character of a month
[An editor is available at the bottom of the page to write and execute the scripts.]
108. From the following table, write a SQL query to find those employees who joined in the month of where the second letter is 'a'. Return number of employees.
Sample table: employees
Sample Solution:
SELECT *
FROM employees
WHERE to_char(hire_date,'mon') LIKE '_a%';
Sample Output:
emp_id | emp_name | job_name | manager_id | hire_date | salary | commission | dep_id --------+----------+----------+------------+------------+---------+------------+-------- 66928 | BLAZE | MANAGER | 68319 | 1991-05-01 | 2750.00 | | 3001 68736 | ADNRES | CLERK | 67858 | 1997-05-23 | 1200.00 | | 2001 69324 | MARKER | CLERK | 67832 | 1992-01-23 | 1400.00 | | 1001 (3 rows)
Explanation:
The given query in SQL that selects all the columns and rows from the table 'employees' where the hire date has a month that starts with the letter "a".
The function TO_CHAR() converts the date in the "hire_date" column to a string representation of the month, and the LIKE operator with the pattern 'a%' matches any string that has a second character that is the letter "a". The underscore () represents any single character.
Practice Online
Sample Database: employee
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Employee start with 'A' and end with 'N', length six.
Next SQL Exercise: List the employees whose names contain AR together.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics