Sort Rows In Groups

Create Table

-- Create table called adventurers
CREATE TABLE adventurers (
    -- string variable
    name varchar(255),
    -- integer variable
    age int,
    -- string variable
    race varchar(255),
    -- string variable
    weapon varchar(255)
)

Insert Rows

-- Insert into the table adventurers
INSERT INTO adventurers (name, age, race, weapon)
VALUES ('Fjoak Doom-Wife', 28, 'Human', 'Axe'),
       ('Alooneric Cortte', 29, 'Elf', 'Bow'),
       ('Piperel Ramsay', 35, 'Elf', 'Sword'),
       ('Casimir Yardley', 14, 'Elf', 'Magic')

Sort Rows By One Column

-- Retrieve rows
SELECT * FROM adventurers
-- Order rows by age in ascending order
ORDER BY age ASC
nameageraceweapon
Casimir Yardley14ElfMagic
Fjoak Doom-Wife28HumanAxe
Alooneric Cortte29ElfBow
Piperel Ramsay35ElfSword

Sort Rows By Two Columns

-- Retrieve rows
SELECT * FROM adventurers
-- Order rows, when race is elf, sort by name
ORDER BY CASE WHEN race = 'Elf' THEN name
              -- Otherwise sort by weapon
              ELSE weapon
         END
nameageraceweapon
Alooneric Cortte29ElfBow
Fjoak Doom-Wife28HumanAxe
Casimir Yardley14ElfMagic
Piperel Ramsay35ElfSword