SQL Exercise: Employees who are Sales Representatives or Sales Men
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+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 2003-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 2005-09-21 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 2001-01-13 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 2006-01-03 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 2007-05-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 2005-06-25 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 2006-02-05 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 2007-02-07 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 2002-08-17 | FI_MGR | 12008.00 | 0.00 | 101 | 100 | | 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | 2002-08-16 | FI_ACCOUNT | 9000.00 | 0.00 | 108 | 100 | ...... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 2002-06-07 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Sample Solution:
-- Selecting 'first_name', 'last_name', and 'hire_date' columns
SELECT first_name, last_name, hire_date
-- Specifying the table to retrieve data from ('employees')
FROM employees
-- Filtering the results to include only records where 'job_id' is either 'SA_REP' or 'SA_MAN'
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 Tree:
Go to:
PREV : Details of jobs in descending order by title.
NEXT : Average salary of each commission-based department.
Practice Online
Query Visualization:
Duration:
Rows:
Cost:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
