SQL Keywords

SQL END

What is the SQL END keyword used for?

END marks the termination of a code block or expression such as BEGIN...END, CASE, or control-flow statements.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL END: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite (within CASE), Snowflake, BigQuery

SQL END Full Explanation

END is a reserved word used to close several kinds of compound statements in SQL. Most commonly it pairs with BEGIN to wrap multiple SQL commands inside stored procedures, triggers, functions, or anonymous code blocks. It also terminates CASE expressions and various control-flow constructs in procedural dialects (IF...END IF, LOOP...END LOOP, WHILE...END WHILE, etc.). The presence of END signals to the parser that the preceding block is complete so execution can proceed. END itself performs no action; its role is purely syntactic. Some dialects require a delimiter (semicolon or slash) after END, and PL/SQL often appends the object name (END procedure_name;) for clarity. Failing to include END where required results in compilation errors such as "missing END" or "syntax error at end of input".

SQL END Syntax

-- Close a BEGIN...END block
BEGIN
    -- statements
END;

-- Complete a CASE expression
CASE
    WHEN condition THEN result
    ELSE default_result
END

-- PL/pgSQL function example
CREATE OR REPLACE FUNCTION add_numbers(a int, b int)
RETURNS int AS $$
BEGIN
    RETURN a + b;
END;
$$ LANGUAGE plpgsql;

SQL END Parameters

Example Queries Using SQL END

-- 1. Using BEGIN...END inside a transaction block
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
END;  -- in PostgreSQL this would be COMMIT instead; shown for block illustration

-- 2. CASE expression with END in a SELECT
SELECT id,
       CASE WHEN balance < 0 THEN 'overdrawn'
            WHEN balance = 0 THEN 'zero'
            ELSE 'positive'
       END AS balance_state
FROM accounts;

-- 3. Stored procedure with END in MySQL
DELIMITER $$
CREATE PROCEDURE transfer(in from_id INT, in to_id INT, in amt DECIMAL(10,2))
BEGIN
    START TRANSACTION;
    UPDATE accounts SET balance = balance - amt WHERE id = from_id;
    UPDATE accounts SET balance = balance + amt WHERE id = to_id;
    COMMIT;
END$$
DELIMITER ;

Expected Output Using SQL END

  • END itself does not emit a resultset
  • It simply tells the database engine that the enclosed block or expression is complete so the statement can be compiled or executed

Use Cases with SQL END

  • Wrapping multiple SQL statements in a single executable block
  • Terminating CASE expressions within SELECT, UPDATE, or INSERT statements
  • Closing stored procedures, functions, triggers, and anonymous blocks
  • Finalizing control-flow structures in procedural SQL languages

Common Mistakes with SQL END

  • Omitting END after a BEGIN or CASE, leading to syntax errors
  • Placing a semicolon before END in dialects where it should follow END
  • Using END without matching BEGIN or other opening keyword

Related Topics

BEGIN, CASE, IF, LOOP, WHILE, COMMIT, ROLLBACK

First Introduced In

SQL-92 standard

Frequently Asked Questions

What does END pair with?

END typically pairs with BEGIN, CASE, IF, LOOP, WHILE, and other control-flow starters to close the construct.

Does END generate any output?

No. END is purely syntactic and produces no rows or messages by itself.

Why do I get a "syntax error at end of input"?

This usually means the parser reached the end of your code without finding the expected END to close a BEGIN or CASE.

Can END be nested?

Yes. Multiple BEGIN...END blocks can be nested. Each BEGIN must have its own corresponding END.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!