I am trying to practice inner joins but in vain.I am getting table does not exist error while executing inner join statements even though I have created tables and data are present
Kindly help me with this.Also, it shows Customers is disconnected from rest of the join graph.
I have tried using double quotes for table names but then it gives different error as
ORA-00904: "CUSTOMERS"."CUSTOMER_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
SELECT
Orders.OrderID,
Customers.CustomerName
FROM
Orders
inner join Customers on Orders.Customer_Id = Customers.Customer_Id;
Its Oracle 11g and SYS is the owner.
The problem could be due to your create table statement,which contains double quoted table names, that are not all in uppercase. Remember, oracle always stores table names and column names in uppercase by default, unless you have used double quoted strings with varying case in the create/Alter statements.Using the whole table names to refer the columns should be avoided, as that is the reason for invalid identifier error in your case.
So, either rename the table from "Customers" to CUSTOMERS and "Orders" to ORDERS if you can, or use quotes for table names in your select query along with appropriate aliases .
SELECT
o.OrderID,
c.CustomerName
FROM
"Orders" o
inner join "Customers" c on o.Customer_Id = c.Customer_Id;
Also, as #horse has already suggested, create table in any sample schema like HR, SCOTT etc and not in SYS.
Related
I am trying to run the following code:
create or replace view StoreContactPurchaseHistoryView AS
select
STORE.StoreName,
StoreContactAndPhone(contact,phone),
PURCHASE_ITEM.PurchaseItemID,
PURCHASE_ITEM.PurchaseDate,
PURCHASE_ITEM.ItemDescription,
PURCHASE_ITEM.PriceUSD
from
store,
purchase_item
left join purchase_item on purchase_item.storeid=store.storeid
;
I keep getting this error:
Error report -
ORA-00904: "STORE"."STOREID": invalid identifier
00000 - "%s: invalid identifier"
Never use commas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax.
That said, you want something like:
select s.StoreName, StoreContactAndPhone(contact, phone) as col,
pi.PurchaseItemID, pi.PurchaseDate,
pi.ItemDescription, pi.PriceUSD
from store s join
purchase_item pi
on pi.storeid = s.storeid;
Note that I also added table aliases so the query is easier to write and to read.
You are mixing explicit and implicit joins, and seem to need only one join:
create or replace view StoreContactPurchaseHistoryView AS
select
s.StoreName,
StoreContactAndPhone(contact,phone),
p.PurchaseItemID,
p.PurchaseDate,
p.ItemDescription,
p.PriceUSD
from
store s
left join purchase_item p on p.storeid = s.storeid
;
I used table aliases to shorten the query.
Side note: it is unclear what expression StoreContactAndPhone(contact,phone) actually refers to; I left it as-is in the query.
Change it to this:
create or replace view StoreContactPurchaseHistoryView AS
select
STORE.StoreName,
StoreContactAndPhone(contact,phone),
PURCHASE_ITEM.PurchaseItemID,
PURCHASE_ITEM.PurchaseDate,
PURCHASE_ITEM.ItemDescription,
PURCHASE_ITEM.PriceUSD
from
store
left join purchase_item on purchase_item.storeid=store.storeid
;
I am trying to run a select statement from two tables, the data that I want to return takes on 3-4 joins to achieve. I am getting the error
ORA-00904: "ADDRESS_USAGES"."AUS_PRO_REFNO": invalid identifier
when both tables and columns exist. I have read the post relating to this error but given that I am just starting out I cant make head nor tail of them. Any suggestions (be gentle). SQL below TIA
select ins_srq_no, adr_line_all from inspections
join properties
on inspections.ins_pro_refno = properties.pro_propref
join addresses
on properties.pro_propref = address_usages.aus_pro_refno
join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;
This on clause is incorrect, because you haven't yet joined to the address_usages table: on properties.pro_propref = address_usages.aus_pro_refno. This is causing Oracle to throw the error you're seeing; it's not about the table or column not existing, it's the fact that within that query, that identifier is invalid because you haven't yet joined to the table.
At a guess (I can't be 100% sure without seeing your table structures and foreign keys), you need to join addresses back to properties. If so, the query should look something like this:
select ins_srq_no, adr_line_all
from inspections
inner join properties
on inspections.ins_pro_refno = properties.pro_propref
inner join addresses
on properties.pro_propref = addresses.[column name of FK to properties]
inner join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;
I keep getting this error trying to run this simple Join.....
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem, client
INNER JOIN client
ON docregitem.nclient = client.nclient
ORA-00904: "DOCREGITEM"."NCLIENT": invalid identifier
I can do a select and all the columns are present and correct...
I think the query you want is:
SELECT dr.reviewdate, dr.nclient, c.name
FROM docregitem dr INNER JOIN
client c
ON dr.nclient = c.nclient;
Your from clause has a comma in it. This is a lot like a cross join, but it affects the columns. These are not known in the on clause, which is what is causing the problem.
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem
INNER JOIN client
ON docregitem.nclient = client.nclient
you try this one as you used client table twice one with simple join and other with inner join without giving the alias name to the table so the compiler is confused in selecting and comparing column from client table.
Can anyone help me with the following statement. I'm new to SQL and maybe overlooking the obvious.
I am trying to run the following query
There is a shipments table with the corresponding columns (PROJECTNO, SUPPLIERNO)
I know I don't the to put SHIPMENTS. in front of the names, but this will eventually end up in a multi table query.
SELECT sup1.SHIPMENTS.PROJECTNO, sup2.SHIPMENTS.PROJECTNO
FROM SHIPMENTS sup1, SHIPMENTS sup2
WHERE sup1.SHIPMENTS.PROJECTNO = sup2.SHIPMENTS.PROJECTNO
AND sup1.SHIPMENTS.SUPPLIERNO <> sup2.SHIPMENTS.SUPPLIERNO;
I keep getting the following error.
ORA-00904: "SUP2"."SHIPMENTS"."SUPPLIERNO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 160 Column: 34
THanks in advance
Your query should be written as:
SELECT sup1.PROJECTNO, sup2.PROJECTNO
FROM SHIPMENTS sup1 JOIN
SHIPMENTS sup2
on sup1.PROJECTNO = sup2.SHIPMENTS.PROJECTNO AND
sup1.SUPPLIERNO <> sup2.SUPPLIERNO;
First, when using an alias there is no need to specify the table name again. Also, you should use explicit join syntax, with an on condition. The implicit syntax, with the condition in the where clause, is supported, but it is less powerful and more difficult to read.
Change all sup2.SHIPMENTS.SUPPLIERNO to sup2.SUPPLIERNO, and do the same for sup1 alias, and for the other colummns.
You must understand aliasing. Once you alias table TAB as T then you should refer to it as simply T.
In your FROM clause, when you say
FROM SHIPMENTS sup1, SHIPMENTS sup2
you are aliasing the SHIPMENTS table as sup1 and sup2 (as 2 instances of same table). So you need to use sup1 and sup2 as the table name, and the real name (SHIPMENTS) isn't valid, and would be ambiguous anyway, so use:
sup1.SUPPLIERNO --refers to SUPPLIERNO column in SHIPMENTS table aliased as sup1
sup2.PRODUCTNO --refers to PRODUCTNO column in SHIPMENTS table aliased as sup2
there is no such thing as sup2.SHIPMENTS.SUPPLIERNO because there is no such thing as a sup2.SHIPMENTS table
Also, a general rule to keep in mind, in Oracle you can use notation like table.column, schema.table or schema.table.column to refer to tables/views or columns. In this case what you've written amounts to table.table.column which doesn't make sense (unless you were using a nested table).
The reason you must use aliasing here is that it is the only way to join a table to itself. If you use the full table name twice, you'll get ambiguity errors. When you self-join a table, Oracle treats each instance (alias) as a distinct table in the query. I prefer to use aliases like a and b to reduce typos.
SELECT a.PROJECTNO, b.PROJECTNO
FROM SHIPMENTS a JOIN SHIPMENTS b USING(PROJECTNO)
WHERE a.SUPPLIERNO <> b.SUPPLIERNO;
Query :
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri
order by Name
The error that i get is :
Error code -1, SQL state 42X01: Syntax error: Encountered "order" at line 4, column 5.
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
Why am i getting an error ?
The error is because you forgot to specify JOIN criteria, like this:
SELECT i.Name, ri.Country, ri.State, ri.City
FROM Information as i
JOIN ResidenceInformation as ri ON ri.column = i.column
ORDER BY Name
You need to replace column with the names of the appropriate columns that link the tables correctly for the output you need.
You should also specify the table alias in your ORDER BY, to protect against an ambiguous column reference error.
You get an error because your syntax is wrong: after join there needs to be on, like this:
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri
on ri.info_id=i.id -- <<< Added a join condition
order by Name
SQL needs to know how to "link up" the rows of the table that you are joining to the row(s) of the other table(s) in the query. I am assuming that ResidenceInformation has a foreign key into Information called info_id.
If the Name is present in both ResidenceInformation and Information, you need to prefix it with the table name or an alias. In fact, it's a good idea to do it anyway for added clarity.
I think you may have forgot to tell the join clause which columns to join against. You need to tell the database how these 2 tables connect to each other. Something like ON i.id = ri.InformationId
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri ON i.id = ri.InformationId
order by i.Name
Also, you may need the table alias in the order by clause, which I've added as well.