title: How to deal with: Error Code 1054. Unknown column 'dept' in 'field list'
body: <p>I went through already: <a href="https://stackoverflow.com/questions/27619026/error-code-1248-every-derived-table-must-have-its-own-alias-no-solution-found/27619065">Error Code: 1248. Every derived table must have its own alias No solution found for query</a>, but still not sure what correct column name its expecting ?</p>
  
  <p>Case-1:</p>
  
  <pre><code>SELECT max(avg_sal), min(avg_sal) 
  FROM (SELECT dept , avg(salary) as avg_sal from Employee e  GROUP BY dept HAVING dept in ('Admin','IT')); 
  </code></pre>
  
  <blockquote>
    <p>12:04:44   SELECT max(avg_sal), min(avg_sal)  FROM (SELECT dept , avg(salary) as avg_sal from Employee e  GROUP BY dept HAVING dept in ('Admin','IT')) LIMIT 0, 1000   Error Code: 1248. Every derived table must have its own alias   0.000 sec</p>
  </blockquote>
  
  <p>Case:2</p>
  
  <pre><code>SELECT max(avg_sal), min(avg_sal) 
  FROM (SELECT dept , avg(salary) as avg_sal from Employee e  GROUP BY dept HAVING dept in ('Admin','IT')) as dept; 
  </code></pre>
  
  <p>then I get </p>
  
  <blockquote>
    <p>SELECT max(avg_sal), min(avg_sal) 
    FROM (SELECT dept , avg(salary) as avg_sal from Employee e  GROUP BY dept HAVING dept in ('Admin','IT')) as dept; </p>
  </blockquote>
  
  <p>Dept</p>
  
  <pre><code>@Entity
  public class Dept {
      @Id
      @GeneratedValue
      private Integer id;
      private String name;
  }
  </code></pre>
  
  <p>Employee</p>
  
  <pre><code>@Entity
  @NamedQuery(name = "Employee.findMaxSalariesByDept", 
              query = "SELECT e.dept, MAX(e.salary) FROM Employee e GROUP BY e.dept.name HAVING e.dept.name in ?1")
  public class Employee {
      @Id
      @GeneratedValue
      private Long id;
      private String name;
      @ManyToOne(cascade = CascadeType.ALL)
      private Dept dept;
      private int salary;
  }
  </code></pre>
  
  <p>Show Table:</p>
  
  <pre><code>CREATE TABLE `employee` (
     `id` bigint(20) NOT NULL AUTO_INCREMENT,
     `name` varchar(255) DEFAULT NULL,
     `salary` int(11) NOT NULL,
     `dept_id` int(11) DEFAULT NULL,
     PRIMARY KEY (`id`),
     KEY `FKfow2bhgypdy2ij4oyukrn6cqw` (`dept_id`)
   ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
  </code></pre>