0

I have a block of Oracle SQL code that does work. I figured out I cannot get the cert column and the max(run_time) at the same time.

select
  cert,
  max(run_time)
from
  bond_films
where title in
(
'Casino Royale','On Her Majesty_s Secret Service','Licence to Kill'
);

I was reading a SQL reference and it has the following syntax, but I thought my code matches the syntax?

SELECT [column,] group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column] ;

Can someone explain this to me or suggest a source which says I cannot write a code like I did?

1 Answer 1

2

You need a GROUP BY clause because you have included the cert column. If you had only done SELECT MAX(run_time) it would not be necessary to supply a GROUP BY since you are selecting the maximum value of all rows. But, since you have requested another column (cert), Oracle expects that you want to retrieve the max value for each different cert.

select
  cert,
  max(run_time)
from
  bond_films
where title in ('Casino Royale','On Her Majesty_s Secret Service','Licence to Kill')
GROUP BY cert;
Sign up to request clarification or add additional context in comments.

2 Comments

Michael, Thank you for your advise. Can you tell me where to find relevent syntax so that I know I have to use GROUP BY in this case?
@Dean I'm sorry I can't find the relevant Oracle docs, and I only know it from experience. MySQL will not require GROUP BY in this instance and would group & return all rows, but Oracle & others require it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.