Senior
Modify SQL: only employees over 30 years old. Then: only departments with more than 100 employees.
sobes.tech AI
Answer from AI
Suppose we have two entities: Employee and Department. First, we need to filter employees older than 30 years, and then select only those departments that have more than 100 such employees.
Example in Java using Stream API and collections:
import java.util.*;
import java.util.stream.*;
class Employee {
int age;
Department department;
// constructor, getters
}
class Department {
String name;
// constructor, getters
}
// employees - list of all employees
List<Employee> employees = ...;
// Filter employees older than 30
List<Employee> filteredEmployees = employees.stream()
.filter(e -> e.getAge() > 30)
.collect(Collectors.toList());
// Group by departments and count employees
Map<Department, Long> deptCounts = filteredEmployees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
// Select departments with more than 100 employees
Set<Department> largeDepartments = deptCounts.entrySet().stream()
.filter(entry -> entry.getValue() > 100)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
// Final: employees older than 30 working in large departments
List<Employee> result = filteredEmployees.stream()
.filter(e -> largeDepartments.contains(e.getDepartment()))
.collect(Collectors.toList());
This approach allows applying both conditions sequentially to obtain the desired result.