- Lab
-
Libraries: If you want this lab, consider one of these libraries.
Combine Shipment and Inventory Data with SQL in PostgreSQL
In this Code Lab, learners will work through a real-world scenario designed to teach the learning objectives through scaffolded tasks. In a fictional distribution company, operations analysts have discovered discrepancies between warehouse inventory records and outbound shipment reports across multiple regions. Learners will investigate the issue by working with prebuilt PostgreSQL tables and fake operational data, using joins, comparison logic, set operations, and CTEs to reconcile inventory accurately.
Lab Info
Table of Contents
-
Challenge
Introduction
In this lab, you will learn the fundamentals of modifying and retrieving data in a PostgreSQL database using SQL. You'll insert new records, update existing data, delete rows, and use transactions to control when changes are saved or discarded. Along the way, you'll use the
RETURNINGclause to inspect modified data and writeSELECTqueries to verify the results of your work. By the end of the lab, you'll be comfortable performing the core data manipulation operations that form the foundation of everyday database development. Throughout the lab, you will write SQL in the files located in thesqlfolder.To execute a task file and view the results, run:
psql -d postgres -f sql/task-X-Y.sqlFor example:
psql -d postgres -f sql/task-1-1.sqlIf you need to restore the database to its original state, run:
bash db/reset_database.shTo exit the PostgreSQL interactive shell (
psql), run the following command:\q ``` info> This lab experience was developed by the Pluralsight team using an internally developed AI tool. All sections were verified by human experts for accuracy prior to publications. However, content may still contain errors or inaccuracies, and we recommend independent verification. <br></br> To report a problem or provide feedback, [click here](https://help.pluralsight.com/hc/en-us/requests/new). Feedback may be used to improve accuracy in accordance with our Privacy Policy. -
Challenge
Step 1: Insert New Customer Orders
In this step, you will add new customer orders to the order management system.
Customer service representatives regularly enter new orders as customers place purchases. The operations team relies on accurate order records to track fulfillment, inventory planning, and customer communication.
You will use
INSERTstatements to add new rows to theorderstable. Along the way, you will practice inserting individual records, returning inserted rows for verification, and adding multiple records in a single statement.By the end of this step, you will understand:
- How to add new records to a PostgreSQL table
- How to specify values for individual columns
- How to use the
RETURNINGclause to verify inserted data - How to insert multiple rows with a single statement
-
Challenge
Step 2: Update Existing Order Data
In this step, you will correct existing customer order records with targeted
UPDATEstatements.Operational databases often contain records that need to be corrected after review. An order total may be entered incorrectly, a status may be outdated, or a support team may need to update one specific row after confirming new information.
You will use
UPDATEstatements withWHEREclauses to make safe, targeted changes to theorderstable. You will also useRETURNINGto confirm which row was modified.By the end of this step, you will understand:
- How to update existing rows in PostgreSQL
- How to use
WHEREto target one specific order - How to use
RETURNINGto review modified data immediately
-
Challenge
Step 3: Delete Invalid or Cancelled Orders
In this step, you will remove invalid or cancelled orders from the system.
Not every order should remain in an operational database. Cancelled orders, duplicate entries, and invalid records are often removed after review so downstream reporting and fulfillment processes remain accurate.
You will use
DELETEstatements to remove rows from theorderstable and useRETURNINGto confirm which records were deleted.By the end of this step, you will understand:
- How to remove rows from a PostgreSQL table
- How to safely target records with a
WHEREclause - How to use
RETURNINGto review deleted records
-
Challenge
Step 4: Control Changes with Transactions
In this step, you will use transactions to control when database changes are applied.
When working with production systems, it is often necessary to test a change before permanently saving it. PostgreSQL transactions allow you to group one or more operations together and decide whether to keep the changes with
COMMITor undo them withROLLBACK.You will start a transaction, make a change, undo that change, and then commit a separate update. These are common workflows used by database administrators, developers, and support teams when modifying operational data.
By the end of this step, you will understand:
- How to start a transaction with
BEGIN - How to undo changes with
ROLLBACK - How to permanently save changes with
COMMIT - Why transactions help protect data integrity
- How to start a transaction with
-
Challenge
Step 5: Query the Final Order State
In this final step, you will review the current contents of the
orderstable after completing the modifications throughout the lab.After inserting, updating, deleting, and managing transactions, it is important to verify that the data reflects the intended business outcomes. Analysts and support teams often perform final review queries before closing a ticket, approving a deployment, or handing work off to another team.
You will inspect the final table, review important status and pricing fields, and focus on the records that were affected during the lab.
By the end of this step, you will understand:
- How to review the contents of a modified table
- How to inspect key business fields after data changes
- How to verify the records affected by previous operations Congratulations on completing this lab!
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.