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.
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.
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?
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.
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.
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.
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.
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.
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.
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.