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.
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
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 | SUBJECT | MARKS | STUDENT_ID |
SAM | MATHS | 40 | 1 |
JOSEPH | ECONOMICS | 62 | 2 |
DARRELL | ARTS | 78 | 3 |
CHRISTOPHER | MATHS | 78 | 4 |
WALTER | ARTS | 59 | 5 |
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