Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Single-Row Subquery

Learn how to use a subquery that returns only one row or one value.

What is a Single-Row Subquery?

A Single-Row Subquery is a subquery that returns only one row or one value as its result.

Because the subquery returns a single value, we can commonly use comparison operators such as =, >, <, >= and <=.

Basic Syntax


SELECT column_name
FROM table_name
WHERE column_name = (
    SELECT column_name
    FROM table_name
    WHERE condition
);
            

The inner query should return only one value when using a single-value comparison operator.

Example Table

Consider the following Employees table:

EmployeeId EmployeeName Department Salary
1 Rahul IT 30000
2 Priya HR 45000
3 Amit IT 50000
4 Sneha Finance 40000

Example 1: Using = Operator

Suppose we want to find employees who have the same salary as Priya.


SELECT EmployeeName, Salary
FROM Employees
WHERE Salary = (
    SELECT Salary
    FROM Employees
    WHERE EmployeeName = 'Priya'
);
            
How it works
  1. The inner query finds Priya's salary.
  2. The inner query returns one value.
  3. The outer query finds employees whose salary matches that value.

Example 2: Using > Operator

Find employees whose salary is greater than Rahul's salary.


SELECT EmployeeName, Salary
FROM Employees
WHERE Salary > (
    SELECT Salary
    FROM Employees
    WHERE EmployeeName = 'Rahul'
);
            

The inner query returns Rahul's salary. The outer query then returns employees whose salary is greater than Rahul's salary.

Example 3: Using Aggregate Function

Aggregate functions such as MAX(), MIN() and AVG() normally return a single value, making them useful with single-row subqueries.

Find employees whose salary is equal to the highest salary in the table:


SELECT EmployeeName, Salary
FROM Employees
WHERE Salary = (
    SELECT MAX(Salary)
    FROM Employees
);
            

The inner query returns the highest salary. The outer query finds the employee having that salary.

Comparison Operators

Operator Meaning Example
= Equal to Salary = (Subquery)
> Greater than Salary > (Subquery)
< Less than Salary < (Subquery)
>= Greater than or equal to Salary >= (Subquery)
<= Less than or equal to Salary <= (Subquery)

Important Points

  • A single-row subquery returns one row or one value.
  • It can commonly be used with comparison operators.
  • The inner query is normally enclosed in parentheses.
  • Aggregate functions such as MAX(), MIN() and AVG() can return a single value.
  • If a subquery returns multiple rows while using a single-value operator such as =, SQL Server can generate an error.

Summary

A Single-Row Subquery returns one row or one value and is commonly used with comparison operators such as =, >, <, >= and <=. It is useful when the result of one query is required for comparison in another query.