SQL Queries – Subqueries

A subquery is a query inside another query, used to filter, compare or aggregate data based on specific conditions.
In this DeGalaLab guide, you’ll learn how to apply SQL subqueries with operators like WHERE, HAVING, IN, EXISTS, BETWEEN, and others — with clear explanations and practical examples.

SQL - Subqueries

🧭 Role: Exercice
🗂️ Area: Data Science
📅 Year: 2025
🧩 Stack: SQL
📝 Credits: deGalaLab

Results / Insights

| Operador | Ús principal | Retorna quan... |
| -------------- | ----------------------- | --------------------------------------- |
| **WHERE** | Filtrar segons condició | Coincideix amb el valor de subconsulta |
| **HAVING** | Filtrar grups agregats | La condició sobre l’agregació és certa |
| **BETWEEN** | Rang de valors | Valor dins del rang retornat |
| **IN** | Inclusió en conjunt | Valor pertany a la llista |
| **LIKE** | Patró de text | Coincideix amb el patró retornat |
| **IS NULL** | Valors nuls | Resultat nul o sense assignar |
| **ANY / SOME** | Comparació parcial | Condició certa per **almenys un** valor |
| **ALL** | Comparació total | Condició certa per **tots** els valors |
| **EXISTS** | Existència | Subconsulta retorna alguna fila |

A subquery is a query inside another query.
It is used to obtain values that are then compared or filtered in the main query.

The subquery can appear in different parts of the SQL statement: in WHERE, HAVING, FROM, or SELECT.

WHERE

Used to filter results according to the value returned by a subquery.

SELECT Name, Salary
FROM Employees
WHERE Salary > (
    SELECT AVG(Salary)
    FROM Employees
);

Returns employees with a salary higher than the average.


HAVING

Allows applying conditions on aggregate functions using subqueries.

SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > (
    SELECT AVG(Salary)
    FROM Employees
);

Shows departments with an average salary above the overall average.


SELECT Name, Salary
FROM Employees
WHERE Salary BETWEEN (
    SELECT MIN(Salary) FROM Employees WHERE Department = 'Sales'
)
AND (
    SELECT MAX(Salary) FROM Employees WHERE Department = 'Sales'
);

BETWEEN

Checks if a value is within a range returned by a subquery.

Returns employees whose salaries fall within the range of the Sales department.


SELECT Name
FROM Employees
WHERE Department IN (
    SELECT DepartmentName
    FROM Departments
    WHERE City = 'Barcelona'
);

IN

Checks whether a value belongs to a set of results returned by a subquery.

Selects employees working in departments located in Barcelona.


SELECT Name
FROM Clients
WHERE Name LIKE (
    SELECT CONCAT(Prefix, '%')
    FROM Settings
    WHERE Key = 'client_prefix'
);

LIKE

Although commonly used with text patterns, it can also be combined with subqueries.

Returns clients whose names start with the prefix defined in the Settings table.


SELECT Name
FROM Employees
WHERE Salary > ALL (
    SELECT Salary
    FROM Employees
    WHERE Department = 'Administration'
);

ANY, SOME, ALL

Allow comparing a value with all or some of the results of a subquery.

Returns employees who earn more than all employees in Administration.

SELECT Name
FROM Employees
WHERE Salary > ANY (
    SELECT Salary
    FROM Employees
    WHERE Department = 'Sales'
);

Returns employees who earn more than at least one worker in Sales.


SELECT Name
FROM Clients c
WHERE EXISTS (
    SELECT *
    FROM Orders o
    WHERE o.Client_ID = c.Client_ID
);

EXISTS

Checks if the subquery returns at least one row.

Shows clients who have at least one registered order.


OperatorMain useReturns when…
WHEREFilter by conditionMatches the subquery value
HAVINGFilter aggregate groupsThe condition on the aggregation is true
BETWEENValue rangeValue is within the returned range
INSet inclusionValue belongs to the list
LIKEText patternMatches the returned pattern
IS NULLNull valuesResult is null or unassigned
ANY / SOMEPartial comparisonCondition is true for at least one value
ALLTotal comparisonCondition is true for all values
EXISTSExistenceSubquery returns one or more rows