Page 1 of 1

What’s wrong in the following query?

Posted: Sat Jun 25, 2016 7:47 am
by malisha21
SELECT student_code, name
FROM students
WHERE marks =
(SELECT MAX(marks)
FROM students
GROUP BY subject_code);
pls suggest me for this
i am stuck here

Re: What’s wrong in the following query?

Posted: Sat Jun 25, 2016 7:56 am
by doublemax
What's the table structure for "students"?

Re: What’s wrong in the following query?

Posted: Sat Jun 25, 2016 7:52 pm
by iwbnwif
... and what is the problem you are having, and which database are you using (may or may not be relevant)?

Code: Select all

SELECT student_code, name
FROM students
WHERE marks = 
(SELECT MAX(marks)
FROM students
GROUP BY subject_code);
For a start, the GROUP BY clause is probably redundant because MAX(marks) should only return a single result in the sub-query, did you mean:

Code: Select all

SELECT student_code, name
FROM students
WHERE marks = 
(SELECT MAX(marks)
FROM students)
GROUP BY subject_code;

Re: What’s wrong in the following query?

Posted: Tue Jun 28, 2016 9:01 am
by marcelinux
malisha21 wrote:SELECT student_code, name
FROM students
WHERE marks =
(SELECT MAX(marks)
FROM students
GROUP BY subject_code);
An agregate function need explicit all others columns in the table:
... GROUP BY subject_code, student_code, name;
isn't it?