SQL Print

Galaxy Glossary

How do you display output in SQL?

The SQL `PRINT` statement is used to display messages or values in a SQL environment. It's primarily for debugging and displaying results outside of the main query output.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The `PRINT` statement in SQL is a crucial tool for debugging and displaying information during the execution of a query. Unlike the `SELECT` statement, which retrieves data from a table, `PRINT` displays messages or values directly to the console or output window. It's particularly helpful when you need to check intermediate results, display error messages, or provide feedback to the user without affecting the main query results. It's often used in stored procedures or functions to provide feedback on the execution flow or to display calculated values. The `PRINT` statement is not used to retrieve data from a table; it's used for outputting messages or values. It's a simple but powerful tool for enhancing the debugging and informational capabilities of your SQL code.

Why SQL Print is important

The `PRINT` statement is essential for debugging and providing informative feedback during SQL operations. It allows developers to track the flow of execution and identify potential issues within stored procedures or functions. This makes troubleshooting and maintenance much easier.

Example Usage


-- Calculate the running total of sales for each order.
CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    OrderDate DATE,
    Sales DECIMAL(10, 2)
);

INSERT INTO Orders (OrderID, OrderDate, Sales)
VALUES
(1, '2023-10-26', 100),
(2, '2023-10-26', 150),
(3, '2023-10-27', 200),
(4, '2023-10-27', 250),
(5, '2023-10-28', 300);

SELECT
    OrderID,
    OrderDate,
    Sales,
    SUM(Sales) OVER (ORDER BY OrderDate) AS RunningTotal
FROM
    Orders;

Common Mistakes

Want to learn about other SQL terms?