w3resource

SQL Exercises: Find all employees with last names Gabriel or Dosio

SQL SUBQUERY: Exercise-34 with Solution

34. From the following tables write a SQL query to find employees whose last name is Gabriel or Dosio. 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_lname' is in the set ('Gabriel', 'Dosio')
WHERE emp_lname IN ('Gabriel' , 'Dosio');

Output of the Query:

emp_idno	emp_fname	emp_lname	emp_dept
843795		Enric		Dosio		57
748681		Henrey		Gabriel		47

Explanation:

The above SQL query is selecting all columns and rows from the 'emp_details' table where the last name of the employee is either 'Gabriel' or 'Dosio'. The IN operator is used to check if a value is present within a list of values and if it matches any of the values on the list, the row is returned by the query.

Visual Explanation:

SQL Subqueries Inventory Exercises: Find all the details of employees whose last name is Gabriel or Dosio.

Practice Online


Sample Database:

Model Database

Query Visualization:

Duration:

Query visualization of Find all the details of employees whose last name is Gabriel or Dosio - Duration

Rows:

Query visualization of Find all the details of employees whose last name is Gabriel or Dosio - Rows

Cost:

Query visualization of Find all the details of employees whose last name is Gabriel or Dosio - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Prices for the most expensive products by each company.
Next SQL Exercise: List all employees in departments 89 and 63.

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.