SQL Exercise: Employees joined with a as the 2nd character of a month
SQL employee Database: Exercise-108 with Solution
[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.
SQL: Tips of the Day
Concatenate strings of a string field in a PostgreSQL 'group by' query:
Input:
ID COMPANY_ID EMPLOYEE 1 1 Anna 2 1 Bill 3 2 Carol 4 2 Dave
SELECT company_id, string_agg(employee, ', ') FROM mytable GROUP BY company_id;
Output:
COMPANY_ID EMPLOYEE 1 Anna, Bill 2 Carol, Dave
Database: PostgreSQL
Ref: https://bit.ly/2XTiRjq
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook