w3resource

SQL Exercises: List all employees in departments 89 and 63

SQL SUBQUERY: Exercise-35 with Solution

35. From the following tables, write a SQL query to find the employees who work in department 89 or 63. Return emp_idno, emp_fname, emp_lname and emp_dept.

Sample table: emp_details
 EMP_IDNO EMP_FNAME       EMP_LNAME         EMP_DEPT
--------- --------------- --------------- ----------
   127323 Michale         Robbin                  57
   526689 Carlos          Snares                  63
   843795 Enric           Dosio                   57
   328717 Jhon            Snares                  63
   444527 Joseph          Dosni                   47
   659831 Zanifer         Emily                   47
   847674 Kuleswar        Sitaraman               57
   748681 Henrey          Gabriel                 47
   555935 Alex            Manuel                  57
   539569 George          Mardy                   27
   733843 Mario           Saule                   63
   631548 Alan            Snappy                  27
   839139 Maria           Foster                  57

Sample Solution:

-- Selecting all columns from the 'emp_details' table
SELECT * 
-- Specifying the table to retrieve data from ('emp_details')
FROM emp_details
-- Filtering the results based on the condition that 'emp_dept' is in the set (89, 63)
WHERE emp_dept IN (89, 63);

Output of the Query:

emp_idno	emp_fname	emp_lname	emp_dept
526689		Carlos		Snares		63
328717		Jhon		Snares		63
733843		Mario		Saule		63

Explanation:

The above SQL query is selecting all columns and rows from the 'emp_details' table where the department number of the employee is either 89 or 63.
By using the IN operator, the query checks if a value is present within a list of values, and if so, the row will be returned.

Visual Explanation:

SQL Subqueries Inventory Exercises: Display all the details of employees who works in department 89 or 63.

Practice Online


Sample Database:

Model Database

Query Visualization:

Duration:

Query visualization of Display all the details of employees who works in department 89 or 63 - Duration

Rows:

Query visualization of Display all the details of employees who works in department 89 or 63 - Rows

Cost:

Query visualization of Display all the details of employees who works in department 89 or 63 - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Find all employees with last names Gabriel or Dosio.
Next SQL Exercise: Departments with allotment amounts exceeding Rs. 50000.

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.