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
Related
I am trying to create a query with the top 3 shops that have the highest average revenue amount and retail_type being fashion and shoes. I have to join 3 tables where I have to join 2 to the 1st one. Something seems to be wrong with the syntax below:
Select * from (Select
a.shop_id,
c.retail_type,
avg(b.revenue_amount) as avg_revenue_amount
From
shops a
Left join rev b on a.shop_id = b.shop_id
Left join trans c on b.trans_id = c.trans_id
Where
c.retail_type in (‘fashion’, ‘ shoes ’)
Group by
1,
2
Order by
avg_revenue_amount desc)
limit 10;
I would recommend writing the query as:
select s.shop_id, t.retail_type,
avg(r.revenue_amount) as avg_revenue_amount
from shops s join
rev r
on s.shop_id = r.shop_id join
trans
t
on r.trans_id = t.trans_id
where t.retail_type in ('fashion', 'shoes')
group by s.shop_id, t.retail_type
order by avg_revenue_amount desc
limit 10;
You don't specify your database, but the following are potentially problematic:
Bad quotes in the in clause.
Lack of alias after the derived table.
Use of numeric placeholders in GROUP BY.
Not all of these are necessarily errors, but they could be depending on your database.
The following would not cause a syntax error but might cause other problems:
Spaces around a string for an IN comparison means the comparison might not work.
Use of arbitrary letters for table aliases makes the query really hard to follow.
The WHERE clause turns the LEFT JOINs to inner joins so LEFT JOINs are highly misleading.
The subquery is superfluous.
You have extra bracket on the line:
avg_revenue_amount) desc)
Also you have typo on the second line: a.shop.id -> a.shop_id and another problem with quotes: ‘fashion’, ‘ shoes ’ -> 'fashion', 'shoes'
Check edited query on the SQL Fiddle.
I'm trying to do an extremely simple sql query but am running into an issue with this one column named Group. The Group column is not a key.
The following two statements fail in SQL:
SELECT *
FROM Lib.Stuff st
LEFT JOIN Lib.StuffOptions sto
on st.OptionId=sto.id
order by pa.Group desc, pa.Level
SELECT st.LongName,
st.Group,
sto.Name
FROM Lib.Stuff st
LEFT JOIN Lib.StuffOptions sto
on st.OptionId=sto.id
Both calls fail with the same error: Incorrect syntax near the keyword Group. I can use literally any other column name of Stuff and it works. But only for Group it fails.
Group is a reserved keyword, you need to use square brackets to escape it.
SELECT st.LongName,
st.[Group],
sto.Name
FROM Lib.Stuff st
LEFT JOIN Lib.StuffOptions sto
on st.OptionId=sto.id
Better to avoid using reserved keywords as identifiers
USE pubs
SELECT DISTINCT L.au_fname, L.au_lname
FROM dbo.authors L, dbo.titles B
JOIN dbo.titleauthor C ON dbo.authors.au_id=C.au_id
INNER JOIN dbo.titleauthor C1 ON B.title_id=C1.title_id
WHERE B.price >= 13
ORDER BY L.au_lname ASC
The bolded dbo.authors in this code keeps producing the error below... I clearly don't understand aliases because i cannot for the life of me, determine what is wrong. I tried changing it to "L" but the same error happens. I didn't think a double join would be this hard.
To explain what I am trying to do here. I am trying to get information from table authors to display based on a WHERE statement on table titles. however they only have common PK's on a 3rd table named title author. There for I am trying to join all 3 tables together via the common PK's.
At one location you are using cartesian product and at one location you are using joins..thats not allowed.
You need to use like below- Add join conditions....I dont have ur column names.
USE pubs
GO
SELECT DISTINCT L.au_fname, L.au_lname
FROM dbo.authors L
INNER JOIN dbo.titleauthor C ON L.au_id=C.au_id
INNER JOIN dbo.titles B ON B.title_id=C.title_id
WHERE B.price >= 13
ORDER BY L.au_lname ASC
Just need to ask for your help, I have been having some problems with MS Access, I'm trying to create a nested INNER JOIN to perform a query. All of the fields that I need are showing up, however when I try to add a new entry it gives me an error
Can't Add Records Join key of table is not in record set.
Here's my code:
SELECT
Applicant_ID, Complete_Name, Date_of_Birth, Date_of_Application, Gender,
City_Address, Position_Applied, Civil_Status, Age, Educational_Attainment,
Table_JuniorRecruiter.Junior_Recruiter_ID, Junior_Recruiter_Name, Exam_Remarks,
Table_Exam.Exam_Number
FROM
(Table_Applicant
INNER JOIN
Table_Exam ON Table_Applicant.Exam_Number = Table_Exam.Exam_Number)
INNER JOIN
Table_JuniorRecruiter ON Table_Applicant.Junior_Recruiter_ID = Table_JuniorRecruiter.Junior_Recruiter_ID;
The brackets you have round the first table and its first inner join give a syntax error. Between the ( and the ) needs to be a complete SELECT statement, and you start off with a table name, which no valid SELECT statement will do.
Now I don't actually know what you're trying to do, but this will be valid SQL:
SELECT
Applicant_ID, Complete_Name, Date_of_Birth, Date_of_Application, Gender,
City_Address, Position_Applied, Civil_Status, Age, Educational_Attainment,
Table_JuniorRecruiter.Junior_Recruiter_ID, Junior_Recruiter_Name, Exam_Remarks,
Table_Exam.Exam_Number
FROM
Table_Applicant
INNER JOIN
Table_Exam ON Table_Applicant.Exam_Number = Table_Exam.Exam_Number
INNER JOIN Table_JuniorRecruiter
ON Table_Applicant.Junior_Recruiter_ID
= Table_JuniorRecruiter.Junior_Recruiter_ID
Here I've just taken out the ( and the ). It should be valid SQL (provided the table and column names are correct) but it might not be the actual query you want.
However, I can't see what you'd gain by making the table_applicant and table_exam join a nested subquery: doing that looks totally un-necessary.
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;