SlideShare a Scribd company logo
Naveen Kumar Veligeti

CLAUSES in SQL Server
SQL Server provides with the following clauses that can be used in the SELECT statements:





WHERE
GROUP BY
HAVING
ORDER BY

The complete syntax of the SELECT statement looks as following:
SELECT <select_list> FROM <tname>
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
WHERE Clause:
The WHERE clause is a filter that defines the conditions each row in the source tables must meet
to qualify for the SELECT. Only rows that meet the conditions contribute data to the result set.
Data from rows that do not meet the conditions is not used.
SELECT * FROM EMP WHERE JOB='MANAGER'
SELECT * FROM EMP WHERE DEPTNO=20
GROUP BY Clause:
The GROUP BY clause partitions the result set into groups based on the values in the columns of
the group_by_list. For example, the Emp table has 3 values in Deptno column. A GROUP
BY Deptno clause partitions the result set into 3 groups, one for each value of Deptno.
Ex-1:How to find the highest salaries for each department.
Sol: SELECT DEPTNO, MAX(SAL) FROM EMP GROUP BY DEPTNO
Ex-2:How to find the highest salaries for each job.
Sol: SELECT JOB, MAX(SAL) FROM EMP GROUP BY JOB

1
Naveen Kumar Veligeti

Ex-3:How to find the highest salaries for each department in it for each job.
Sol: SELECT DEPTNO, JOB, MAX(SAL) FROM EMP GROUP BY DEPTNO, JOB
Note: While using the GROUP By clause the select_list of the query should contain only the
following:




Group Functions or Aggregate Functions
Columns used in the Group By Clause
Constants.

Ex-4:How to find the number of employees working for each department.
Sol: SELECT DEPTNO, COUNT(*) FROM EMP GROUP BY DEPTNO
Ex-5:How to find the number of employees working for each department only if the number
is greater than 3.
Sol: SELECT DEPTNO, COUNT(*) FROM EMP GROUP
BY DEPTNO HAVING COUNT(*)>3
HAVING Clause:
The HAVING clause is an additional filter that is applied to the result set. Logically, the
HAVING clause filters rows from the intermediate result set built from applying any FROM,
WHERE, or GROUP BY clauses in the SELECT statement. HAVING clauses are typically used
with a GROUP BY clause.
Ex-6:How to find the number of Clerk’s working for each department.
Sol: SELECT DEPTNO, COUNT(*) FROM EMP WHERE JOB='CLERK' GROUP
BY DEPTNO
Ex-7:How to find the number of Clerk's working for each department only if the count is
greater than 1.
Sol: SELECT DEPTNO, COUNT(*) FROM EMP WHERE JOB='CLERK' GROUP
BY DEPTNO HAVING COUNT(*)>1
ORDER BY clause:
The ORDER BY clause defines the order in which the rows in the result set are
sorted. order_list specifies the result set columns that make up the sort list. The ASC and DESC
keywords are used to specify if the rows are sorted in an ascending or descending sequence.

2
Naveen Kumar Veligeti

ORDER BY order_list[ ASC | DESC ]
SELECT * FROM EMP ORDER BY SAL
SELECT * FROM EMP ORDER BY SAL DESC
SELECT * FROM EMP ORDER BY SAL, COMM

3

More Related Content

PPTX
Baabtra.com little coder chapter - 7
PPTX
Functions
PDF
5. Group Functions
PPTX
Lab3 aggregating data
PDF
Manual for Troubleshooting Formulas & Functions in Excel
DOCX
Optiva-13449_TestResults
PDF
03 ohp slides 1
PPTX
How to use vlookup in MS Excel
Baabtra.com little coder chapter - 7
Functions
5. Group Functions
Lab3 aggregating data
Manual for Troubleshooting Formulas & Functions in Excel
Optiva-13449_TestResults
03 ohp slides 1
How to use vlookup in MS Excel

What's hot (6)

PPT
Common table expressions
PPTX
VLOOKUP HLOOKUP INDEX MATCH
PPT
PPT
Mysql Statments
 
PPT
PPTX
Vlookup Description
Common table expressions
VLOOKUP HLOOKUP INDEX MATCH
Mysql Statments
 
Vlookup Description
Ad

Viewers also liked (8)

PDF
Sql where clause
PPT
Executing Sql Commands
PPTX
Sql server ___________session_10(group by clause)
PPTX
Sql query performance analysis
PPT
Aggregating Data Using Group Functions
PPT
ALL ABOUT SQL AND RDBMS
PPT
Ch 7 Physical D B Design
PPTX
Physical database design(database)
Sql where clause
Executing Sql Commands
Sql server ___________session_10(group by clause)
Sql query performance analysis
Aggregating Data Using Group Functions
ALL ABOUT SQL AND RDBMS
Ch 7 Physical D B Design
Physical database design(database)
Ad

Similar to Clauses in sql server (20)

PPT
Sql query [select, sub] 4
PDF
Group by clause mod
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
PDF
Introduction to oracle functions
PPT
Module03
PPTX
Sql clauses by Manan Pasricha
PPT
PPT
PPT
PPTX
Its about a sql topic for basic structured query language
PDF
PPTX
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
PDF
Using of Group By and Having Clause in MySQL
PPT
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PDF
Structured query language(sql)
PPTX
Module 3.1.pptx
PPT
Aggregate Functions,Final
PPTX
DBMS: Week 07 - Advanced SQL Queries in MySQL
PPTX
SQL Data Manipulation language and DQL commands
Sql query [select, sub] 4
Group by clause mod
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to oracle functions
Module03
Sql clauses by Manan Pasricha
Its about a sql topic for basic structured query language
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Using of Group By and Having Clause in MySQL
Database Systems - SQL - DDL Statements (Chapter 3/3)
Structured query language(sql)
Module 3.1.pptx
Aggregate Functions,Final
DBMS: Week 07 - Advanced SQL Queries in MySQL
SQL Data Manipulation language and DQL commands

More from Naveen Kumar Veligeti (8)

DOCX
.Net framework components by naveen kumar veligeti
DOCX
Basic CSS concepts by naveen kumar veligeti
DOCX
Php forms and validations by naveen kumar veligeti
DOCX
Why use .net by naveen kumar veligeti
DOCX
Types of sql commands by naveen kumar veligeti
DOCX
Sql data types for various d bs by naveen kumar veligeti
DOCX
Beginners introduction to asp.net
DOCX
.Net framework by naveen kumar veligeti
.Net framework components by naveen kumar veligeti
Basic CSS concepts by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
Why use .net by naveen kumar veligeti
Types of sql commands by naveen kumar veligeti
Sql data types for various d bs by naveen kumar veligeti
Beginners introduction to asp.net
.Net framework by naveen kumar veligeti

Recently uploaded (20)

PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
Classroom Observation Tools for Teachers
PDF
Complications of Minimal Access Surgery at WLH
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Empowerment Technology for Senior High School Guide
PPTX
Introduction to Building Materials
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
advance database management system book.pdf
PPTX
Lesson notes of climatology university.
PDF
RMMM.pdf make it easy to upload and study
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Classroom Observation Tools for Teachers
Complications of Minimal Access Surgery at WLH
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Chinmaya Tiranga quiz Grand Finale.pdf
Hazard Identification & Risk Assessment .pdf
Empowerment Technology for Senior High School Guide
Introduction to Building Materials
Weekly quiz Compilation Jan -July 25.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Digestion and Absorption of Carbohydrates, Proteina and Fats
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
advance database management system book.pdf
Lesson notes of climatology university.
RMMM.pdf make it easy to upload and study
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx

Clauses in sql server

  • 1. Naveen Kumar Veligeti CLAUSES in SQL Server SQL Server provides with the following clauses that can be used in the SELECT statements:     WHERE GROUP BY HAVING ORDER BY The complete syntax of the SELECT statement looks as following: SELECT <select_list> FROM <tname> [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ] ] WHERE Clause: The WHERE clause is a filter that defines the conditions each row in the source tables must meet to qualify for the SELECT. Only rows that meet the conditions contribute data to the result set. Data from rows that do not meet the conditions is not used. SELECT * FROM EMP WHERE JOB='MANAGER' SELECT * FROM EMP WHERE DEPTNO=20 GROUP BY Clause: The GROUP BY clause partitions the result set into groups based on the values in the columns of the group_by_list. For example, the Emp table has 3 values in Deptno column. A GROUP BY Deptno clause partitions the result set into 3 groups, one for each value of Deptno. Ex-1:How to find the highest salaries for each department. Sol: SELECT DEPTNO, MAX(SAL) FROM EMP GROUP BY DEPTNO Ex-2:How to find the highest salaries for each job. Sol: SELECT JOB, MAX(SAL) FROM EMP GROUP BY JOB 1
  • 2. Naveen Kumar Veligeti Ex-3:How to find the highest salaries for each department in it for each job. Sol: SELECT DEPTNO, JOB, MAX(SAL) FROM EMP GROUP BY DEPTNO, JOB Note: While using the GROUP By clause the select_list of the query should contain only the following:    Group Functions or Aggregate Functions Columns used in the Group By Clause Constants. Ex-4:How to find the number of employees working for each department. Sol: SELECT DEPTNO, COUNT(*) FROM EMP GROUP BY DEPTNO Ex-5:How to find the number of employees working for each department only if the number is greater than 3. Sol: SELECT DEPTNO, COUNT(*) FROM EMP GROUP BY DEPTNO HAVING COUNT(*)>3 HAVING Clause: The HAVING clause is an additional filter that is applied to the result set. Logically, the HAVING clause filters rows from the intermediate result set built from applying any FROM, WHERE, or GROUP BY clauses in the SELECT statement. HAVING clauses are typically used with a GROUP BY clause. Ex-6:How to find the number of Clerk’s working for each department. Sol: SELECT DEPTNO, COUNT(*) FROM EMP WHERE JOB='CLERK' GROUP BY DEPTNO Ex-7:How to find the number of Clerk's working for each department only if the count is greater than 1. Sol: SELECT DEPTNO, COUNT(*) FROM EMP WHERE JOB='CLERK' GROUP BY DEPTNO HAVING COUNT(*)>1 ORDER BY clause: The ORDER BY clause defines the order in which the rows in the result set are sorted. order_list specifies the result set columns that make up the sort list. The ASC and DESC keywords are used to specify if the rows are sorted in an ascending or descending sequence. 2
  • 3. Naveen Kumar Veligeti ORDER BY order_list[ ASC | DESC ] SELECT * FROM EMP ORDER BY SAL SELECT * FROM EMP ORDER BY SAL DESC SELECT * FROM EMP ORDER BY SAL, COMM 3