w3resource

SQL Subquery Exercises: Display the first and last name, salary, and department ID for those employees whose salary is equal to the salary of the employee who works in that department which ID is 40

SQL SUBQUERY: Exercise-40 with Solution

From the following table, write a SQL query to find those employees whose salary matches that of the employee who works in department ID 40. Return first name, last name, salary, and department ID.

Sample table: employees


Sample Solution:

SELECT first_name, last_name, salary, department_id 
 FROM employees
  WHERE salary IN (
                   SELECT salary 
                     FROM employees 
                       WHERE department_id = 40);

Sample Output:

first_name	last_name	salary	department_id
Shanta		Vollman		6500.00		50
Susan		Mavris		6500.00		40

Pictorial Presentation:

SQL Subqueries: Display the first and last name, salary, and department ID for those employees whose salary is equal to the salary of the employee who works in that department which ID is 40.

Practice Online


Query Visualization:

Duration:

Query visualization of Display the first and last name, salary, and department ID for those employees whose salary is equal to the salary of the employee who works in that department which ID is 40 - Duration

Rows:

Query visualization of Display the first and last name, salary, and department ID for those employees whose salary is equal to the salary of the employee who works in that department which ID is 40 - Rows

Cost:

Query visualization of Display the first and last name, salary, and department ID for those employees whose salary is equal to the salary of the employee who works in that department which ID is 40 - 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 employees who work in that department where the employee works of ID 201. Return first name, last name, salary, and department ID.
Next: From the following table, write a SQL query to find those employees who work in the department 'Marketing'. Return first name, last name and department ID.

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