liveBook · Manning

Else clause

As explained in Learn SQL in a Month of Lunches

ELSE is a procedural SQL control-flow keyword that introduces the fallback branch executed when preceding conditions are not satisfied.

The ELSE clause provides the fallback action when an IF condition is false or NULL. Think of it as otherwise: do this when the preferred action cannot happen. ELSE must follow an IF; writing it alone causes a syntax error. section 21.2.2

Skip an incomplete promotion insert

A stored procedure can insert a promotion only when _PromotionCode has a value; otherwise, ELSE returns a useful explanation:\n\nIF _PromotionCode IS NOT NULL THEN\n INSERT INTO promotion (PromotionID, PromotionCode, PromotionStartDate, PromotionEndDate)\n SELECT _PromotionID, _PromotionCode, _PromotionStartDate, _PromotionEndDate;\nELSE\n SELECT 'No PromotionCode, INSERT skipped.' AS Message;\nEND IF;\n\nCalling AddPromotion with a NULL promotion code returns the message instead of inserting the row. section 21.2.2

Use ELSE in these practical ways:

  • Complete a decision structure after checking several possible failures with ELSEIF; the final ELSE handles the successful insert. section 21.2.3
  • Group several fallback statements inside BEGIN and END, such as an INSERT followed by an “INSERT successful.” message. section 21.2.3
  • Supply a default result in a CASE expression for values that match no WHEN condition, including unknown or NULL values. section 21.1.3
DROP PROCEDURE AddPromotion;
 
DELIMITER //
 
CREATE PROCEDURE AddPromotion (
    IN _PromotionID int,
    IN _PromotionCode varchar(10),
    IN _PromotionStartDate datetime,
    IN _PromotionEndDate datetime
)
BEGIN
 
IF _PromotionCode IS NOT NULL THEN
    INSERT INTO promotion (
    PromotionID,
    PromotionCode,
    PromotionStartDate,
    PromotionEndDate
    )
    SELECT
        _PromotionID,
        _PromotionCode,
        _PromotionStartDate,
        _PromotionEndDate
        ;
ELSE
    SELECT 'No PromotionCode, INSERT skipped.' AS Message;
END IF;
 
END //
 
DELIMITER ;

Inspect a complete AddPromotion procedure that combines IF, ELSE, INSERT, and a fallback message. section 21.2.2

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains syntax, NULL behavior, stored-procedure use, multiple branches, and grouped statements, while connecting row-level alternatives to CASE.
Examples
Uses the AddPromotion procedure, skipped inserts, successful inserts, promotion parameters, and CASE-based title-price messages.
What it assumes
Assumes familiarity with IF conditions and procedural statements while teaching ELSE through plain-language otherwise examples and complete SQL.
What you can do after
Apply ELSE to return fallback messages, skip invalid inserts, provide default CASE results, and complete multi-condition procedures.
How it connects
Links ELSE with IF statements, ELSEIF, stored procedures, BEGIN and END blocks, INSERT statements, and CASE expressions.

Also explained in

Compare how 5 books explain else clause →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.