Skip to content
Advertisement

How to `insert into` with select using `group by` and having a unique constraint key in Oracle?

I am trying to insert into a table, 5 values from other 2 tables, but when I try to insert them, Oracle sends me a message that I am violating the unique key restriction, what I do not understand why I have this problem if I am using the distinct clause in the select.

Thanks

My query:

This is the table that already exist and the unique key statement

I would expect inserting the department_id without problems.

Advertisement

Answer

This is your query:

The problem is the salary in the group by. If you want one row per department, then you can do:

Notes:

  • Never use commas in the FROM clause.
  • Always use proper, explicit, standard JOIN syntax.
  • Use table aliases so your queries are easier to write and read.
  • SELECT DISTINCT is almost never appropriate with GROUP BY.
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement