w3resource

PostgreSQL Restricting and Sorting Data: Display the name and hire date for those who were hired in 1987


4. Write a query to display the name, including first_name and last_name and hire date for all employees who were hired in 1987.

Sample Solution:

Code:

SELECT first_name, last_name, hire_date 
FROM employees 
WHERE TO_CHAR(hire_date, 'YYYY')  LIKE '%87';

Sample table: employees


Output:

pg_exercises=# SELECT first_name, last_name, hire_date
pg_exercises-# FROM employees
pg_exercises-# WHERE TO_CHAR(hire_date, 'YYYY')  LIKE '%87';
 first_name  |  last_name  | hire_date
-------------+-------------+------------
 Steven      | King        | 1987-06-17
 Neena       | Kochhar     | 1987-06-18
 Lex         | De Haan     | 1987-06-19
 Alexander   | Hunold      | 1987-06-20
 Bruce       | Ernst       | 1987-06-21
 David       | Austin      | 1987-06-22
 Valli       | Pataballa   | 1987-06-23
 Diana       | Lorentz     | 1987-06-24
 ...
 Guy         | Himuro      | 1987-07-05
 Karen       | Colmenares  | 1987-07-06
 Matthew     | Weiss       | 1987-07-07
 Adam        | Fripp       | 1987-07-08
 Payam       | Kaufling    | 1987-07-09
 Shanta      | Vollman     | 1987-07-10
 ...
 TJ          | Olson       | 1987-07-19
 Jason       | Mallin      | 1987-07-20
 Michael     | Rogers      | 1987-07-21
 Ki          | Gee         | 1987-07-22
 Hazel       | Philtanker  | 1987-07-23
 Renske      | Ladwig      | 1987-07-24
 Stephen     | Stiles      | 1987-07-25
 John        | Seo         | 1987-07-26
 Joshua      | Patel       | 1987-07-27
 Trenna      | Rajs        | 1987-07-28
 ...
 Hermann     | Baer        | 1987-09-29
 Shelley     | Higgins     | 1987-09-30
 William     | Gietz       | 1987-10-01
(106 rows)

Practice Online


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

Previous: Write a query to display the name, including first_name and last_name, and salary who works in the department either 30 or 100 and salary is out of the range between $10,000 and $15,000.
Next: Write a query to get the first name of the employee who holds the letter 'c' and 'e' in the first name.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.