Students with Highest Mark [duplicate] - sql

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 1 year ago.
Write a query to display the student names and the maximum mark scored by them in any subject, ordered by name in ascending order. Give an alias to the maximum mark as MAX_MARK.
I am not able to find the logic for this. Kindly help me with it. Do it in oracle SQL I am at beginner level in SQL.
SELECT MAX(M.VALUE), S2.SUBJECT_ID,M.STUDENT_ID, S2.SUBJECT_NAME,S2.SUBJECT_CODE
from Mark M INNER JOIN SUBJECT S2
ON M.SUBJECT_ID=S2.SUBJECT_ID group BY S2.SUBJECT_ID,
S2.SUBJECT_CODE, S2.SUBJECT_NAME;
I am getting error with this query if I get this student id with the help of the above query then I can easily solve this question using subquery concept.

You don't need subject there. Question asks Max mark per student, regardless of subject:
SELECT s.Student_Name, MAX(M.VALUE) as MAX_MARK
from Student s
inner Join Mark M on m.student_id = s.student_id
group by s.student_id, s.student_name
order by s.student_name;

Related

SQL Error unknown column in where clause? [duplicate]

This question already has answers here:
How do I use alias in where clause? [duplicate]
(4 answers)
Closed 2 years ago.
Write a query to display hotel id, hotel name, and number of orders taken by hotels that have taken orders more than 5 times. Give an alias name for number of orders as 'NO_OF_ORDERS'.sort the result based on hotel id in ascending order.
Data Base Image
The Query I wrote:
select hotel_details.hotel_id, hotel_name, count(orders.hotel_id) as no_of_orders
from hotel_details join orders on hotel_details.hotel_id = orders.hotel_id
where no_of_orders > 5
group by orders.hotel_id order by no_of_orders;
Error I go:
unkown column 'number_of_orders' in 'where clause'
You need a HAVING clause, not a WHERE clause:
select
hd.hotel_id,
hd.hotel_name,
count(o.hotel_id) as no_of_orders
from hotel_details hd
inner join orders o on hd.hotel_id = o.hotel_id
group by
o.hotel_id
having
no_of_orders > 5
order by
no_of_orders;
The count of orders occurs during the GROUP BY portion of the query, at which point WHERE has already happened. So, assertions on anything from GROUP BY belong in a HAVING clause.

Select All the similar data in order wise [duplicate]

This question already has answers here:
sql ORDER BY multiple values in specific order?
(12 answers)
Closed 2 years ago.
I have a large amount of data in data base.Consider this i have a table of student records and i need to get all the student with A grade first and then the B grade and so on..
It sounds like you want order by:
select t.*
from t
order by t.grade;

Column part cannot have quantifier in oracle [duplicate]

This question already has an answer here:
Cannot have a qualifier in the select list while performing a JOIN w/ USING keyword
(1 answer)
Closed 4 years ago.
Hi I have a schema and am trying to get distinct student id's
The sql I wrote makes sense, but Oracle says
ERROR at line 1:
ORA-25154: column part of USING clause cannot have qualifier
The sql I wrote is
SQL> Select distinct Student.s_id
2 from (Student join Takes using(s_id))
3 join (Instructor join Teaches using(i_id))
4 using (course_id, sec_id, semester, year)
5 where instructor.name = 'evan';
Remove the reference to Student in the select:
select distinct s_id

SQL: Access query unique meets multiple criterias

I have a table containing the columns studentID, course and start_date where studentID is unique.
I want to write a query so that students that haven't gone to any courses with a start date within the year 2010 appears. My problem is that I seem to return all students that have gone to any course that isn't 2010 even though they have attended a course during 2010.
SELECT DISTINCT studentID
FROM attendant
WHERE Startdatum NOT BETWEEN #1/1/2010# AND #12/31/2010#
What I'm really asking is; Is there any way to not show a row if it contains a certain value? How?
Subquery of some sort?
It should be something like:
SELECT DISTINCT studentID FROM attendant
WHERE studentID NOT IN
(SELECT DISTINCT studentID FROM attendant
WHERE Startdatum BETWEEN #1/1/2010# AND #12/31/2010#)
A good way to answer this question is using the having clause:
SELECT studentID
FROM attendant
GROUP BY studentID
HAVING sum(iif(Startdatum BETWEEN #1/1/2010# AND #12/31/2010#, 1, 0)) = 0;
The having clause is counting the number of courses that each student took during the time frame. If there are any, then the filter fails.

ORA-00979: not a GROUP BY expression [duplicate]

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 7 years ago.
I am trying show all the different companies for which students work. However only companies where more than four students are employed should be displayed.
This is what I have so far:
SELECT EMPLOYER, COUNT (STUDENT_ID)
FROM STUDENT
GROUP BY STUDENT_ID
HAVING COUNT (STUDENT_ID) >4;
I keep getting this message:
ERROR at line 1:
ORA-00979: not a GROUP BY expression
I don't get it. I also tried this earlier:
SELECT STUDENT.EMPLOYER, COUNT (STUDENT.STUDENT_ID)
FROM STUDENT
GROUP BY STUDENT.STUDENT_ID
HAVING COUNT (STUDENT.STUDENT_ID) >4;
but nothing seems to work. Any help is appreciated. I am on SQL*Plus if that helps.
Try:
SELECT EMPLOYER, COUNT (STUDENT_ID)
FROM STUDENT
GROUP BY EMPLOYER
HAVING COUNT (STUDENT_ID) >4;
- this will return a list of all employers with more than 4 students.
When grouping or including aggregated fields, your select statement should only include fields that are either aggregated or included in the group by clause - in your existing select, you are including EMPLOYER in your select clause, but not grouping by it or aggregating it.