PostgreSQL Subquery: Find the names and salaries of the employees who have higher salary than the employee whose last_name is Bull
1. Write a query to find the first_name, last_name and salaries of the employees who have a higher salary than the employee whose last_name is Bull.
Sample Solution:
Code:
-- This SQL query retrieves the first name, last name, and salary of employees whose salary is greater than that of an employee with the last name 'Bull'.
SELECT first_name, -- Selects the first_name column from the employees table
last_name, -- Selects the last_name column from the employees table
salary -- Selects the salary column from the employees table
FROM employees -- Specifies the table from which to retrieve data, in this case, the employees table
WHERE salary > -- Filters the rows to include only those where the salary is greater than the salary of an employee with the last name 'Bull'
(SELECT salary -- Subquery: Selects the salary of an employee with the last name 'Bull'
FROM employees
WHERE last_name = 'Bull');
Explanation:
- This SQL query retrieves the first name, last name, and salary of employees whose salary is greater than that of an employee with the last name 'Bull'.
- The outer SELECT statement retrieves the first name, last name, and salary from the employees table.
- The WHERE clause filters the rows to include only those where the salary is greater than the salary obtained from the subquery.
- The subquery retrieves the salary of an employee with the last name 'Bull'.
- The subquery is enclosed in parentheses and executed before the outer query.
Sample table: employees
Output:
pg_exercises=# SELECT first_name, last_name, salary pg_exercises-# FROM employees pg_exercises-# WHERE salary > pg_exercises-# (SELECT salary pg_exercises(# FROM employees pg_exercises(# WHERE last_name = 'Bull'); first_name | last_name | salary -------------+------------+---------- Alexander | Hunold | 9000.00 Bruce | Ernst | 6000.00 David | Austin | 4800.00 Valli | Pataballa | 4800.00 Diana | Lorentz | 4200.00 Den | Raphaely | 11000.00 Steven | King | 24000.00 Neena | Kochhar | 17000.00 Janette | King | 10000.00 Patrick | Sully | 9500.00 Allan | McEwen | 9000.00 Lindsey | Smith | 8000.00 Louise | Doran | 7500.00 Sarath | Sewall | 7000.00 Clara | Vishney | 10500.00 Mattea | Marvins | 7200.00 Payam | Kaufling | 7900.00 Shanta | Vollman | 6500.00 Kevin | Mourgos | 5800.00 Ellen | Abel | 11000.00 Alyssa | Hutton | 8800.00 ... | ... | ... Hermann | Baer | 10000.00 Shelley | Higgins | 12000.00 William | Gietz | 8300.00 (62 rows)
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: PostgreSQL Subquery - Exercises, Practice, Solution
Next: Write a SQL subquery to find the first_name and last_name of all employees who works in the IT department.
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://w3resource.com/postgresql-exercises/subquery/postgresql-subquery-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics