Country or region and language

    Choose your country or region and your language.

    Suggested for you

      All countries and regions

        Choose your character

        It shows in the top bar and on your profile. Change it any time.

        The Work Gym / Free resources

        SQL commands

        The 29 SQL commands you need for queries, changing data, building tables, transactions and permissions, grouped as DQL, DML, DDL, TCL and DCL. Every example runs on the same free sample database, and each row links to a lesson where your query is checked.

        Every example runs on Kestrel, a fictional online store. Download the database to run them yourself.

        Each row links to a lesson of the SQL course, where you practice it and your answer is checked. The first unit is free.

        Query data (DQL)

        NameWhat it doesExamplePractice
        SELECTReads columns from a table. SELECT * returns every column.
        SELECT name, price
        FROM products;
        SELECT: choose the columns you need
        WHEREKeeps only the rows that meet a condition. Combine conditions with AND and OR.
        SELECT name, price
        FROM products
        WHERE category = 'Kitchen' AND price < 30;
        WHERE: keep only the rows you want
        ORDER BY, LIMITSorts the result, DESC for largest first, and LIMIT keeps the first rows. SQL Server writes SELECT TOP 5 instead.
        SELECT name, price
        FROM products
        ORDER BY price DESC
        LIMIT 5;
        ORDER BY and LIMIT: sort and take the top rows
        DISTINCTRemoves duplicate rows from the result.
        SELECT DISTINCT country
        FROM customers;
        DISTINCT: remove duplicate rows
        LIKEMatches text patterns: % stands for any run of characters and _ for exactly one.
        SELECT first_name, email
        FROM customers
        WHERE email LIKE '%@example.com';
        LIKE: match text patterns
        IN, BETWEENIN matches any value in a list. BETWEEN matches a range and includes both ends.
        SELECT order_id, status, total
        FROM orders
        WHERE status IN ('shipped', 'delivered')
          AND total BETWEEN 50 AND 200;
        AND, OR, IN and BETWEEN: combine conditions
        IS NULLFinds missing values. = NULL never matches anything; use IS NULL or IS NOT NULL.
        SELECT order_id, order_date
        FROM orders
        WHERE shipped_date IS NULL;
        NULL, IS NULL and COALESCE
        GROUP BYReturns one row per group, with aggregates such as COUNT, SUM and AVG.
        SELECT category, COUNT(*) AS products, AVG(price) AS avg_price
        FROM products
        GROUP BY category;
        GROUP BY: totals for each group
        HAVINGFilters groups after GROUP BY. WHERE filters rows before the grouping.
        SELECT customer_id, SUM(total) AS spent
        FROM orders
        GROUP BY customer_id
        HAVING SUM(total) > 200;
        HAVING: filter groups
        JOINCombines rows from two tables where the keys match. JOIN means INNER JOIN.
        SELECT o.order_id, c.first_name, o.total
        FROM orders AS o
        JOIN customers AS c ON c.customer_id = o.customer_id;
        INNER JOIN: combine matching rows
        LEFT JOINKeeps every row of the left table, with NULLs where nothing matches, so customers with no orders still appear.
        SELECT c.first_name, COUNT(o.order_id) AS orders
        FROM customers AS c
        LEFT JOIN orders AS o ON o.customer_id = c.customer_id
        GROUP BY c.customer_id, c.first_name;
        LEFT JOIN: keep rows without a match
        UNIONStacks two results with the same columns and removes duplicates. UNION ALL keeps them.
        SELECT email FROM customers
        UNION
        SELECT contact_email FROM suppliers;
        UNION and UNION ALL
        CASE WHENIf-then logic inside a query.
        SELECT name,
          CASE WHEN price >= 50 THEN 'premium' ELSE 'standard' END AS tier
        FROM products;
        CASE WHEN: if-then logic
        SubqueryA query inside another query, here to compare each price with the average.
        SELECT name, price
        FROM products
        WHERE price > (SELECT AVG(price) FROM products);
        Subqueries: a query inside a query
        WITH (CTE)Names a step so the next query can read it like a table.
        WITH spend AS (
          SELECT customer_id, SUM(total) AS spent
          FROM orders
          GROUP BY customer_id
        )
        SELECT * FROM spend ORDER BY spent DESC;
        CTEs: name a step with WITH
        OVER (window)Calculates across related rows without collapsing them, here a price rank within each category.
        SELECT name, category, price,
          RANK() OVER (PARTITION BY category ORDER BY price DESC) AS price_rank
        FROM products;
        Window functions: ROW_NUMBER, RANK and DENSE_RANK

        Change data (DML)

        NameWhat it doesExamplePractice
        INSERTAdds rows to a table. List the columns so the values go where you expect.
        INSERT INTO products (product_id, name, category, price, cost, units_in_stock)
        VALUES (21, 'Oak Serving Tray', 'Kitchen', 38.00, 14.50, 30);
        INSERT: add rows
        UPDATEChanges values in existing rows. Without WHERE it changes every row.
        UPDATE products
        SET price = price * 1.05
        WHERE category = 'Decor';
        UPDATE: change rows
        DELETERemoves rows. Run the same WHERE in a SELECT first to see what will go.
        DELETE FROM signups_raw
        WHERE email IS NULL;
        DELETE: remove rows

        Define tables (DDL)

        NameWhat it doesExamplePractice
        CREATE TABLECreates a table with its columns, data types and keys.
        CREATE TABLE returns (
          return_id INTEGER PRIMARY KEY,
          order_id INTEGER REFERENCES orders (order_id),
          reason VARCHAR(100),
          returned_on DATE
        );
        CREATE TABLE and data types
        ALTER TABLEChanges a table's structure: add, rename or drop a column.
        ALTER TABLE customers
        ADD COLUMN phone VARCHAR(20);
        ALTER TABLE
        CREATE VIEWSaves a query under a name you can select from like a table.
        CREATE VIEW open_orders AS
        SELECT order_id, customer_id, order_date
        FROM orders
        WHERE shipped_date IS NULL;
        Views
        CREATE INDEXBuilds an index so filters and joins on that column can find rows faster.
        CREATE INDEX idx_orders_customer
        ON orders (customer_id);
        Indexes
        TRUNCATERemoves every row at once and keeps the empty table.
        TRUNCATE TABLE signups_raw;
        DELETE: remove rows
        DROP TABLEDeletes a table and all its rows. IF EXISTS avoids an error when it is not there.
        DROP TABLE IF EXISTS returns;
        CREATE TABLE and data types

        Transactions (TCL)

        NameWhat it doesExamplePractice
        BEGIN, COMMITGroups changes into a transaction: COMMIT keeps them all, together.
        BEGIN;
        UPDATE products SET units_in_stock = units_in_stock - 2 WHERE product_id = 3;
        COMMIT;
        Transactions: BEGIN, COMMIT, ROLLBACK
        ROLLBACKUndoes every change since BEGIN.
        BEGIN;
        DELETE FROM signups_raw;
        ROLLBACK;
        Transactions: BEGIN, COMMIT, ROLLBACK

        Permissions (DCL)

        NameWhat it doesExamplePractice
        GRANTGives a user or role a permission on a table (PostgreSQL syntax).
        GRANT SELECT ON orders TO analyst;
        PostgreSQL syntax
        REVOKETakes a permission back.
        REVOKE SELECT ON orders FROM analyst;
        PostgreSQL syntax

        Checked against

        Every row was last checked on Nov 20, 2026.

        Questions

        What is the difference between DDL and DML?

        DDL (data definition language) commands change the structure: CREATE, ALTER, DROP and TRUNCATE. DML (data manipulation language) commands change the rows: INSERT, UPDATE and DELETE. SELECT is often grouped on its own as DQL.

        Which database do the examples run on?

        They run on Kestrel, a fictional online store you can download for free as a SQL script, CSV files or an Excel workbook. The syntax is standard SQL and PostgreSQL runs every example as written. SQL Server writes TOP instead of LIMIT, and SQLite has no TRUNCATE or GRANT.

        More cheat sheets

        Free resources

        Your Work Gym account

        Save your progress and pick up on another device. Your browser's progress comes with you.

        New accounts get 12 hours of full access: every course, unit, project and exam. The clock starts when you open your first lesson.

        Plan your practice from a job ad or syllabus

        Have a referral code?

        or

        You can keep practicing without an account.

        Change your plan

        Checking the price

        Help and feedback

        Ask a question, tell us what is not working, or share an idea. We reply by email.

        What is it about?

        We use it only to reply to you.