From the given table "emp_det" which has column values "emp_id", "fist_name", "last_name", "email","person_id", "dept_id", "salary". Write a SQL query to retrieve the employee ID, first name, and last name of employees whose first names contain the letter 'S' and who work in departments where at least one employee's first name contains the letter 'S'.Table Name: emp_det
Question
From the given table "emp_det" which has column values "emp_id", "fist_name", "last_name", "email","person_id", "dept_id", "salary". Write a SQL query to retrieve the employee ID, first name, and last name of employees whose first names contain the letter 'S' and who work in departments where at least one employee's first name contains the letter 'S'.Table Name: emp_det
Solution
Sure, here is the SQL query that you need:
SELECT emp_id, first_name, last_name
FROM emp_det
WHERE first_name LIKE '%S%'
AND dept_id IN (
SELECT dept_id
FROM emp_det
WHERE first_name LIKE '%S%'
);
Here's how this query works:
-
The
SELECTstatement is used to select theemp_id,first_name, andlast_namecolumns from theemp_dettable. -
The
WHEREclause is used to filter the results. Thefirst_name LIKE '%S%'condition checks if the first name contains the letter 'S'. -
The
ANDoperator is used to combine the conditions. Thedept_id INcondition checks if the department ID is in the list of department IDs where at least one employee's first name contains the letter 'S'. -
The subquery
(SELECT dept_id FROM emp_det WHERE first_name LIKE '%S%')is used to get the list of department IDs where at least one employee's first name contains the letter 'S'.
Similar Questions
You are given a table Employee which has the following columns :id, FirstName, LastName.You have to use a query to display all those employees whose first names start with the letter ‘R’ and end with the letter ‘I’. Some queries are given below -(a) SELECT * FROM Employee WHERE FirstName LIKE '%RI%';(b) SELECT * FROM Employee WHERE FirstName LIKE 'R%' AND FirstName LIKE '%I';(c) SELECT * FROM Employee WHERE FirstName LIKE 'R' AND 'I';(d) SELECT * FROM Employee WHERE FirstName LIKE 'R%I';Choose the most appropriate option from below :Options: Pick one correct answer from belowOnly (c)Only (b)(b) and (d)(b), (c) and (d)(a) and (c)None of the above
Evaluate below sql statement SELECT emp_name, department FROM employees WHERE department = 'HR' UNION SELECT contractor_name, department FROM contractors WHERE department = 'IT';Select one:a. Retrieves the names of all employees and contractors from the HR and IT departments.b. Retrieves the names of employees from the HR department and all contractors.c. Retrieves the names of employees and contractors from the HR and IT departments, excluding employees from the IT department and contractors from the HR department.d. Retrieves the names of employees from the HR department and contractors from the IT department.
Find the following table EmployeeDetails, with the attributes EmployeeID, EmpName, Designation, Salary, DeptNo. You are assigned to a task of providing the solution to the operations department by writing the query which displays the employeeID and the Designation of all the employees whose Salaries are more than the Salary of employees designated as ManagerC
From the given table "dept_det" which has columns as "dept_id", "name1", "manger_id", and "location_id". Write a query to retrieve the manager ID of the department where the department ID is 30.Table name:dept_det
Create Table Employee with attributes firstName,LastName,SSN,Address,Salary,Birthday,Sex,SupervisorSSN,DepartmentNo
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.