Professional Program on Data Analyst
Exercise: Retrieve and Analyze Customer and Order Data with SQLite
Objective:
● Retrieve and analyse customer and order data using SQLite.
Tasks:
1. Retrieve Customer and Order Details
2. Perform Joins to Combine Tables
3. Use GROUP BY and HAVING to Find Top-Selling Products
4. Implement Subqueries to Filter Data
How to Use SQLite
SQLite is a software library that provides a relational database management system. It is a
popular choice for local or embedded databases. SQLite is lightweight and self-contained,
making it an excellent tool for learning SQL and handling small to medium-sized databases.
To use SQLite, you can follow these steps:
1. Install SQLite:
○ Download and install SQLite from the official website.
○ You can also use an online tool like SQLite Online to practise without
installation.
2. Set Up Your Database:
○ Create a new database or use an existing one.
○ Create tables and insert data into your database using SQL commands.
3. Execute SQL Queries:
○ Use a SQLite command-line tool or an online tool to execute SQL queries and
analyse your data.
Task Details
1. Retrieve Customer and Order Details
Query to Retrieve Customer Details: This query will select all columns from the
Customers table.
SELECT * FROM Customers;
This will return all customer details such as CustomerID, CustomerName, ContactName,
Address, City, PostalCode, and Country.
Query to Retrieve Order Details with Customer Names: This query will join the Orders
table with the Customers table to retrieve order details along with the customer names.
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Orders.ShipperID
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
● Orders.OrderID retrieves the Order ID.
● Customers.CustomerName retrieves the Customer Name.
● Orders.OrderDate retrieves the Order Date.
● Orders.ShipperID retrieves the Shipper ID.
2. Perform Joins to Combine Tables
Query to Combine Customers and Orders Tables: This query will perform an inner join to
combine the Customers and Orders tables, retrieving customer names along with their
order details.
SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
● Customers.CustomerName retrieves the Customer Name.
● Orders.OrderID retrieves the Order ID.
● Orders.OrderDate retrieves the Order Date.
3. Use GROUP BY and HAVING to Find Top-Selling Products
Query to Find Top-Selling Products: This query will group the order details by ProductID
and sum the quantities to find the top-selling products.
SELECT OrderDetails.ProductID, SUM(OrderDetails.Quantity) AS TotalUnitsSold
FROM OrderDetails
GROUP BY OrderDetails.ProductID
ORDER BY TotalUnitsSold DESC
LIMIT 3;
● OrderDetails.ProductID retrieves the Product ID.
● SUM(OrderDetails.Quantity) calculates the total units sold for each product.
● GROUP BY OrderDetails.ProductID groups the results by Product ID.
● ORDER BY TotalUnitsSold DESC sorts the results in descending order of total
units sold.
● LIMIT 3 limits the results to the top 3 products.
4. Implement Subqueries to Filter Data
Query to List Customers Who Have Made Purchases in the Last 18 Months: This query
will use a subquery to list customers who have made purchases in the last 18 months.
SELECT CustomerName
FROM Customers
WHERE CustomerID IN (
SELECT CustomerID
FROM Orders
WHERE OrderDate >= DATE('now', '-18 months')
);
● The inner query SELECT CustomerID FROM Orders WHERE OrderDate >=
DATE('now', '-18 months') retrieves the Customer IDs of customers who have
made orders in the last 18 months.
● The outer query SELECT CustomerName FROM Customers WHERE
CustomerID IN (...) retrieves the names of customers whose IDs are in the
result set of the inner query.
Summary
In this exercise, you learned how to:
1. Retrieve customer and order details using SQL queries in SQLite.
2. Perform joins to combine data from multiple tables.
3. Use GROUP BY and HAVING clauses to find top-selling products.
4. Implement subqueries to filter data based on specific conditions.
Dataset for Practice
Use your existing dataset or create sample data to practise these tasks and replicate the
exercise.