Inner Join Invalid Object Name - sql

Trying to do a simple inner join query but keep getting errors
Select Alias from Db.SchmaName.Customers as Customer
Select Alias from Db.SchmaName.Orders as Order
Inner Join Order on Customer.Alias = Order.Alias
The Db and SchmaNames are both exactly the same but Alias column is different between the 2 tables.
Both of the select statements work as expected but the error for the Inner Join is
"Invalid object name 'Order'"
I get this error even if I do not use the table alias and instead type out the full path to the table

I'm surprised that's the error you're getting. ORDER is a reserved word in SQL Server, don't use it for your object and alias names, you should be getting the error:
Incorrect syntax near 'order'.
Don't use reserved words (or even key words really) for object names, or if you have to, quote them ([Order]). Personally I would do:
SELECT Alias --This is missing an alias as well, so would be ambigous
FROM Db.SchmaName.Orders O
INNER JOIN Customer C ON C.Alias = O.Alias;
Aliases are great for making your SQL succinct. Aliasing the table Customers as Customer, doesn't really do that. A lot of people (like myself), tend to use a few characters to alias a table. So, for Customer I used C. Personally, I tend to use the letters of specific words in the object's name. For example, if I have a table called StockItem I would probably use SI. For a table I have called ic_BD_HPT1 I use HPT1.

Those are two queries, not one.
Select Alias from Db.SchmaName.Customers as Customer
Select Alias from Db.SchmaName.Orders as Order
Inner Join Order on Customer.Alias = Order.Alias
Is actually
Select Alias from Db.SchmaName.Customers as Customer;
Select Alias from Db.SchmaName.Orders as Order
Inner Join Order on Customer.Alias = Order.Alias;
The second query is invalid, as it tries to join the Order alias to itself.
To join two tables you need to specify the tables themselves in the FROM, JOIN statements. If you want to select a column that exists in both tables you have to explicitly specify which one you want, eg:
Select Order.Alias
from Db.SchmaName.Orders as [Order]
Inner Join Db.SchmaName.Customers as Customer
on Customer.Alias = [Order].Alias;
ORDER is a keyword which means you need to either enclose it in square brackets or use a better alias. In fact, Orders is just fine :
Select Orders.Alias
from Db.SchmaName.Orders
Inner Join Db.SchmaName.Customers as Customer
on Customer.Alias = Orders.Alias;

use different table alias name rather key word of SQL(Order),another issue Alias column is exist in your both table so use table.column in selection and
your join need like below
Select Customer.Alias from Db.SchmaName.Customers as Customer
inner join Db.SchmaName.Orders as O
on Customer.Alias = O.Alias

Order is a Keyword in SQL. You can not use Keywords in SQL for aliases, except if you put [] around the alias.
I would use a different name for the alias.

There are two queries in your question.
This is one
Select Alias from Db.SchmaName.Customers as Customer
And this is the second one
Select Alias from Db.SchmaName.Orders as Order
Inner Join Order on Customer.Alias = Order.Alias
The error you are getting is on the second query,
You are trying to JOIN against the Alias you've given to the table Orders, i.e ORDER. What you should do instead is reference the Alias on the ON clause, like so
Select Order.Alias, Customer.Alias
from Db.SchmaName.Orders as Order
INNER JOIN Db.SchmaName.Customers as Customer on Order.Alias = Customer.Alias

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.

ORA-00904 for one INNER JOIN but not for another

for one particular query I've written up the ORA-00904 error has occurred
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.SHORTDESC = R.SHORTDESC
INNER JOIN COLOURTYPE0698 C
ON M.COLOURNAME = C.COLOURNAME
ORDER BY Title ASC;
specfically for ON **M.**SHORTDESC = R.SHORTDESC
However, the previous query I had just written beforehand that used an INNER JOIN as well
SELECT '103040698' as StudID, A.FullName, M.Title, M.RelYear
FROM CASTING0698 C
INNER JOIN ACTOR0698 A
ON C.ActorNo = A.ActorNo
INNER JOIN MOVIE0698 M
ON C.MovieNo = M.MovieNo
ORDER BY RelYear DESC;
this query runs perfectly, so I'm just unclear what I've done wrong.
From what I remember INNER JOINs are written with the child table first then the parent table e.g. ON C.ActorNo = A.ActorNo but I could be wrong since I know some software will accept either way while others are sensitive to it.
for context, I'm using SQLJunior for my school work
here is the whole error
ON M.shortdesc = R.shortdesc
*
Error at line 4: ORA-00904: "M"."SHORTDESC": invalid identifier
In plain English the error message means "the MOVIES0698 table doesn't have a column called SHORTDESC"
Remember that Oracle column names are only case insensitive if they are not enclosed in quotes, or were quoted allcaps. If a column name is enclosed in quotes, and is not all uppercase, when it is created then it must forced more be referred to using quotes
SHORTDESC -- not case sensitive, you could write SELECT shortDesc
"SHORTDESC" -- not case sensitive, because it's all caps anyway, can write SELECT shortdesc
"ShortDesc" -- case sens, must refer to this column using quotes forever more: SELECT "ShortDesc"... JOIN ON M."ShortDesc" etc
In summary, never enclosed oracle column names in quotes, for any reason. If you want to call a column something that is a reserved word, call it something else
If movies has no such column you have to join on a different column (SHORTDESC seems like it would be a text column, which probably makes for a poor join target anyway; is there no other column that is intended to relate movies and ratings better? Such as a "movieid" in the ratings table? It seems to me like one movie could have one or more ratings (some critics rate it high, others low, and you average the scores of many ratings..?)
I was using the wrong column names to join these tables together, i was writing it as
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.SHORTDESC = R.SHORTDESC
INNER JOIN COLOURTYPE0698 C
ON M.COLOURNAME = C.COLOURNAME
ORDER BY Title ASC;
instead of
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.ratingcode= R.ratingcode
INNER JOIN COLOURTYPE0698 C
ON M.colourcode= C.colourcode
ORDER BY Title ASC;
TL:DR
I had to use the proper foreign/primary key for INNER JOIN

Alias in Where clauses and Join on clauses

I thought using an alias in the WHERE clause and JOIN ON clause is illegal because of the logical execution sequence. These two clauses are before the SELECT so the alias is not defined.
However, I read two codes lately they had an alias in these two clauses and they ran without problems.
SELECT w1.id
FROM weather AS w1
JOIN weather AS w2 ON DATEDIFF(w1.recordDate, w2.recordDate) = 1
AND w1.Temperature > w2.Temperature;
This is on leetcode, using an alias in the JOIN clause.
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName = 'Around the Horn'
AND c.CustomerID = o.CustomerID;
This is on W3school, using an alias in the Where clause.
Now, I am confused about the restrictions of using an alias. Any thoughts?
I think you are confusing column aliases and table aliases.
Table aliases are defined in the FROM clause. As a best practice, they should always be used to reference to column names. In your first example, w1 and w2 are table aliases and w1.temperature and w2.temperature are qualified column references.
A column alias is defined in the SELECT clause:
select 'this is a ' as column_alias
The column_alias cannot be used in the ON or WHERE clauses associated with the SELECT.

How to use ISNULL() function along with join

While using the ISNULL function along with aliases of column name that I have given to form a left join, I get an error.
The error is:
I am unable to understand what am I doing wrong. The data is stored in customers table:
The sql code that I am using is:
select ISNULL(c2.name,'N/A') as referredby , c1.name as name
from customers as c1
left outer join customers as c2 on c1.referredby = c2.id
order by referredby;
Don't use isnull()! The standard version is coalesce():
select coalesce(cref.name, 'N/A') as referredby_name, c.name as name
from customers c left outer join
customers cref
on c.referredby = cref.id
order by referredby_name;
I made a two other changes. First, the table aliases better describe the role of the tables. I also changed the column alias to referredby_name, so it doesn't match a column in a table. Neither of these are required; with them, I think the query reads more easily.

Trouble with parenthesis in MS Access for SQL Inner Joins

Have tried the below SQL in MS Access but cannot seem to get it working, anyone got a better idea?
SELECT top 4 Student.STUDENT_DEGREE, Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM (((Student)
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;
Whenever i run this i just get the error:
Syntax error in FROM clause
Group is a reserved word. Enclose that name in square brackets to avoid confusing the db engine. You can also assign an alias for the table name.
FROM
(Student
INNER JOIN Result
ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN [Group] AS g
ON Result.RESULT_GROUP_ID = g.GROUP_ID
I had to guess which tables contain those fields in the last ON clause. If you set up the joins in Design View of the Access query designer, it will help you get the names right. It will also add the parentheses which the db engine requires for any query which includes more than one join.
Also qualify the table sources for the field names in your SELECT list and elsewhere in the query. Here again, the query designer can supply the correct names for you.
Remove the extra set of parentheses around Student:
SELECT top 4 Student.STUDENT_DEGREE,Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM ((Student
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;