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!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

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.

SQL Print 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;

SQL Print Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

When should I use PRINT instead of SELECT while debugging SQL?

Use PRINT when you only need to display messages, variables, or intermediate values to the console without returning a formal result set. Because PRINT does not query tables or alter the output of your main statement, it is ideal for tracing stored-procedure flow, confirming that conditional branches are reached, or surfacing user-friendly status updates.

Does the PRINT statement affect query performance or final result sets?

PRINT has negligible performance overhead because it merely writes a string to the output buffer and does not execute any data-retrieval logic. It does not change or append to your result sets, so your calling application—or BI tool—receives the same rows it would have without the debugging lines.

How can Galaxy’s SQL editor make working with PRINT statements easier?

Galaxy highlights PRINT statements in its syntax-aware editor and streams their output into a dedicated, searchable console. This lets developers watch execution flow in real time, quickly iterate on stored procedures, and share annotated queries with teammates—without cluttering Slack or Notion threads.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.