SELECT ,DISTINCT, FROM, WHERE, GROUP BY,HAVING and ORDER BY Commands together in SQL SERVER

In the earlier articles we have seen examples for SELECT, DISTINCT FROM, WHERE, GROUP BY, HAVING and ORDER BY statements.


In this article we will discuss how all the above commands can be combined in a single query.


Syntax for the query containing SELECT, DISTINCT, FROM, WHERE, GROUP BY , HAVING and ORDER BY Commands is:


Syntax :


SELECT  DISTINCT <Column Names>
FROM <TABLE NAME>
WHERE <Condition>
GROUP BY <Column Names>
HAVING <Condition>
ORDER BY <Column Names>


Example:




We have a table with the following data in the table STUDENTS.


NAME SUBJECTMARKSSTUDENT_ID
SAMMATHS401
JOSEPHECONOMICS622
DARRELLARTS783
CHRISTOPHERMATHS784
WALTERARTS595




In order find out the average marks ( avg marks less than 75) among the students who are having marks more than 60 the following query can be using.


SELECT  DISTINCT SUBJECT,AVG(MARKS)
FROM  STUDENTS   
WHERE MARKS > 60
GROUP BY SUBJECT
HAVING AVG(MARKS) <75
ORDER BY SUBJECT

Result Set:
























Result indicates the subjects in which students have to concentrate more

This entry was posted in . Bookmark the permalink.

Leave a reply