w3resource

SQL Exercise: Employees that work in department 47 or department 63

SQL Boolean Operator Statement: Exercise-12 with Solution

From the following table, write a SQL query to find the employees who work at depart 47 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 :

-- This query selects all columns from the 'emp_details' table.
SELECT *
-- Specifies the table from which to retrieve the data (in this case, 'emp_details').
FROM emp_details
-- Filters the rows to only include those where the 'emp_dept' column has the value 47 or 63.
WHERE emp_dept = 47 OR emp_dept = 63;

Output of the Query:

emp_idno	emp_fname	emp_lname	emp_dept
526689		Carlos		Snares		63
328717		Jhon		Snares		63
444527		Joseph		Dosni		47
659831		Zanifer		Emily		47
748681		Henrey		Gabriel		47
733843		Mario		Saule		63

Code Explanation:

The said SQL query that is selecting all columns (*) from the 'emp_details' table where the value of the "emp_dept" column is equal to 47 OR the value of the "emp_dept" column is equal to 63.
The query will return all rows from the 'emp_details' table that meet either of these conditions. It will return all the rows where the department of the employee is 47 or 63.

Relational Algebra Expression:

Relational Algebra Expression: Display all the data of employees that work in department 47 or department 63.

Relational Algebra Tree:

Relational Algebra Tree: Display all the data of employees that work in department 47 or department 63.

Practice Online


Query Visualization:

Duration:

Query visualization of Display all the data of employees that work in department 47 or department 63 - Duration

Rows:

Query visualization of Display all the data of employees that work in department 47 or department 63 - Rows

Cost:

Query visualization of Display all the data of employees that work in department 47 or department 63 - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Find those employees whose last name is Dosni or Mardy.
Next SQL Exercise: SQL Wildcard and Special operators Exercises Home

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.