Subqueries in SQL
A subquery is a query within another query. We can use subqueries to retrieve data from one table based on the values of another table. Here's an example:
sql
SELECT name, department
FROM employees
WHERE department IN (
SELECT id
FROM departments
WHERE name = 'Sales'
);
In this example, we're selecting the name and department of employees who work in the Sales department. We're using a subquery to retrieve the department id of the Sales department from the "departments" table and then filtering the "employees" table based on that id.
No comments:
Post a Comment