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

SELECT Statement

The SELECT statement is used to retrieve data from one or more tables in SQL Server. It is one of the most frequently used SQL statements.

With SELECT, we can retrieve all columns, specific columns, filtered records, sorted records and a limited set of rows.

Basic SELECT Syntax

SQL
SELECT column_name
FROM table_name;

The SELECT keyword specifies the columns that we want to retrieve, while FROM specifies the table from which the data should be retrieved.

Sample Students Table

StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai
103 Amit 21 Pune

SELECT All Columns

The asterisk * is used when we want to retrieve all columns from a table.

SQL
SELECT *
FROM Students;
Output
StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai
103 Amit 21 Pune

SELECT Specific Columns

We can retrieve only the columns that are required instead of selecting every column.

SQL
SELECT StudentName, Age
FROM Students;
Output
StudentName Age
Rahul 22
Priya 23
Amit 21

SELECT with WHERE

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

SQL
SELECT *
FROM Students
WHERE Age > 21;
Output
StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai

SELECT with AND

The AND operator is used when multiple conditions must be satisfied.

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

SELECT with OR

The OR operator is used when at least one of multiple conditions should be satisfied.

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

SELECT with ORDER BY

ORDER BY is used to sort the result in ascending or descending order.

Ascending Order
SQL
SELECT *
FROM Students
ORDER BY Age ASC;
Descending Order
SQL
SELECT *
FROM Students
ORDER BY Age DESC;

SELECT DISTINCT

DISTINCT is used to return only unique values from the selected column or columns.

SQL
SELECT DISTINCT City
FROM Students;

SELECT TOP

TOP is used to return a specified number of rows from the result.

SQL
SELECT TOP 2 *
FROM Students;

SELECT Statement Flow

SQL

SELECT
   ↓
FROM
   ↓
WHERE
   ↓
ORDER BY
   ↓
Result
            

SELECT Statement Quick Reference

Clause / Keyword Purpose
SELECT Specifies the columns to retrieve.
FROM Specifies the table.
WHERE Filters records.
ORDER BY Sorts the result.
DISTINCT Returns unique values.
TOP Returns a specified number of rows.
Summary

The SELECT statement is used to retrieve data from SQL Server tables. We learned how to select all or specific columns, filter records using WHERE, combine conditions using AND and OR, sort results using ORDER BY, retrieve unique values using DISTINCT and limit the number of rows using TOP.