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
sqldadatabase. - Use
INNER JOINto join thecustomerstable to thesalestable,INNER JOINto join theproductstable to thesalestable, andLEFT JOINto join thedealershipstable to thesalestable. - Now, return all columns of the
customerstable and theproductstable. Then, return thedealership_idcolumn from thesalestable, but fill indealership_idin sales with-1if it isNULL. - Add a column called
high_savingsthat returns1if the sales amount was500less thanbase_msrpor 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 Â Â Â Â Â Â ...