Question: ![enrollment table](https://i.stack.imgur.com/SbpuF.png)
![student table](https://i.stack.imgur.com/N8mMe.png)

Hello there, I have a questions for you guys. You see enrollment and student table in images below. I want to run a query getting first and last name from student table but students should belong to a section with less than 5 enrolled students.

Does that make any sense? If i am not clear please do ask me a question. Any help with this is greatly appreciated.

Answer: 
Why access one table more than once, if you can save some work by using analytic functions?

```
select
  first_name, last_name
from 
  (
    select
      s.first_name, s.last_name, count(*) over(partition by e.section_id) as enrollment_count
    from 
      student s
      join enrollment e using (student_id)
  )
where
  enrollment_count < 5;

```
```
select t2.first_name, t2.last_name
from table2 t2
inner join 
    (select student_id from table1 where section_id in 
        (select section_id from 
            (select section_id, count(student_id) 
                from table1
                group by section_id
                having count(student_id) < 5
            )
        )
    )t1
on t1.student_id = t2.student_id;

```

Its not pretty, but it should work.