Path: blob/main/06. Databases and SQL for Data Science with Python/03. Intermediate SQL/04. Working with Multiple Tables/README.md
8170 views
Working with Multiple Tables
There are 3 main ways of accessing multiple tables in the same query:
Sub-queries, introduced here
Implicit
JOINJOINoperators (INNER JOIN,OUTER JOIN, etc.)
This section will only consider the first two methods, as the latter is covered in the following module: Bonus Module - Advanced SQL for Data Engineering
Multiple Tables with Sub-Queries
To explain sub-queries with multiple tables, the following tables will be used in some examples.
To retrieve only the employee records that correspond to departments in the DEPARTMENTS table:
To retrieve only the list of employees from a specific location:
EMPLOYEEStable does not contain location informationNeed to get location information from
DEPARTMENTStable To do so:SELECT * FROM EMPLOYEES WHERE DEP_ID IN (SELECT DEPT_ID_DEP FROM DEPARTMENTS WHERE LOC_ID = 'L0002') ;
To retrieve the department ID and name for employees who earn more than $70,000:
Implicit Join
To access multiple tables using an Implicit Join, specify 2 tables in the FROM clause, e.g.
The result is a full join (or Cartesian join):
Every row in the first table is joined with every row in the second table
The result set will have more rows than in both tables
To limit the result set, additional operands can be used, such as
This notation can also be shortened by using aliases for table names, so that it becomes:
These aliases can also be used in the select clause itself, in examples such as