O'Reilly logo

MySQL 8 Cookbook by Karthik Appigatla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required