sqloracleoracle10gora-00918

ORA 00918- Column ambiguosly defined error


There are two tables in my Oracle database.

First table is (customers)-

customer_id Customer_name Customer_age Customer_address salary
103 Giriraj Rathi      22   Kolkata    12000
100 Subir Adhikari     22   Bolpur     10000
101 Rakesh Chatterjee  21   Tarkeshwar 8000
102 Jayanta Patra      20   Tarkeshwar 9000
104 Abhi Karmakar      22   Burdwan    8000
105 Mainak Manna       21   Burdwan    9000
106 Subho Gupta        20   Kolkata    10000
107 Aritra Das         23   Kolkata    7000
108 Pradip Paul        22   Kolkata    5000
109 Sourav Banerjee    22   Bolpur     9000

Second table is (Orders):

Order_id Order_date   customer_id  amount

200 12-03-13    100 1100
201 09-05-13    101 1400
202 07-04-12    103 2500
204 29-05-13    104 2400
203 09-02-13    105 9000
205 18-06-13    106 2100
206 09-07-13    107 1600
207 18-05-13    108 2900
209 18-04-13    109 2400

Now I wanted to join both the tables. So I used the query:

select customer_id,
       customer_name,
       customer_address, 
       order_id,order_date, 
       amount 
  from customers, 
       orders 
 where customers.customer_id=orders.customer_id; 

I Googled about the error and found this happens when there is ambiguity in the SQL code itself, but in this case I see nothing.


Solution

  • It is always a good idea to add the table name/alias to the column like this

    select c.customer_id,
           c.customer_name, 
           c.customer_address, 
           o.order_id,
           o.order_date,
           o.amount 
    from customers c
    inner join orders o on c.customer_id = o.customer_id
    

    If you don't then the DB don't know which column to take and both tables have a column named customer_id.