w3resource

SQL Exercise: Employees whose salary is out of a given range

SQL SORTING and FILTERING on HR Database: Exercise-11 with Solution

11. From the following table, write a SQL query to find those employees whose salaries are not between 7000 and 15000 (Begin and end values are included). Sort the result-set in ascending order by the full name (first and last). Return full name and salary.

Sample table : employees


Sample Solution:

SELECT first_name || ' ' || last_name as Name, salary
 FROM  employees
   WHERE salary NOT BETWEEN 7000 AND 15000
     ORDER BY first_name || ' ' || last_name;

Sample Output:


       name        |  salary
-------------------+----------
 Alana Walsh       |  3100.00
 Alexander Khoo    |  3100.00
 Alexis Bull       |  4100.00
 Amit Banda        |  6200.00
 Anthony Cabrio    |  3000.00
 Britney Everett   |  3900.00
 Bruce Ernst       |  6000.00
 Charles Johnson   |  6200.00
 Curtis Davies     |  3100.00
 David Austin      |  4800.00
 David Lee         |  6800.00
 Diana Lorentz     |  4200.00
 Donald OConnell   |  2600.00
 Douglas Grant     |  2600.00
 Girard Geoni      |  2800.00
 Guy Himuro        |  2600.00
 Hazel Philtanker  |  2200.00
 Irene Mikkilineni |  2700.00
 James Landry      |  2400.00
 James Marlow      |  2500.00
 Jason Mallin      |  3300.00
 Jean Fleaur       |  3100.00
 Jennifer Dilly    |  3600.00
 Jennifer Whalen   |  4400.00
 John Seo          |  2700.00
 Joshua Patel      |  2500.00
 Julia Dellinger   |  3400.00
 Julia Nayer       |  3200.00
 Karen Colmenares  |  2500.00
 Kelly Chung       |  3800.00
 Kevin Feeney      |  3000.00
 Kevin Mourgos     |  5800.00
 Ki Gee            |  2400.00
 Laura Bissot      |  3300.00
 Lex De Haan       | 17000.00
 Luis Popp         |  6900.00
 Martha Sullivan   |  2500.00
 Michael Rogers    |  2900.00
 Mozhe Atkinson    |  2800.00
 Nandita Sarchand  |  4200.00
 Neena Kochhar     | 17000.00
 Pat Fay           |  6000.00
 Peter Vargas      |  2500.00
 Randall Matos     |  2600.00
 Randall Perkins   |  2500.00
 Renske Ladwig     |  3600.00
 Samuel McCain     |  3200.00
 Sarah Bell        |  4000.00
 Shanta Vollman    |  6500.00
 Shelli Baida      |  2900.00
 Sigal Tobias      |  2800.00
 Stephen Stiles    |  3200.00
 Steven King       | 24000.00
 Steven Markle     |  2200.00
 Sundar Ande       |  6400.00
 Sundita Kumar     |  6100.00
 Susan Mavris      |  6500.00
 Timothy Gates     |  2900.00
 TJ Olson          |  2100.00
 Trenna Rajs       |  3500.00
 Valli Pataballa   |  4800.00
 Vance Jones       |  2800.00
 Winston Taylor    |  3200.00
(63 rows)

Code Explanation:

The said query in SQL which retrieves the first name, last name concatenated as "Name", and salary columns from the 'employees' table where the salary is not between 7000 and 15000. The results are ordered by the concatenated result of first name and last name (displayed as "Name").

Practice Online


HR database model

Query Visualization:

Duration:

Query visualization of Display the full name, and salary, for all employees whose salary is out of the range 7000 and 15000 and arranged the full name in ascending order - Duration

Rows:

Query visualization of Display the full name, and salary, for all employees whose salary is out of the range 7000 and 15000 and arranged the full name in ascending order - Rows

Cost:

Query visualization of Display the full name, and salary, for all employees whose salary is out of the range 7000 and 15000 and arranged the full name in ascending order - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Employees whose first name is ending with the letter m.
Next SQL Exercise: Employees who was hired during given dates.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

Difference between natural join and inner join

One significant difference between INNER JOIN and NATURAL JOIN is the number of columns returned-

Consider:

TableA                           TableB
+------------+----------+        +--------------------+    
|Column1     | Column2  |        |Column1  |  Column3 |
+-----------------------+        +--------------------+
| 1          |  2       |        | 1       |   3      |
+------------+----------+        +---------+----------+

The INNER JOIN of TableA and TableB on Column1 will return

SELECT * FROM TableA AS a INNER JOIN TableB AS b USING (Column1);
SELECT * FROM TableA AS a INNER JOIN TableB AS b ON a.Column1 = b.Column1;
+------------+-----------+---------------------+    
| a.Column1  | a.Column2 | b.Column1| b.Column3|
+------------------------+---------------------+
| 1          |  2        | 1        |   3      |
+------------+-----------+----------+----------+

The NATURAL JOIN of TableA and TableB on Column1 will return:

SELECT * FROM TableA NATURAL JOIN TableB
+------------+----------+----------+    
|Column1     | Column2  | Column3  |
+-----------------------+----------+
| 1          |  2       |   3      |
+------------+----------+----------+

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

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook