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 Operation

The SELECT statement is used to retrieve data from a SQL Server table.

SELECT is one of the most important SQL commands. It allows us to view records stored in database tables. We can retrieve all columns, specific columns, or only those records that satisfy a particular condition.

What is SELECT?

The SELECT statement is used to retrieve records from one or more tables.

It does not modify the existing data. It only reads and displays the requested information.

SELECT Syntax

SQL
SELECT column1, column2
FROM table_name;

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

Example Table

For the following examples, we will use the Students table.

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

SELECT All Columns

To retrieve all columns from a table, we can use the asterisk *.

SQL
SELECT *
FROM Students;

Output

The query returns all columns and all records from the Students table.

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

SELECT Specific Columns

We can retrieve only the columns that we need instead of retrieving all columns.

SQL
SELECT StudentName, Age
FROM Students;

Output

StudentName Age
Rahul 22
Priya 23
Amit 21
Neha 22

SELECT with WHERE

The WHERE clause is used to retrieve only those records that satisfy a specified condition.

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

Output

Only students whose City is Pune are returned.

StudentId StudentName Age City
101 Rahul 22 Pune
104 Neha 22 Pune

SELECT with Numeric Condition

We can also use comparison operators with numeric columns.

SQL
SELECT *
FROM Students
WHERE Age > 21;

SELECT with ORDER BY

ORDER BY is used to sort the retrieved records.

SQL
SELECT *
FROM Students
ORDER BY Age ASC;

ASC sorts the records in ascending order, while DESC sorts them in descending order.

SELECT DISTINCT

DISTINCT is used to remove duplicate values from the result.

SQL
SELECT DISTINCT City
FROM Students;

Practical Example

Suppose we want to display the names and cities of all students from the Students table.

SQL
SELECT StudentName, City
FROM Students;

The result will contain only StudentName and City.

Important Points

  • SELECT is used to retrieve data.
  • SELECT does not modify existing records.
  • The * symbol retrieves all columns.
  • Specific columns can be selected by writing their column names.
  • WHERE can be used to filter records.
  • ORDER BY can be used to sort records.
  • DISTINCT can be used to remove duplicate values.

Summary

In this section, we learned how the SELECT statement retrieves data from SQL Server tables. We covered selecting all columns, selecting specific columns, filtering records using WHERE, sorting records using ORDER BY and retrieving unique values using DISTINCT.