PostgreSQL JOINS: Make a join with two tables job_history and employees to display the status of employees who is currently drawing the salary above 10000
12. Write a query to make a join with two tables job_history and employees to display the status of employees who is currently drawing the salary above 10000.
Sample Solution:
Code:
-- This SQL query retrieves job history records for employees whose salary is greater than 10000, by joining the job_history and employees tables.
SELECT jh.* -- Selects all columns from the job_history table
FROM job_history jh -- Specifies the first table from which to retrieve data, in this case, the job_history table, aliasing it as 'jh'
JOIN employees em -- Joins the job_history table with the employees table, specifying the second table and aliasing it as 'em'
ON (jh.employee_id = em.employee_id) -- Specifies the join condition based on the employee_id column
WHERE em.salary > 10000; -- Filters the rows to include only those where the salary of the employee is greater than 10000
Explanation:
- This SQL query retrieves job history records for employees whose salary is greater than 10000 by joining the job_history and employees tables.
- The SELECT statement selects all columns from the job_history table.
- The FROM clause specifies the first table from which to retrieve data, which is the job_history table, aliased as 'jh'.
- A JOIN operation is performed with the employees table, specifying the second table and aliasing it as 'em'.
- The ON clause specifies the join condition where the employee_id in the job_history table matches the employee_id in the employees table.
- The WHERE clause filters the rows to include only those where the salary of the employee is greater than 10000.
Sample table: employees
Sample table: Job_history
Output:
pg_exercises=# SELECT jh.* FROM job_history jh pg_exercises-# JOIN employees em pg_exercises-# ON (jh.employee_id = em.employee_id) pg_exercises-# WHERE em.salary > 10000; employee_id | start_date | end_date | job_id | department_id -------------+------------+------------+------------+--------------- 114 | 1998-03-24 | 1999-12-31 | ST_CLERK | 50 101 | 1993-10-28 | 1997-03-15 | AC_MGR | 110 101 | 1989-09-21 | 1993-10-27 | AC_ACCOUNT | 110 102 | 1993-01-13 | 1998-07-24 | IT_PROG | 60 201 | 1996-02-17 | 1999-12-19 | MK_REP | 20 (5 rows)
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a query to make a join with two tables employees and jobs to display the job title, employee name, and the difference between salary and the minimum salary of the employees.
Next: Write a query to make a join with two tables employees and departments to display department name, first_name and last_name, hire date and salary for all the managers who achieved a working experience is more than 15 years.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/postgresql-exercises/join/postgresql-join-exercise-12.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics