Interactive SQL Lab • Watch Queries Run

SQL Demo: See Every Clause in Action

A SQL query is a list of steps applied to rows. In this lab you pick options, read the SQL they build, press Run and watch each step happen: WHERE checks rows, JOIN pairs them, GROUP BY puts them in buckets, ORDER BY sorts them and HAVING filters the buckets.

How to use this lab

Each section has its own controls. Run plays the animation, Reset puts the data back. Changing an option resets that section.

Animation speed
Step 1 • Table, Columns & Rows

Anatomy of a Table

Click the table name, any column header, any row number or any cell. The panel explains what you clicked and the SQL below shows where it comes from.

Table definition (DDL)

          
        
Step 2 • Linking Tables

Foreign Keys: Rows That Point at Other Rows

A foreign key is a column whose values must exist in another table’s primary key. orders.cust_id points at customers.cust_id, so every order must belong to a real customer. Click any row to follow its link, then try statements below. Will the database accept them?

When a customer is deleted
How the link is declared

          
Try a statement
customers (parent)
orders (child)
Step 3 • Filtering Rows

WHERE: Keep Only the Rows You Want

WHERE tests every row against a condition. Rows where the condition is TRUE are kept; rows where it is FALSE (or NULL) are thrown away. Pick a ready-made filter or build your own, then press Run.

Try a filter

          
employees · input (8 rows)
Result
Step 4 • Combining Tables

JOIN: Match Rows Across Two Tables

A JOIN pairs each row of one table with the rows of another table that share a value, here cust_id. The join type decides what happens to rows that find no partner.

Venn diagram of the selected join customers orders

          
customers c (left table)
orders o (right table)
Result

Why is order 104 here? It points at customer 5, who does not exist. A real schema would block this with a FOREIGN KEY (see the foreign key demo); it is left in so you can see what RIGHT and FULL joins do with an unmatched row.

Step 5 • Summarising Rows

GROUP BY: Put Rows into Buckets

GROUP BY collects rows that share a value into one bucket, then an aggregate function (COUNT, SUM, AVG, MIN, MAX) turns each bucket into a single result row.


          
employees · input (8 rows)
Buckets
Result
Step 6 • Sorting

ORDER BY: Sort the Result

A table has no built-in order. If the order matters, you must ask for it with ORDER BY. Add a second sort column to break ties between rows with the same first value.


          
employees
Step 7 • Filtering Groups

HAVING: Filter the Buckets

WHERE filters rows before grouping. HAVING filters groups after grouping, so it can test aggregates like COUNT(*) or AVG(salary). Turn on the WHERE option to see both work in one query.

Try a condition

          
employees · input (8 rows)
Groups
Result
Putting It Together

The Order SQL Really Runs In

You write SELECT first, but the database runs it fifth. Knowing the real order explains most beginner errors, such as why WHERE cannot use COUNT(*) but ORDER BY can use a column alias.


          

Want to run real SQL? The SQL Foundations course runs PostgreSQL in your browser.