Wednesday, January 1, 2025

How can I write a SQL query to show the maximum and minimum marks together from a student table?

 To write a SQL query that shows both the maximum and minimum marks from a student table, you can use the MAX() and MIN() aggregate functions in a SELECT statement. Here's a basic example:

SELECT
    MAX(marks) AS MaxMarks,
    MIN(marks) AS MinMarks
FROM
    student;

Explanation:

  • MAX(marks): Returns the highest value in the marks column.
  • MIN(marks): Returns the lowest value in the marks column.
  • student: The table where the data is stored (assuming the table is named student).

Example output:

MaxMarks MinMarks
98 35

This query will return a single row with the maximum and minimum marks from the marks column in the student table.

If you want the result for each student:

If you'd like to display the maximum and minimum marks for each student, assuming there's a column like student_id or name, you can group by the student identifier. Here's an example:

SELECT
    student_id,
    MAX(marks) AS MaxMarks,
    MIN(marks) AS MinMarks
FROM
    student
GROUP BY
    student_id;

This will return the maximum and minimum marks for each student based on their unique student_id.

No comments:

Post a Comment