How to do it...

To avoid look ups on three different tables using three statements, you can use JOIN to club them. The important thing to note here is to join two tables, you should have one, or more, common column to join. You can join employees and dept_manager based on emp_no, they both have the emp_no column. Though the names don't need to match, you should figure out the column on which you can join. Similarly, dept_mgr and departments have dept_no as a common column.

Like a column alias, you can give table an alias and refer columns of that table using an alias. For example, you can give employees an alias using FROM employees AS emp and refer columns of the employees table using dot notation, such as emp.emp_no:

mysql> SELECT  emp.emp_no, ...

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.