The Wayback Machine - https://web.archive.org/web/20240619072234/https://www.geeksforgeeks.org/sql-query-to-find-all-the-students-with-marks-greater-than-average-marks/
Open In App

SQL Query to Find all the Students with Marks Greater than Average Marks

Last Updated : 19 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Query in SQL is like a statement that performs a task. Here, we need to write a query to find all the students whose marks are greater than the average marks of students.

We will first create a database named “geeks” then we will create a table “Students” in that database.

Create a Database:

We can create a Database using the command:

Syntax: CREATE DATABASE DATABASE_NAME;

So let’s create a geeks database as shown below:

CREATE DATABASE geeks;

Using Database:

Use the below command to use the geeks database:

USE geeks;

Adding table into Database:

To add a table into the database we use the below command:

Syntax: CREATE TABLE table_name (Attribute_name datatype...);

So, let’s create a students table within the geeks database as shown below:

CREATE TABLE Students(
Id int,
Name varchar(20),
TotalMarks int);

Here Table Added Successfully.

To see the description of the table:

EXEC sp_columns Students;

Inserting values into Tables:

For inserting records into the table we can use the below command:

Syntax: INSERT INTO table_name(column1,
                column2,
                column 3,.....)
                 VALUES( value1,
                value2,
                value3,.....);

So let’s add some records to the students table:

INSERT INTO Students VALUES (1,'Neha',90);
INSERT INTO Students VALUES (2,'Sahil',50);
INSERT INTO Students VALUES (3,'Rohan',70);
INSERT INTO Students VALUES (4,'Ankita',80);
INSERT INTO Students VALUES (5,'Rahul',65);
INSERT INTO Students VALUES (6,'Swati',55);
INSERT INTO Students VALUES (7,'Alka',75);

Now let us print the data available in the table using the SELECT statement as shown below:

SELECT * FROM department;

Note: Here * represents all. If we execute this query, the entire table will be displayed.

Output :

The query for the data:

Use the below syntax for querying for all students with greater marks than the average of the class:

Syntax:
SELECT column1 FROM table_name
WHERE column2 > (SELECT AVG(
                            column2) 
FROM table_name);

Now use the above syntax to make the query on our students table as shown below:

SELECT Name FROM Students WHERE TotalMarks > (SELECT AVG(TotalMarks) FROM Students);

Output:


Similar Reads

SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to find Employees With Higher Salary than Their Department Average ?
Analyzing and understanding salary distributions within departments is a critical aspect of human resource management. In this article, we will explore SQL queries to calculate the average salary for each department and identify employees whose salaries exceed their departmental averages. These queries provide valuable insights for performance eval
4 min read
How to Find Average Marks of Each Subject in SQL?
In SQL, sometimes we need to find the average value of a column based on another column of the table such as finding the subject-wise average marks scored by the students of a class. This involves the use of the GROUP BY clause along with the AGGREGATE function like AVG. The same is depicted in the below article. For this, we use a specific kind of
2 min read
How to Find Average Marks of Each Student in SQL?
In SQL, sometimes we need to find the average value of a column based on another column of the table such as finding the student-wise average marks scored by him/her in all the subjects. This involves the use of the GROUP BY clause along with the AGGREGATE function like AVG. The same is depicted in the below article. For this article, we will be us
2 min read
SQL Query to find an employee whose salary is equal to or greater than a specific number
In this article, we will discuss the overview of SQL query and our main focus will be on how to Find an Employee whose salary is equal to or greater than a specific number in SQL with the help of examples. Let's discuss it one by one. Introduction :SQL (Structured Query Language ) is used to maintain structured data in relational databases and this
3 min read
SQL Query to Find the Average Value in a Column
In this article, we are going to see how to find the average value in a column in SQL. A column in the SQL table is the vertical catalog structure. In this article, we will be using the Microsoft SQL Server as our database. For the purpose of example, we will be creating a sample table and performing the same operations on it. Table Definition: We
3 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
SQL Query to Add Email Validation Using Only One Query
In this article let us see how can we check for the validation of mails in the student database using MSSQL as the database server. For example, if the email is like abcdedfABCDEF...0123456@gmail.com this is the valid form of an email if it is other than this then that is said to Invalid. So, now we will discuss this concept in detail step-by-step:
2 min read
SQL Query to Find Names of the Employees Whose Department Have Number of Employees Less than 2
In SQL, we need to find out the department wise information from the given table containing information about employees. One such data is the name of employee who belong to a department having less than 2(i.e. only 1) employees. We shall use the GROUP BY, IN and COUNT clause to achieve this. This is illustrated below. For this article, we will be u
2 min read
SQL Server Query to Find All Permissions/Access for All Users in a Database
Permissions in SQL Server determine what actions a user can perform on database objects. There can be multiple users in a database, so keeping track of granted permissions is necessary to keep the database safe. In this article, we will look at methods to find all permissions/access for all users in a database. How to Find All Permissions/Access fo
2 min read
Article Tags :