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

    Summarizing and grouping · Lesson 5 of 20

    Aggregates: COUNT, SUM, AVG

    Turn a whole table into one number, and mind the NULLs.

    8 minutes3 games

    Sometimes the question isn't "give me rows" but "how many?" or "what's the average?". Aggregate functions take a whole column and return a single value:

    COUNT counts, SUM adds, AVG averages, MIN and MAX find the extremes.

    SQL
    SELECT COUNT(*) AS students,
           SUM(xp) AS total_xp,
           ROUND(AVG(xp), 1) AS avg_xp,
           MIN(xp) AS lowest,
           MAX(xp) AS highest
    FROM students;

    xp =

    320[0]
    150[1]
    90[2]
    410[3]
    NULL[4]
    275[5]
    60[6]
    180[7]

    AVG(xp) = 1485 ÷ 7, not ÷ 8: the NULL is left out of the calculation entirely.

    Important

    Every aggregate ignores NULL except COUNT(*), which counts rows themselves. So COUNT(*) = 8 but COUNT(xp) = 7. To treat NULL as zero: AVG(COALESCE(xp, 0)).

    What's the output?

    SELECT COUNT(*), COUNT(xp), COUNT(DISTINCT city)
    FROM students;

    What will this code print?

    Combine these with WHERE: SELECT COUNT(*) FROM students WHERE city = 'Cairo' counts only Cairo students, because filtering happens first.

    Complete the code

    Complete it to find the price of the most expensive course.

    SELECT (price) AS top_price FROM courses;

    Interview question

    What's the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column)?

    Exercise

    In courses: get the total hours as total_hours and the highest price as top_price.

    Expected result:
    total_hours | top_price
    146 | 600
    SQL
    SELECT
    FROM courses;

    Learn more from

    • PostgreSQL docs: Aggregate functionspostgresql.org
    • SQLite: Built-in aggregate functionssqlite.org
    • Modern SQL: NULL in SQLmodern-sql.com

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousORDER BY and LIMIT: sort and take the topNextGROUP BY and HAVING
    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.