w3resource

SQL Exercise: Employees who are Sales Representatives or Sales Men

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

28. From the following table, write a SQL query to find all those employees who are either Sales Representatives or Salesmen. Return first name, last name and hire date.

Sample table: employees


Sample Solution:

SELECT first_name, last_name, hire_date
	FROM employees 
		WHERE job_id IN ('SA_REP', 'SA_MAN');

Sample Output:

 first_name  | last_name  | hire_date
-------------+------------+------------
 John        | Russell    | 2004-10-01
 Karen       | Partners   | 2005-01-05
 Alberto     | Errazuriz  | 2005-03-10
 Gerald      | Cambrault  | 2007-10-15
 Eleni       | Zlotkey    | 2008-01-29
 Peter       | Tucker     | 2005-01-30
 David       | Bernstein  | 2005-03-24
 Peter       | Hall       | 2005-08-20
 Christopher | Olsen      | 2006-03-30
 Nanette     | Cambrault  | 2006-12-09
 Oliver      | Tuvault    | 2007-11-23
 Janette     | King       | 2004-01-30
 Patrick     | Sully      | 2004-03-04
 Allan       | McEwen     | 2004-08-01
 Lindsey     | Smith      | 2005-03-10
 Louise      | Doran      | 2005-12-15
 Sarath      | Sewall     | 2006-11-03
 Clara       | Vishney    | 2005-11-11
 Danielle    | Greene     | 2007-03-19
 Mattea      | Marvins    | 2008-01-24
 David       | Lee        | 2008-02-23
 Sundar      | Ande       | 2008-03-24
 Amit        | Banda      | 2008-04-21
 Lisa        | Ozer       | 2005-03-11
 Harrison    | Bloom      | 2006-03-23
 Tayler      | Fox        | 2006-01-24
 William     | Smith      | 2007-02-23
 Elizabeth   | Bates      | 2007-03-24
 Sundita     | Kumar      | 2008-04-21
 Ellen       | Abel       | 2004-05-11
 Alyssa      | Hutton     | 2005-03-19
 Jonathon    | Taylor     | 2006-03-24
 Jack        | Livingston | 2006-04-23
 Kimberely   | Grant      | 2007-05-24
 Charles     | Johnson    | 2008-01-04
(35 rows)

Code Explanation:

The said query in SQL that retrieves the first name, last name, and hire date of employees whose job id is either 'SA_REP' or 'SA_MAN' from the 'employees' table.

Relational Algebra Expression:

Relational Algebra Expression: Display the  first and last name and date of joining of the employees who is either Sales Representative or Sales Man.

Relational Algebra Tree:

Relational Algebra Tree: Display the  first and last name and date of joining of the employees who is either Sales Representative or Sales Man.

Practice Online


HR database model

Query Visualization:

Duration:

Query visualization of Display the first and last name and date of joining of the employees who is either Sales Representative or Sales Man - Duration

Rows:

Query visualization of Display the first and last name and date of joining of the employees who is either Sales Representative or Sales Man - Rows

Cost:

Query visualization of Display the first and last name and date of joining of the employees who is either Sales Representative or Sales Man - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Details of jobs in descending order by title.
Next SQL Exercise: Average salary of each commission-based department.

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