w3resource

SQL Exercise: Employees, joined in a given month and salary range

SQL employee Database: Exercise-50 with Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

50. From the following table, write a SQL query to identify employees who joined in the month of FEBRUARY with a salary range of 1001 to 2000 (Begin and end values are included.). Return complete information about the employees.

Sample table: employees


Pictorial Presentation:

SQL exercises on employee Database: List the employees, joined in the month FEBRUARY with a salary range between 1001 to 2000

Sample Solution:

SELECT *
FROM employees
WHERE to_char(hire_date,'MON') = 'FEB'
  AND salary BETWEEN 1000 AND 2000;

Sample Output:

 emp_id | emp_name | job_name | manager_id | hire_date  | salary  | commission | dep_id
--------+----------+----------+------------+------------+---------+------------+--------
  64989 | ADELYN   | SALESMAN |      66928 | 1991-02-20 | 1700.00 |     400.00 |   3001
  65271 | WADE     | SALESMAN |      66928 | 1991-02-22 | 1350.00 |     600.00 |   3001
(2 rows)

Explanation:

The said query in SQL that selects all columns from the employees table where the hire_date month is February and the salary falls between 1000 and 2000.

The hire_date column is first converted to a character string in the abbreviated month format ('MON') using the to_char function. The resulting string is then compared to the string 'FEB'.

The BETWEEN operator is used to specify a range of values that includes both the lower and upper bounds.

The WHERE clause filters the rows from the employees table that the month of the hire date must be February and the salary of the employee must be between 1000 and 2000.

Practice Online


Sample Database: employee

employee database structure

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: List the managers of department 1001 or 2001.
Next SQL Exercise: List all the employees who joined before or after 1991.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.