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_detailsEMP_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:
Practice Online
Sample Database:
Query Visualization:
Duration:
Rows:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql-exercises/subqueries/sql-subqueries-inventory-exercise-34.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics