w3resource

SQL Exercise: The date when he left his last job and his employee ID

SQL SORTING and FILTERING on HR Database: Exercise-32 with Solution

32. From the following table, write a SQL query to find those employees who have completed their previous jobs. Return employee ID, end_date.

Sample table : job_history


Sample Solution:

SELECT employee_id , MAX(end_date)
FROM job_history
WHERE employee_id IN (SELECT employee_id
FROM job_history
GROUP BY 1
HAVING COUNT(employee_id) > 1)
GROUP BY 1

Sample Output:

 employee_id |    max
-------------+------------
         101 | 2005-03-15
         200 | 2006-12-31
         176 | 2007-12-31
(7 rows)

Code Explanation:

The said query in SQL that retrieves the employee ID and the latest end date for each employee with multiple job history entries. The query first selects the employee IDs that have more than one entry in the 'job_history' table using a subquery and the "HAVING COUNT" clause. Then, it retrieves the employee IDs and their corresponding maximum end dates from the 'job_history' table, grouping the results by the employee IDs.

Practice Online


HR database model

Query Visualization:

Duration:

Query visualization of Display the employee ID and the date on which he ended his previous job - Duration

Rows:

Query visualization of Display the employee ID and the date on which he ended his previous job - Rows

Cost:

Query visualization of Display the employee ID and the date on which he ended his previous job - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Departments more than 10 employees who get commissions.
Next SQL Exercise: A department 50 employee without a commission %.

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.

SQL: Tips of the Day

Difference between natural join and inner join

One significant difference between INNER JOIN and NATURAL JOIN is the number of columns returned-

Consider:

TableA                           TableB
+------------+----------+        +--------------------+    
|Column1     | Column2  |        |Column1  |  Column3 |
+-----------------------+        +--------------------+
| 1          |  2       |        | 1       |   3      |
+------------+----------+        +---------+----------+

The INNER JOIN of TableA and TableB on Column1 will return

SELECT * FROM TableA AS a INNER JOIN TableB AS b USING (Column1);
SELECT * FROM TableA AS a INNER JOIN TableB AS b ON a.Column1 = b.Column1;
+------------+-----------+---------------------+    
| a.Column1  | a.Column2 | b.Column1| b.Column3|
+------------------------+---------------------+
| 1          |  2        | 1        |   3      |
+------------+-----------+----------+----------+

The NATURAL JOIN of TableA and TableB on Column1 will return:

SELECT * FROM TableA NATURAL JOIN TableB
+------------+----------+----------+    
|Column1     | Column2  | Column3  |
+-----------------------+----------+
| 1          |  2       |   3      |
+------------+----------+----------+

Ref: https://bit.ly/3AG5CId

 





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