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

DISTINCT

The DISTINCT keyword is used to return only unique values from a column or combination of columns.

When a table contains duplicate values and we want to display each value only once, we can use the DISTINCT keyword.

What is DISTINCT?

DISTINCT removes duplicate values from the result returned by a SELECT query.

It does not delete duplicate records from the actual table. It only removes duplicate values from the query result.

Syntax

SQL
SELECT DISTINCT column_name
FROM table_name;

Example Table

StudentId StudentName City Course
101 Rahul Pune .NET
102 Priya Mumbai Java
103 Amit Pune Python
104 Neha Nashik .NET
105 Rohan Pune Java

Without DISTINCT

Suppose we want to display all cities from the Students table.

SQL
SELECT City
FROM Students;

The result may contain duplicate city names because multiple students can belong to the same city.

Using DISTINCT

To display each city only once, use DISTINCT.

SQL
SELECT DISTINCT City
FROM Students;

The result will contain only unique city names.

DISTINCT with Course

DISTINCT can be applied to any column where we want unique values.

SQL
SELECT DISTINCT Course
FROM Students;

This returns each course only once.

DISTINCT with Multiple Columns

DISTINCT can also be used with multiple columns.

SQL
SELECT DISTINCT City, Course
FROM Students;

In this case, SQL Server returns unique combinations of City and Course.

DISTINCT with WHERE

DISTINCT can be combined with the WHERE clause to filter records and then return unique values.

SQL
SELECT DISTINCT City
FROM Students
WHERE Course = '.NET';

This returns the unique cities of students enrolled in the .NET course.

DISTINCT with ORDER BY

DISTINCT can also be used with ORDER BY to return unique values in a specific order.

SQL
SELECT DISTINCT City
FROM Students
ORDER BY City ASC;

This returns unique city names in alphabetical order.

DISTINCT vs GROUP BY

DISTINCT is mainly used to remove duplicate values from a result set.

GROUP BY is generally used when we want to group records and perform aggregate operations such as COUNT, SUM, AVG, MIN and MAX.

SQL
SELECT DISTINCT City
FROM Students;

The above query simply returns unique cities.

Important

  • DISTINCT returns unique values.
  • DISTINCT does not delete duplicate records from the table.
  • DISTINCT can be used with one or multiple columns.
  • DISTINCT can be combined with WHERE.
  • DISTINCT can be combined with ORDER BY.

Summary

In this section, we learned how the DISTINCT keyword is used to return unique values from SQL Server query results. We covered DISTINCT with single columns, multiple columns, WHERE and ORDER BY.