w3resource

SQL Subquery Exercises: Display the details of the current job for those employees who worked as a Sales Representative in the past

SQL SUBQUERY: Exercise-51 with Solution

From the following table, write a SQL query to find employees who have previously worked as 'Sales Representatives'. Return all the fields of jobs.

Sample table: jobs


Sample table: employees


Sample table: job_history


Sample Solution:

SELECT * 
	FROM jobs 
		WHERE job_id IN 
		(SELECT job_id 
			FROM employees 
				WHERE employee_id IN 
        (SELECT employee_id 
			FROM job_history 
				WHERE job_id='SA_REP'));

Sample Output:

job_id	job_title		min_salary	max_salary
SA_REP	Sales Representative	6000		12000

Pictorial Presentation:

SQL Subqueries Exercises: Display the details of the current job for those employees who worked as a Sales Representative in the past.

Practice Online


Query Visualization:

Duration:

Query visualization of Display the details of the current job for those employees who worked as a Sales Representative in the past - Duration

Rows:

Query visualization of Display the details of the current job for those employees who worked as a Sales Representative in the past - Rows

Cost:

Query visualization of Display the details of the current job for those employees who worked as a Sales Representative in the past - Cost

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

Previous: From the following table, write a SQL query to find those managers who supervise four or more employees. Return manager name, department ID.
Next: From the following table, write a SQL query to find those employees who earn second-lowest salary of all the employees. Return all the fields of employees.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



SQL: Tips of the Day

"where 1=1" statement?

It's usually when folks build up SQL statements.

When you add and value = "Toyota" you don't have to worry about whether there is a condition before or just WHERE.

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