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
Related
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;
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.
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;
This question already has answers here:
Get top 1 row of each group
(19 answers)
sql server select first row from a group
(2 answers)
Closed 3 years ago.
I have 2 columns in a table where column A has distinct records and column b does not i.e. column b could have multiple items of the same value in each row.
What I require is to only show distinct records based on column B. I do need both columns though as it will be an input in Crystal Reports but I don't care what value column A has.
CreatedBy column has multiple different values and I need to get those distinct values.
Query
SELECT DISTINCT nh.InNeoHistoryId, nh.CreatedBy
FROM NeoHistory nh
GROUP BY nh.CreatedBy, nh.InNeoHistoryId
Result
Try below query
SELECT nh.CreatedBy,min(nh.InNeoHistoryId)
FROM NeoHistory nh group by nh.CreatedBy
Just make a small tweak to your query:
SELECT MIN(nh.InNeoHistoryId), nh.CreatedBy
FROM NeoHistory nh
GROUP BY nh.CreatedBy
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.