2

The following SQL statement works in MySQL but not with Oracle:

SELECT *, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_A

Oracle complaint: "FROM keyword not found where expected"


actually the statement was incorrect, we were not grouping by COLUMN_A but another column instead. actually what we want is this

SELECT *, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_B

this works but gives us only column A and B

SELECT COLUMN_B, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_B

what we want is this, but it doesn't work (group by error)

SELECT COLUMN_B, COLUMN_C .... COLUMN_X, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_B
2
  • 1
    Correct. You'll have to adjust the query. Commented Aug 17, 2010 at 20:00
  • 1
    Could you describe what you are actually trying to achieve. This statement does not really make sense at all Commented Aug 17, 2010 at 21:09

5 Answers 5

3

That's because Oracle requires you to define all the columns not wrapped in an aggregate function (MIN, MAX, COUNT, etc). SQL Server would return a similar error. MySQL's behavior is documented here.

Because your query is using SELECT *, I can't re-write it properly for you. But I also can't guarantee a syntactically correct version would return the same results as you see on MySQL either. Grouping by the same column you want the MAX is quite odd...

Sign up to request clarification or add additional context in comments.

4 Comments

Actually all DBMS except MySQL will refuse to run such a query. MySQL simply returns indeterminate results
actually the statement was incorrect, we were not grouping by COLUMN_A but another column instead. actually what we want is this <code> SELECT *, MAX(COLUMN_A) FROM table_xyz WHERE COLUMN_A <= 100 GROUP BY COLUMN_B this works but gives us only column A and B SELECT COLUMN_B, MAX(COLUMN_A) FROM table_xyz WHERE COLUMN_A <= 100 GROUP BY COLUMN_B what we want is this, but it doesn't work (group by error) SELECT COLUMN_B, COLUMN_C .... COLUMN_X, MAX(COLUMN_A) FROM table_xyz WHERE COLUMN_A <= 100 GROUP BY COLUMN_B </code>
@a_horse_with_no_name: No, SQLite supports the same "hidden column" functionality that MySQL supports. But any real database, yes - you're correct. :)
@Dave: Update your question with example data, output from DESC table_xyz, and expected output.
1

If you want the max() for column_a you don't need the group by at all:

SELECT MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 

Comments

1

In addition to what everyone else is saying, Oracle does not allow mixing * with explicit column definitions in queries:

SQL> select *, table_name from user_tables;
select *, table_name from user_tables
        *
ERROR at line 1:
ORA-00923: FROM keyword not found where expected

Oracle hasn't even looked at the fact that you are trying to get columns outside of those included in the group by clause. Which as others have stated, Oracle will not do.

Comments

1

This doesn't answer your MAX issue, but the only way to follow a '*' with other columns is if you use an explicit reference to a table alias - e.g.

 SELECT e.*, zip_code
 FROM  addresses a,
       employees e
 WHERE e.addressId = a.Id

For the MAX value, you will either need to group by all other columns, or look into analytic functions (plenty of previous answers on Stack Overflow).

Comments

0

Multiple problems. Your GROUP BY clause is backwards. You need to define your GROUP BY by the columns in the *. Also what OMG Ponies said before.

Comments

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.