Find Students with Marks Greater Than Average Marks



Given task is to find all the students with marks greater than average marks using SQL. i.e. if the marks of students in a class are 65, 75, 89, 92, and, 60 then the average would be 76.2 and, the query should return records with the marks 89 and 92. Let's start step by step.

Creating a Table

First of all, we need to create a table called Students using the CREATE statement. This table has three columns Student_Id, Subject, and Marks. This table stores three types of subjects, and marks range from 0 to 100 ?

CREATE TABLE Students(
   Student_Id    INT NOT NULL,
   Subject       VARCHAR (20) NOT NULL,
   Marks         INT NOT NULL
);

Populate the Table

Let us insert records into this table using the INSERT INTO statement as follows ?

INSERT INTO (Student_Id, Subject, Marks ) Students VALUES 
(1, 'Math', 75), 
(2, 'Math', 82),
(3, 'Math', 95),
(4, 'English', 67),
(5, 'English', 78),
(6, 'English', 88),
(7, 'Science', 100),
(8, 'Science', 92),
(9, 'Science', 79);

Displaying the Data

Now, let's display these 9 records using the SELECT query.

SELECT * FROM Students;

This query after executing will display all 9 records with their 3 columns as follows ?

Student_Id Subject Marks
1 Math 75
2 Math 82
3 Math 95
4 English 67
5 English 78
6 English 88
7 Science 100
8 Science 92
9 Science 79

Finding the Average Marks

Now, let's find out the average Marks of 9 students. This is done using the following query ?

SELECT AVG(marks) as Average_Marks from Students;

Following is the output of this query ?

84

Now, let's retrieve the Student_Id and Marks of all the students who have taken science subject, using the WHERE clause.

SELECT Student_Id, Marks
FROM Students
WHERE subject in ('Science');

Output

Student_Id Marks
7 100
8 92
9 79

Using Sub-query to Calculate the Average Marks

Sub-query is a query that is nested within another query. It is used to perform intermediate calculations to simple complex calculations such as aggregation, filter etc. Below is the sample template to use it.

SELECT column1, column2, ...
FROM table1
WHERE column_name operator (SELECT column_name
FROM table2
WHERE condition);

This is called sub-query where "SELECT column_name FROM table2 WHERE condition is nested in the another query. Let's combine all the above steps from 2 to 5 to write our final query.

SELECT student_id, subject, marks
FROM student
WHERE marks > (SELECT AVG(marks) FROM student);

Output

Student_Id Subject Marks
3 Math 95
6 English 88
7 Science 100
8 Science 92

Let's break down this query ?

  • The sub-query calculates the average marks of all students in the Students table.
    (SELECT AVG(marks) FROM students)
    
  • This query internally in the system will look in this way ?
    SELECT Student_Id, Subject, Marks
    FROM Students
    WHERE marks > 84;
    
  • Now, The "WHERE" clause filters the rows to include only those students whose marks are greater than the average marks (84 in this example). Finally, it will display the names of all the students whose marks are greater than 84 as seen above.

Thank you for going through this tutorial! we hope you are now excited and confident in applying the WHERE clause and sub-queries in your SQL tasks. You are ready to start using these powerful tools in your daily professional and personal projects. Happy querying, and enjoy discovering the insights your data can reveal!

Updated on: 2024-08-18T20:39:14+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements