This lab introduces basic SQL filtering and sorting techniques using WHERE
, ORDER BY
, and logical operators. It builds upon data inserted in SQL Lab 2 and simulates tasks performed by support engineers or analysts filtering records for reports or diagnostics.
- Who: A reporting analyst tasked with filtering customer orders for a monthly sales report.
- What: Use SQL queries to filter and sort data based on specific business rules.
- Where: The
ShopEZ
database, usingCustomers
andOrders
tables. - When: After data entry and during reporting or data cleanup.
- Why: To extract meaningful subsets of data from larger tables for targeted analysis.
USE ShopEZ; -- Sets the context to the ShopEZ database
Explanation: Ensures all queries target the correct database.
SELECT * FROM Orders -- Selects all columns from the Orders table
WHERE TotalAmount > 100; -- Filters results to only include orders where TotalAmount is greater than 100
Explanation: Filters the orders to find only high-value transactions.
SELECT * FROM Orders -- Selects all columns from the Orders table
ORDER BY OrderDate DESC; -- Sorts results in descending order by OrderDate (newest first)
Explanation: Helps prioritize or review the most recent orders.
SELECT * FROM Orders -- Selects all columns from the Orders table
WHERE TotalAmount > 50 -- Filters to only orders over $50
ORDER BY TotalAmount DESC; -- Sorts the results by TotalAmount from highest to lowest
Explanation: Combines filtering and sorting to find larger transactions and ranks them by size.
SELECT
o.OrderID,
o.OrderDate,
o.TotalAmount,
c.FirstName,
c.LastName
FROM Orders o -- Uses alias 'o' for Orders
JOIN Customers c ON o.CustomerID = c.CustomerID -- Joins Customers to include customer info
WHERE c.FirstName = 'Alice' -- Filters for records where the customer's first name is Alice
ORDER BY o.OrderDate; -- Sorts Alice's orders by date (oldest to newest)
Explanation: Retrieves all orders placed by Alice and displays them in chronological order, useful for customer service reviews or audit trails.
This lab demonstrated how to apply filters and sorting criteria to SQL queries, allowing users to isolate relevant data from large tables. These techniques are essential for operational reporting, quality checks, and business insights.