1

I have 2 Tables - Department and Employees. I am trying to display department that hire at least 1 employee which must be sorted by department id

Department Table

create table department (
      dept_id integer not null,
      dept_name varchar(30) not null,
      dept_location varchar(30) not null,
      unique(dept_id)
  );

Employee Table


  create table employee (
      emp_id integer not null,
      emp_name varchar(50) not null,
      dept_id integer not null,
      salary integer not null,
      unique(emp_id)
  );

I have written this SQL query

SELECT  department.dept_id, sum(salary),count(*) 
FROM employee 
     INNER JOIN department
       ON employee.dept_id =department.dept_id
        GROUP BY department.dept_id
          HAVING COUNT(*) > 0;
ORDER BY department.dept_id

I am getting

syntax error at or near "ORDER"
1
  • Delete ; before order by Commented Jun 14, 2019 at 19:02

1 Answer 1

1

You have the order by clause in wrong position ..

the order must be the last clause

SELECT  department.dept_id, sum(employee.salary), count(*) 
FROM employee 
     INNER JOIN department
       ON employee.dept_id =department.dept_id
        GROUP BY department.dept_id
          HAVING COUNT(*) > 0
ORDER BY department.dept_id; 

this because the ORDER BY just work for result presentation

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

7 Comments

.. try execute the code .. the error message in based on wrong sequence of the clause .. and during the parsing the inner join is in an wron postion respected thye previsous code .. but the reason is the wrong position of the ORDER BY clause ..
Use the table's name: ORDER BY department.dept_id
if you have ambiguity .. then just add the table name to the order by ... answer updated ..
@forpas thanks for the suggestion .. upvote somewhere
@Karan .. update your question and show me you exect error message
|

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.