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

WHERE Clause

The WHERE clause is used to filter records based on a specified condition.

When we want to retrieve only specific records from a table instead of all records, we use the WHERE clause.

What is WHERE Clause?

The WHERE clause specifies a condition that must be satisfied by the records returned by a SQL query.

It is commonly used with the SELECT, UPDATE and DELETE statements.

Syntax

SQL
SELECT column_name
FROM table_name
WHERE condition;

Example Table

StudentId StudentName Age City Fees
101 Rahul 22 Pune 35000
102 Priya 23 Mumbai 40000
103 Amit 21 Nashik 30000
104 Neha 24 Pune 45000

WHERE with Equal (=)

The equal operator is used when we want records having an exact value.

SQL
SELECT *
FROM Students
WHERE City = 'Pune';

This query returns students whose city is Pune.

WHERE with Greater Than (>)

SQL
SELECT *
FROM Students
WHERE Age > 22;

This returns students whose age is greater than 22.

WHERE with Less Than (<)

SQL
SELECT *
FROM Students
WHERE Age < 22;

This returns students whose age is less than 22.

Greater Than or Equal To (>=)

SQL
SELECT *
FROM Students
WHERE Age >= 22;

Less Than or Equal To (<=)

SQL
SELECT *
FROM Students
WHERE Fees <= 35000;

Not Equal To (<>)

The <> operator returns records where the specified value is different.

SQL
SELECT *
FROM Students
WHERE City <> 'Pune';

WHERE with AND

AND is used when all specified conditions must be true.

SQL
SELECT *
FROM Students
WHERE Age > 21
AND City = 'Pune';

Both conditions must be satisfied.

WHERE with OR

OR is used when at least one of the specified conditions should be true.

SQL
SELECT *
FROM Students
WHERE City = 'Pune'
OR City = 'Mumbai';

Multiple Conditions

Multiple conditions can be combined using AND and OR.

SQL
SELECT *
FROM Students
WHERE Age > 21
AND Fees > 30000
AND City = 'Pune';

WHERE with UPDATE

The WHERE clause can be used with UPDATE to modify only selected records.

SQL
UPDATE Students
SET Fees = 40000
WHERE StudentId = 101;

WHERE with DELETE

The WHERE clause can be used with DELETE to remove only selected records.

SQL
DELETE FROM Students
WHERE StudentId = 104;

Important

  • WHERE filters records based on a condition.
  • WHERE can be used with SELECT, UPDATE and DELETE.
  • AND requires all conditions to be true.
  • OR requires at least one condition to be true.
  • Always check the WHERE condition before UPDATE or DELETE operations.

Summary

In this section, we learned how the WHERE clause is used to filter records in SQL Server. We covered comparison operators, AND, OR, and the use of WHERE with SELECT, UPDATE and DELETE statements.