Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Advanced_SQL_Assignment_with_Tables

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Advanced_SQL_Assignment_with_Tables

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Advanced SQL Assignment with Table Definitions

Scenario:

You are hired as a data analyst for a company that manages customers, their orders, shipments,

and employee relationships. Your task is to use SQL to extract insights, optimize queries, and

analyze

hierarchical relationships using the following tables.

Dataset Definitions and Sample Data

Table: Customers

Schema:

- customer_id: INT (Primary Key)

- first_name: VARCHAR(50)

- last_name: VARCHAR(50)

- age: INT

- country: VARCHAR(50)

Sample Data:

| customer_id | first_name | last_name | age | country |

|-------------|------------|------------|-----|---------|

|1 | John | Doe | 31 | USA |

|2 | Robert | Luna | 22 | USA |

|3 | David | Robinson | 22 | UK |

|4 | John | Reinhardt | 25 | UK |

|5 | Betty | Doe | 28 | UAE |


Table: Orders

Schema:

- order_id: INT (Primary Key)

- item: VARCHAR(50)

- amount: DECIMAL(10, 2)

- customer_id: INT (Foreign Key references Customers.customer_id)

Sample Data:

| order_id | item | amount | customer_id |

|----------|------------|---------|-------------|

|1 | Keyboard | 400.00 | 4 |

|2 | Mouse | 300.00 | 4 |

|3 | Monitor | 12000.00 | 3 |

|4 | Keyboard | 400.00 | 1 |

|5 | Mousepad | 250.00 | 2 |

Table: Shippings

Schema:

- shipping_id: INT (Primary Key)

- status: VARCHAR(50)

- customer: INT (Foreign Key references Customers.customer_id)

Sample Data:

| shipping_id | status | customer |

|-------------|------------|----------|
|1 | Pending |2 |

|2 | Pending |4 |

|3 | Delivered | 3 |

|4 | Pending |5 |

|5 | Delivered | 1 |

Table: Employees

Schema:

- employee_id: INT (Primary Key)

- name: VARCHAR(50)

- position: VARCHAR(50)

- manager_id: INT (Foreign Key references Employees.employee_id)

- department: VARCHAR(50)

- salary: DECIMAL(10, 2)

Sample Data:

| employee_id | name | position | manager_id | department | salary |

|-------------|----------|-----------------|------------|--------------|------------|

|1 | Alice | CEO | NULL | Management | 250000.00 |

|2 | Bob | CTO |1 | Technology | 200000.00 |

|3 | Charlie | CFO |1 | Finance | 200000.00 |

|4 | David | Lead Developer | 2 | Technology | 150000.00 |

|5 | Eve | Developer |4 | Technology | 120000.00 |

|6 | Frank | Accountant |3 | Finance | 100000.00 |

Assignment Tasks:
1. Data Retrieval Using Joins:

a. Write a query to list all customers and their corresponding order details. Include customers who

haven't placed any orders.

b. Create a report showing all employees and their respective managers. Include employees who

don't have a manager.

2. Combining Queries:

a. Combine the results of customers who have either placed orders or have shipments, avoiding

duplicate entries. Include only their customer_id, first_name, and last_name.

3. Query Optimization:

a. Write an optimized query to find the total amount of orders placed by each customer. Display

the customer name and total amount.

4. Working with Hierarchical Data:

a. Write a query to create a hierarchy of employees, showing their name, position, and their

manager's name.

5. Full Outer Join and Exclusions:

a. Simulate a FULL OUTER JOIN between the Customers and Orders tables and retrieve all

records.

6. Advanced Challenge:

a. Write a single query to generate a customer report:

- Customer Name.

- Total Order Amount (NULL if no orders).


- Shipment Status (Pending, Delivered, or No Shipments if none).

- A column indicating if the customer is a High-Value Customer (total order amount > 10,000).

Submission Guidelines:

1. Save all queries in a .sql file with comments explaining the logic for each query.

2. Test your queries against the dataset and ensure they produce correct results.

3. Include screenshots of query outputs if possible.

You might also like