Definition: A subquery is a query nested inside another SQL query, which can be used in SELECT
, INSERT
, UPDATE
, or DELETE
statements.
Syntax
SELECT column1 FROM table_name WHERE column2 IN (SELECT column2 FROM another_table);
Example:
SELECT first_name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1000);
Explanation: This retrieves names of employees who work in departments located in a specific location_id
, using a subquery to find relevant department_id
s.
Leave a Reply