MySQL Joins Exercises: Display the job history that were done by any employee who is currently drawing more than 10000 of salary
MySQL Joins: Exercise-12 with Solution
Write a MySQL query to display the job history that were done by any employee who is currently drawing more than 10000 of salary.
Sample table: employees
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | ............ | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Sample table: Job_history
employee_id start_date end_date job_id department_id ----------- ---------- ---------- ---------- ------------- employee_id start_date end_date` job_id department_id 102 1993-01-13 1998-07-24 IT_PROG 60 101 1989-09-21 1993-10-27 AC_ACCOUNT 110 101 1993-10-28 1997-03-15 AC_MGR 110 201 1996-02-17 1999-12-19 MK_REP 20 114 1998-03-24 1999-12-31 ST_CLERK 50 122 1999-01-01 1999-12-31 ST_CLERK 50 200 1987-09-17 1993-06-17 AD_ASST 90 176 1998-03-24 1998-12-31 SA_REP 80 176 1999-01-01 1999-12-31 SA_MAN 80 200 1994-07-01 1998-12-31 AC_ACCOUNT 90
Code:
-- This SQL query retrieves all columns from the 'job_history' table for employees whose salary is greater than 10000.
SELECT
jh.* -- Selecting all columns from the 'job_history' table.
FROM
job_history jh -- Specifying the 'job_history' table and aliasing it as 'jh'.
JOIN
employees e -- Performing a join with the 'employees' table and aliasing it as 'e'.
ON
(jh.employee_id = e.employee_id) -- Joining the 'job_history' and 'employees' tables based on the 'employee_id' column to associate job history with employees.
WHERE
salary > 10000; -- Filtering the result to include only records where the salary is greater than 10000.
Explanation:
- This SQL query retrieves all columns from the 'job_history' table for employees whose salary is greater than 10000.
- It performs an inner join between the 'job_history' and 'employees' tables based on the 'employee_id' column to associate job history with employees.
- The WHERE clause filters the result to include only records where the salary is greater than 10000.
Sample Output:
EMPLOYEE_ID START_DATE END_DATE JOB_ID DEPARTMENT_ID 102 1993-01-13T05:00:00.000Z 1998-07-24T04:00:00.000Z IT_PROG 60 101 1989-09-21T04:00:00.000Z 1993-10-27T04:00:00.000Z AC_ACCOUNT 110 101 1993-10-28T04:00:00.000Z 1997-03-15T05:00:00.000Z AC_MGR 110 201 1996-02-17T05:00:00.000Z 1999-12-19T05:00:00.000Z MK_REP 20 114 1998-03-24T05:00:00.000Z 1999-12-31T05:00:00.000Z ST_CLERK 50
Go to:
PREV :Write a MySQL query to display job title, employee name, and the difference between salary of the employee and minimum salary for the job.
NEXT :Write a MySQL query to display the first name, last name, hire date, salary of the manager for all managers whose experience is more than 15 years.
MySQL Code Editor:
Structure of 'hr' database :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
