SQL Server DISTINCT and GROUP BY Clause

Last updated on Dec 18 2021
Amit Warghade

Table of Contents

SQL Server DISTINCT and GROUP BY Clause

In SQL Server, DISTINCT clause is employed to get rid of duplicates from the table. The DISTICT clause is merely used with SELECT statement.

Syntax:

  1. SELECT DISTINCT expressions
    FROM [database_name].[dbo].[table_name]
    [WHERE conditions];

Parameter explanation

expressions: It specifies the columns or calculations that you simply want to retrieve.

database_name & table_name: It specifies the name of the database and name of the table on which you would like to try to to your operations.

Example:

DISTINCT clause with single expression

You have a table named “Employees”, having the subsequent data:

image001 26
Parameter

Select distinct salary from Employees where salary is bigger than 12000.

  1. SELECT DISTINCT salary
    FROM [tecklearn].[dbo].[Employees]
    WHERE salary > 15000;

Output:

image002 16
distinct

Here, you’ll see that there’s only 6 distinct salaries within 9 records. Distinct clause shows only unique records.

DISTINCT clause with multiple expressions

DISTINCT clause also can be used with multiple expressions. it’ll remove duplicates from quite one field in your SELECT statement.

  1. SELECT DISTINCT name, age, salary
    FROM [tecklearn].[dbo].[Employees]
    WHERE id > 7;

Output:

image003 15
multiple

SQL Server GROUP BY Clause

SQL Server GROUP BY clause is employed to gather data across multiple records and group the results by one or more columns. it’s used with SELECT statement.

Syntax:

  1. SELECT expression1, expression2, ... expression_n,
    aggregate_function (expression)FROM tables
    [WHERE conditions] 
    GROUP BY expression1, expression2, ... expression_n;

Parameter explanation

expression1, expression2, … expression_n: These expressions aren’t encapsulated within an aggregate function and must be included within the GROUP BY clause.

aggregate_function: It are often a function like SUM, COUNT, MIN, MAX, or AVG functions.

tables: The tables that you simply wish to retrieve records from. There must be a minimum of one table listed within the FROM clause.

WHERE conditions: it’s optional. The conditions that has got to be met for the records to be selected.

Example:

First create a table "Employee2":
image004 12
GROUP BY

Following may be a list of some inserted data within the table.

image005 13
insert

GROUP By using SUM Function

See this instance where we GROUP BY department from the “Employee2” using SUM function:

  1. SELECT department, SUM(salary) AS "Total salary"
    FROM [tecklearn].[dbo].[Employee2]
    GROUP BY department;

Output:

image006 10
SUM

GROUP By using COUNT Function

See this instance where we GROUP BY designation from the “Employee2” using COUNT function:

  1. SELECT designation, COUNT(*) AS "Number of employees"
    FROM [tecklearn].[dbo].[Employee2]
    GROUP BY designation;

Output:

image007 9
COUNT

GROUP By using MIN Function

See this instance where we GROUP BY department on the idea of salary from the “Employee2” using MIN function.

This will retrieve the minimum salary consistent with department:

  1. SELECT department, MIN(salary) AS "Lowest salary"
    FROM [tecklearn].[dbo].[Employee2]
    GROUP BY department;

Output:

image008 11
MIN

GROUP By using MAX Function

See this instance where we GROUP BY department on the idea of salary from the “Employee2” sing MAX function.

This will retrieve the utmost salary consistent with department:

  1. SELECT department, MAX(salary) AS "Highest salary"
    FROM [tecklearn].[dbo].[Employee2]
    GROUP BY department;

Output:

image008 12
MAX

So, this brings us to the end of blog. This Tecklearn ‘SQL Server DISTINCT and GROUP BY Clause’ blog helps you with commonly asked questions if you are looking out for a job in MS-SQL Server. If you wish to learn MS-SQL Server and build a career in Database domain, then check out our interactive, MS-SQL Server Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/ms-sql-server-training-and-certification-course/

MS-SQL Server Training

About the Course

Microsoft SQL Server training course is an online classroom training meant for developers to master the descriptive language to work for relational databases. Important topics included in this SQL course are SQL introduction, relational databases, queries, subqueries, joins, and unions. After completing this course, you will also become proficient in the theory of optimization, transactions, indexing, installing SQL Server, database administration, clustering, implementing programmability objects, designing and implementing database objects, managing database concurrency, and optimizing SQL infrastructure and database objects.

Why Should you take MS-SQL Server Training?

  • The average salary for Sql server developer ranges from approximately $88,666 per year for Database Developer to $115,811 per year for Senior SQL Developer. – Indeed.com
  • Wells Fargo, UPS, US Bank, Perspecta, Silicon Valley Bank, Mindtree Wipro, Infosys & many other MNC’s worldwide use MS SQL Server for their Database deployments.
  • According to Gartner, MS SQL Server is the market leader in the Relational Database with 18.5% market share globally.

What you will Learn in this Course?

Introduction to SQL

  • Various types of databases
  • Introduction to SQL
  • Installation Steps of MS-SQL Server
  • Overview of Microsoft SQL Server Management Studio
  • SQL architecture, client/server relation
  • Database types

Introduction to relational databases, basic concepts of relational tables

  • Working with rows and columns
  • Various operators used like logical and relational
  • Constraints, Primary Key and Foreign Key
  • Insert, Update, Delete, Alter

Working with SQL: Join, Tables, and Variables

  • SQL Operators and Queries
  • SQL functions
  • Creation of Table
  • Retrieval of Data from tables
  • Combining rows from tables using joins
  • Operators such as intersect, except, union, temporary table creation, set operator rules, table variables etc

Deep Dive into SQL Functions

  • Data Retrieval Language
  • Functions
  • Operators
  • Clauses
  • Sub-Queries
  • Correlated Sub-Queries

SQL Views, Functions

  • Sub-queries
  • Views
  • Indexes

Managing Data with Transact-SQL

  • Transact-SQL queries
  • Implementing functions and aggregating data
  • Determining the results of DDL statements on supplied tables and data

Cursor Management and Triggers

  • Cursors
  • Triggers
  • Types of Triggers

Stored Procedures and User Defined functions

  • Understanding of Stored Procedures
  • Key benefits of Stored Procedures
  • Studying user-defined functions

Deep Dive into User-defined Functions

  • Detailed study of user-defined functions
  • Various types of UDFs like Scalar, Inline Table Value, multi-statement Table
  • What is Rank Function? Triggers, when to execute Triggers?

SQL Optimization and Performance

  • Concepts of Concurrency and Locking behaviour
  • Usage of memory-optimized tables to solve issues
  • Examining and Troubleshooting of query plans
  • Performance management of Database instances
  • SQL server Performance Monitoring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "SQL Server DISTINCT and GROUP BY Clause"

Leave a Message

Your email address will not be published. Required fields are marked *