Skip to content
Advertisement

Is there a way to make this SQL more efficient?

Consider the following tables:

department

employee

Write a query to return the columns empname and deptname of the employees belonging to those departments that have a head count of 4 or more. The records should be returned in alphabetical order of empname

This was my take:

How would you improve on this?

Advertisement

Answer

This is shorter and probably performs faster too

Start with the grouping. You don’t need COUNT from the inner query. Then, join to both tables just to get the names.

INNER JOIN is used because once the count is complete, we already know that

  1. the employees exist
  2. the department exists
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement