2. SQL for Data Preparation
Activity 2.01: Building a Sales Model Using SQL Techniques
Solution
- Open your favorite SQL client and connect to the
sqlda
database. - Use
INNER JOIN
to join thecustomers
table to thesales
table,INNER JOIN
to join theproducts
table to thesales
table, andLEFT JOIN
to join thedealerships
table to thesales
table. - Now, return all columns of the
customers
table and theproducts
table. Then, return thedealership_id
column from thesales
table, but fill indealership_id
in sales with-1
if it isNULL
. - Add a column called
high_savings
that returns1
if the sales amount was500
less thanbase_msrp
or lower. Otherwise, it returns0
. There are many approaches to this query, but one of these approaches could be as follows:SELECT Â Â c.*, Â Â p.*, COALESCE(s.dealership_id, -1), Â Â CASE WHEN p.base_msrp - s.sales_amount >500 Â Â Â Â Â Â Â THEN 1 Â Â Â Â Â Â ...