Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    SQLSQL

    First questions to the data

    • What are SQL and databases?
    • SELECT: choosing columns
    • WHERE: filtering rows
    • ORDER BY and LIMIT: sort and take the top

    Summarizing and grouping

    • Aggregates: COUNT, SUM, AVG
    • GROUP BY and HAVING
    • CASE, COALESCE and text functions

    Joining tables

    • Keys and relationships
    • INNER JOIN
    • LEFT JOIN: leave nobody out
    • Three-table joins and self joins
    • Subqueries and CTEs

    Building tables and changing data

    • CREATE TABLE and constraints
    • INSERT, UPDATE and DELETE
    • Transactions: all or nothing
    • SQL injection: the most dangerous mistake

    Professional level

    • Indexes: why is my query slow?
    • Window functions: rankings and totals without losing rows
    • Designing tables: normalization
    • Project: analytics for a learning platform
    SQL

    First questions to the data · Lesson 4 of 20

    ORDER BY and LIMIT: sort and take the top

    Sorting by several columns, where NULLs go, and paging with OFFSET.

    9 minutes2 games

    Without ORDER BY the database promises no order, even if results look sorted today. If order matters, ask for it. ASC is ascending (the default), DESC is descending.

    SQL
    SELECT name, city, xp
    FROM students
    ORDER BY city, xp DESC;

    Sorted by city first, then by XP from highest to lowest inside each city. Notice Youssef (NULL) lands last in Alexandria.

    Important

    Where NULL goes differs: SQLite and MySQL treat it as the smallest value (last under DESC), while PostgreSQL treats it as the largest (first under DESC). For the same result everywhere, write NULLS LAST or NULLS FIRST.

    LIMIT and OFFSET

    Step by step1 / 5
    1. 1SELECT name, xp
    2. ▶FROM students
    3. 3WHERE xp IS NOT NULL
    4. 4ORDER BY xp DESC
    5. 5LIMIT 3;

    Variables

    rows8

    Output

    …

    FROM first: take every row of the table.

    A query is written in one order but runs in another. Step through it.

    OFFSET skips some rows first, which is how pages work: page 2 (3 per page) is LIMIT 3 OFFSET 3.

    SQL
    -- Page 2 of the leaderboard, 3 students per page
    SELECT name, xp
    FROM students
    ORDER BY xp DESC
    LIMIT 3 OFFSET 3;

    A large OFFSET is slow: to reach page 1000 the database reads and throws away every row before it. On big tables use keyset pagination: WHERE xp < last_seen_xp ORDER BY xp DESC LIMIT 20.

    Order the lines

    Put the query's clauses in the order you write them.

    1. Click the lines in the right order

    Interview question

    How do you get the second highest XP? (the classic "second highest salary" question)

    Exercise

    Get the 3 cheapest paid courses (price above zero): title and price, cheapest first.

    Expected result:
    title | price
    SQL | 300
    Python | 400
    JavaScript | 450
    SQL
    SELECT title, price
    FROM courses;

    Learn more from

    • PostgreSQL docs: Sorting rowspostgresql.org
    • PostgreSQL docs: LIMIT and OFFSETpostgresql.org
    • Use The Index, Luke: Paging through resultsuse-the-index-luke.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousWHERE: filtering rowsNextAggregates: COUNT, SUM, AVG
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.