3. SQL for Data Preparation
Activity 3.01: Building a Sales Model Using SQL Techniques
Solution:
- Open
pgAdmin
, connect to thesqlda
database, and open the SQL query editor. - Use
INNER JOIN
to join thecustomers
table to thesales
table:FROM sales s JOIN customers c ON s.customer_id = c.customer_id
Note that the SQL in Steps 2, 3, and 4 is not complete SQL that you can run in pgAdmin
. They are part of the FROM…JOIN
clause on which the full SELECT
statement will be built. They are created to guide you through the process of forming a complex dataset using JOIN
. If you want to test the SQL, you can make it complete by adding SELECT *
at the start.
- Use
INNER JOIN
to join theproducts
table to thesales
table:FROM sales s JOIN customers c ON s.customer_id = c.customer_id JOIN products p ON s.product_id = p.product_id
- Use
LEFT JOIN
to join thedealerships
table (right table) to thesales
table (left table):FROM sales s LEFT JOIN dealerships...