Karan
Karan

Reputation: 359

Error in MySQL code- Syntax error at Inner join

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"

Upvotes: 1

Views: 784

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

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

Upvotes: 1

Related Questions