0

As an exercise, I am creating a database for a zoo. I have an Animal table, Zoo Location ZLoc and a Moving record Mrec table. The Zloc table has an attribute called region. I am trying to SELECT all animals that are in the 'North' region. Here is what I try, but it doesn't produce the expected results:

SELECT 
    nickname, region 
FROM 
    Animal 
    JOIN MRec USING (Animal_ID) 
    JOIN ZLoc USING (ZLoc_ID)
WHERE region IN ‘North’;

nickname is the attribute for animals name. Mrec acts as the associative between animal and Zloc.

To clarify I am using Oracle SQL Developer And I have made sure the single 'quotes' are correct

9
  • 1
    Please post the error Commented Nov 26, 2013 at 22:20
  • For starters, your quotes around North are wrong. Use normal single-quotes like 'North' Commented Nov 26, 2013 at 22:21
  • 1
    Don't use USING. I know it's cool, but it gets so confusing on more complex queries. Commented Nov 26, 2013 at 22:22
  • The error is: Error starting at line 2 in command: SELECT nickname, region FROM Animal JOIN MRec USING (Animal_ID) JOIN ZLoc USING (ZLoc_ID) WHERE zloc.region IN 'North' Error at Command Line:3 Column:7 Error report: SQL Error: ORA-00904: "ZLOC"."REGION": invalid identifier 00904. 00000 - "%s: invalid identifier" Thank you for the advice so far! Commented Nov 26, 2013 at 22:23
  • 1
    That's an Oracle error. Why have you tagged this mysql? Commented Nov 26, 2013 at 22:24

5 Answers 5

1

IN requires parentheses as in IN ('North'). Also make sure you are using apostrophes rather than multibyte curly quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

I have just tried this and I am still getting an "invalid identifier" error. I do think you are correct with the parenthesis though! Thank you!
1

As the error message states, the issue is with the table structure itself, SQL Error: ORA-00904: "ZLOC"."REGION": invalid identifier 00904. 00000 - "%s: invalid identifier"

Which implies column: Zloc->region is not available.

ORA-00904: invalid identifier
Cause: The column name entered is either missing or invalid.
Action: Enter a valid column name. A valid column name must begin with a letter, be less than or equal to 30 characters, and consist of only alphanumeric characters and the special characters $, _, and #. If it contains other characters, then it must be enclosed in double quotation marks. It may not be a reserved word.

Comments

0

I'm not positive about your data / table setup but to select all the animals from the region(loc_id) north this should do the trick. It will select all the animals then joins on the move table (MRec) and then joins onto the location table (ZLoc). You may have to update the names of the fields but this should pull back your target.

SELECT A.NICKNAME, Z.REGION 
FROM ANIMAL A
LEFT JOIN MRec M ON M.NICKNAME = A.NICKNAME 
LEFT JOIN ZLoc Z ON Z.LOC_ID = M.LOC_ID 
WHERE Z.LOC_ID = 'North'

Comments

0

Try this:

SELECT nickname, region 
FROM Animal inner JOIN MRec
ON animal.animal_ID=MRec.Animal_ID
ON MRec.zloc_ID = zloc.zloc_ID
WHERE zloc.region = 'north' 

Comments

0

This works:

SELECT nickname, region 
FROM Animals 
JOIN MRec ON MRec.Animal_ID = Animals.id 
JOIN ZLoc ON MRec.Zloc = ZLoc.id 
WHERE ZLoc.region LIKE 'North'

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.