MySQL Subquery Exercises: Find the name and salary of the employees who have a higher salary than the employee whose last_name='Bull'
MySQL Subquery: Exercise-1 with Solution
Write a MySQL query to find the name (first_name, last_name) and the salary of the employees who have a higher salary than the employee whose last_name='Bull'.
Sample table: employees
Code:
-- Selecting the first name, last name, and salary of employees whose salary is higher than that of the employee with the last name 'Bull'
SELECT FIRST_NAME, LAST_NAME, SALARY
-- Selecting data from the employees table
FROM employees
-- Filtering the result set to include only employees whose salary is higher than the salary of the employee with the last name 'Bull'
WHERE SALARY >
-- Subquery to fetch the salary of the employee with the last name 'Bull'
(SELECT salary FROM employees WHERE last_name = 'Bull');
Explanation :
- This MySQL code selects the first name, last name, and salary of employees from a table named "employees".
- It filters the results to only include employees whose salary is greater than the salary of the employee with the last name 'Bull'.
- This is achieved by using a subquery to fetch the salary of the employee with the last name 'Bull', and then comparing it with the salaries of other employees in the outer query.
MySQL Subquery Syntax :
- The subquery (inner query) executes once before the main query (outer query) executes.
- The main query (outer query) use the subquery result.
Here the where comparison operator uses the '>' operator.
MySQL Code Editor:
Structure of 'hr' database:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous:MySQL Subquery
Next:Write a MySQL query to find the name (first_name, last_name) of all employees who works in the IT department.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics