error when joining 2 tables - sql

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.

Related

Outer join for Alias name and column name -Oracle

I had a working sample query earlier in my code as mentioned below.
SELECT DISTINCT
nombre_aplicacion,
APLICACION,
NOMBRE_APLCODE,
DESCRIPCION,
AREAFUNC
FROM (
select **CODAPLICATION nombre_aplicacion**,
APLICACION,
NOMBRE_APLCODE,
DESCRPTION,
AREAFUNC
from admin.VW_APLICACIONES#dblink,
admin.VW_PRODUCTOS#dblink
where **nombre_aplicacion (+) = CODAPLICATION**
)
WHERE 1=1
ORDER BY nombre_aplicacion ASC;
When I try similar type of query with different tables I was getting error as invalid ORA-00904: "NOMBRE_APLICACION": invalid identifier.
If I remove nombre_aplicacion (+) = CODAPLICATION in where condition query is fetching the result. Can any one suggest why I was facing error as its working earlier with sample query and I was getting error? Is this join is valid?
The query is not valid as:
In the inner sub-query you select areafunc and in the outer query you use area which does not appear in the inner sub-query so will not be available.
In the inner sub-query, you define CODAPLICATION to have the alias nombre_aplicacion and then you try to use that alias in the WHERE clause as a join condition; that will not work.
You have not described which column belongs to which table but you want something like:
SELECT DISTINCT
a.codaplication AS nombre_aplicacion,
a.aplicacion,
a.nombre_aplcode,
p.descrption,
p.areafunc
from APLICACIONES a
LEFT OUTER JOIN PRODUCTOS p
ON (a.primary_key_column = p.foreign_key_column)
ORDER BY nombre_aplicacion ASC;
Note: you are going to have to correct the code to give the correct table aliases for each column and give the correct columns for the join condition.

Oracle [Error] Execution (31: 5): ORA-00904: invalid identifier

I have written the following query
SELECT TPD.*,
FMP.*
FROM TABLE TPD
LEFT JOIN
(SELECT *
FROM TBL FMP
WHERE FMP.FE6_MILEPOST_SEQ_I = 0)
ON FMP.FE6_MILEPOST_I = TPD.CURR_STATION_MP_I
The two sub queries inside works separately but when joined, it throws the following error
[Error] Execution (31: 5): ORA-00904: "FMP"."FE6_MILEPOST_I": invalid identifier
You have to alias your derived table, e.g. like this:
(SELECT FMP.FE6_MILEPOST_I,
FMP.OPERATIONAL_REGION_C,
FMP.OPERATIONAL_REGION_N,
FMP.OPERATING_ZONE_C,
FMP.OPERATING_ZONE_N,
FMP.OPERATING_SUBDIVISION_C,
FMP.OPERATING_SUBDIVISION_N
FROM TBL FMP
WHERE FMP.FE6_MILEPOST_SEQ_I = 0) FMP
The FMP alias that you gave your TBL within the derived table does not "escape" the derived table's nested namespacing scope. I.e. you cannot "see" that table name from outside. Without giving the derived table any name, you can only reference its projected columns without qualification.
Notice that Oracle is one of the few databases that actually allow you to skip aliasing derived tables in the first place, for quick and dirty querying. Even so, I recommend you always alias your derived tables, even in Oracle.
The subquery needs an alias, so you can refer to columns it returns in the outer query.
But here, I would suggest simplifying the FROM clause. Using a subquery does not see useful anyway, so you could just go:
SELECT ...
FROM TABLE TPD
LEFT JOIN TBL FMP
ON FMP.FE6_MILEPOST_SEQ_I = 0
AND FMP.FE6_MILEPOST_I = TPD.CURR_STATION_MP_I

# of columns in two selected tables or a query of a union query do not match

I have a simple qry that give me error
SELECT *
FROM qry_ExecSum
inner join qry_IDS_IT_Everything on qry_ExecSum.P_Code = qry_IDS_IT_Everything.P_Code
It works good If change the * to column names like this :
SELECT qry_ExecSum.P_Code
FROM qry_ExecSum
inner join qry_IDS_IT_Everything on qry_ExecSum.P_Code = qry_IDS_IT_Everything.P_Code
All individual queries run good!!
Hope you are receiving error The multi-part identifier could not be bound, since both tables have the column named P_Code.
To avoid this you can specify the table name or table alias infront of the column name in the SELECT. That is the reason the second query doesn't return the error.

ORA-00904: "ADDRESS_USAGES"."AUS_PRO_REFNO": invalid identifier

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;

Using a select statement as criteria for an update query

I'm trying to put together a query that updates a field within a table. I'm attempting to run a sub select query that gives me a number, and then use that number that resulted from the sub-query as part of the criteria for the update query.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =[tbl_Foundation_Account].[Client_ID]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID =
[2_Import_tbl_AWCDSU].[ECPD Profile ID]))
My issue is I keep receiving this error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
Am I using the sub-query incorrectly? When I've received this error before, it's been because of some ambiguity in the table or field names, but this time I've checked for all that and it should be fine. Can anyone explain what SQL sin I have committed?
On the error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
This is because the table column [tbl_Foundation_Account].[Client_ID] does not exists in the scope of outer UPDATEquery .
The only table the outer query has an inkling about is [2_import_VZW_tbl_SMTN] and it does not have a column like [tbl_Foundation_Account].[Client_ID].
It is akin to writing a column name with a typo or like you said
When I've received this error before, it's been because of some
ambiguity in the table or field names
Please try a query like below.
Note that I am using Inner query syntax and ensuring that a single value is returned by using
select top 1 [Client_ID]
in the inner query. rest of the query syntax is same.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =
(
select top 1 [Client_ID]
from [tbl_Foundation_Account]
where [Foundation_Account_ID] =
(
Select TOP 1 a.Foundation_Account_ID
FROM tbl_Foundation_Account a
INNER JOIN [2_Import_tbl_AWCDSU] b
ON a.Foundation_Account_ID = b.[ECPD Profile ID]
)
)
Another poster submitted this answer earlier, but then deleted it. I was able to try it before they deleted it and it works exactly how I needed it to work. I will use this as the right answer unless someone else can tell me why this is a bad Idea.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] = [tbl_Foundation_Account].[Client_ID]
from [tbl_Foundation_Account]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID = [2_Import_tbl_AWCDSU].[ECPD Profile ID]))