Single-row subqueries in SQL return only one row as the result and are
typically used within a WHERE clause or as a column expression.
Consider a table named Employees
with
columns EmployeeID
, EmployeeName
, and Salary
. We want to retrieve the name of
the employee whose salary is the highest.
SELECT EmployeeName FROM Employees WHERE
Salary = (SELECT
MAX(Salary) FROM
Employees);
In this example, the subquery (SELECT MAX(Salary) FROM Employees)
returns the
highest salary from the Employees
table. The outer query then retrieves the EmployeeName
where the Salary
matches the
highest salary found by the subquery. This query will return the name of the
employee with the highest salary.
Comments
Post a Comment