Subqueries in SQL
Subqueries are SQL statements that are nested within another SQL statement. Subqueries can be used to perform calculations, filter data, or retrieve data from another table. Here's an example:
sql
SELECT name, age
FROM employees
WHERE department = (
SELECT name
FROM departments
WHERE id = 2
);
In this example, we're using a subquery to retrieve the name of the department with an id of 2 from the "departments" table. We're then using that value in the main query to select all employees who work in that department.
No comments:
Post a Comment