0

I am working on a SQL assignment in Oracle. There are two tables.

table1 is called Person10:

fields include: ID, Fname, Lname, State, DOH, JobTitle, Salary, Cat.

table2 is called StateInfo:

fields include: State, Statename, Capital, Nickname, Pop2010, pop2000, pop1990, sqmiles.

Question:

Create a view named A10T2 that will display the StateName, Capital and Nickname of the states that have at least 25 people in the Person10 table with a Cat value of N and an annual salary between $75,000 and $125,000. The three column headings should be StateName, Capital and Nickname. The rows should be sorted by the name of the state.

What I have :

CREATE VIEW A10T2 AS
SELECT StateName, Capital, Nickname
FROM STATEINFO INNER JOIN PERSON10 ON
     STATEINFO.STATE = PERSON10.STATE
WHERE Person10.CAT = 'N' AND 
      Person10.Salary in BETWEEN (75000 AND 125000) AND 
      count(Person10.CAT) >= 25
ORDER BY STATE;

It gives me an error saying missing expression. I may need a group expression... but i dont know what I am doing wrong.

1
  • Can you post the detailed error message please? Commented Apr 14, 2016 at 19:04

4 Answers 4

1

Yeah I originally messed this up when I first answered this because it was on the fly and I didn't have a chance to test what I was putting down. I forgot using a GROUP BY is more suited for aggregate functions (Like SUM, AVG and COUNT in the select) and that's probably why it's throwing the error. Using a ORDER BY is probably the correct option in this case. And you want to order your results by the state so you would use StateName.

SELECT S.StateName, S.Capital, S.Nickname
FROM STATEINFO S
INNER JOIN PERSON10 P ON S.STATE = P.STATE
WHERE P.CAT = 'N' 
AND P.Salary BETWEEN 75000 AND 125000 
ORDER BY S.StateName
HAVING count(P.CAT) >= 25;
Sign up to request clarification or add additional context in comments.

4 Comments

Hi. thanks for the solution but I am still getting an error saying "not a GROUP BY expression"
This would be a much better answer if you explained it somewhat. Answers that consist of only code are not usually helpful to other users.
Ehh..still giving me error: "not a group by expression"
Sorry I forgot using a GROUP BY usually relates to an aggregate function like AVG or SUM in the select. That should be why it's throwing an error so using ORDER BY S.StateName should work. Please let me know if you're still having issues because I tried to do this on the fly without testing it and I'm realizing that wasn't such a good idea.
0

Try moving your count() to HAVING instead of WHERE. You'll also need a GROUP BY clause containing StateName, Capital, and Nickname.

I know this link is Microsoft, not Oracle, but it should be helpful.

https://msdn.microsoft.com/en-us/library/ms180199.aspx?f=255&MSPPError=-2147217396

Comments

0

I'm no Oracle expert, but I'm pretty sure

Person10.Salary in BETWEEN (75000 AND 125000)

should be

Person10.Salary BETWEEN 75000 AND 125000

(no IN and no parentheses). That's how all other SQL dialects I know of work.

Also, move the COUNT() from the WHERE clause to a HAVING clause:

CREATE VIEW A10T2 AS
SELECT StateName, Capital, Nickname
FROM STATEINFO INNER JOIN PERSON10 ON
     STATEINFO.STATE = PERSON10.STATE
WHERE Person10.CAT = 'N' AND 
      Person10.Salary BETWEEN 75000 AND 125000
ORDER BY STATE
HAVING count(Person10.CAT) >= 25;

Comments

0

You can try using a Sub Query like this.

CREATE VIEW A10T2 AS
SELECT statename, capital, nickname
FROM stateinfo
WHERE statename IN (SELECT statename 
                    FROM person10 
                    WHERE Cat = 'N'
                    AND Salary BETWEEN 75000 AND 125000
                    GROUP BY statename
                    HAVING COUNT(*) >= 25)
ORDER BY statename

2 Comments

Thank you so much. I never thought this can be done by nested select statement.
Hi @AyazAmir if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.

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.