Lesson Beginner

SQL syntax

Good SQL is rarely about clever tricks.
Most of the time, it is about getting structure right: what to select, where it comes from, how it is filtered, and how it is ordered.

Think in Clauses, Not in One Long Sentence #

A query is built from clauses.
Each clause has a job, and clauses are easier to reason about when you read them as building blocks instead of as one giant statement.

Core Clauses You Will Use Every Day #

ClausePurpose
SELECTChoose columns to return
FROMChoose source table(s)
WHEREFilter rows before grouping
GROUP BYAggregate by key(s)
HAVINGFilter after aggregation
ORDER BYSort final output
LIMITRestrict row count
sql
SELECT column_list
FROM table_name
WHERE condition
GROUP BY grouping_columns
HAVING aggregate_condition
ORDER BY sort_columns
LIMIT n;

Why Clause Order Feels Weird at First #

SQL is written in one order and evaluated in another.
That mismatch is normal, and understanding it will prevent a lot of confusion:

FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT

This mental model helps explain many beginner errors, especially around aggregation and filtering.

Run and Tweak #

Run this query, then change one clause at a time to see how the output changes:

Loading SQL editor...

Knowledge check #

3 questions

0 / 3 answered
  1. Which of these is the standard clause order when writing a query?

  2. SQL keywords like SELECT and FROM are:

  3. How do you write a single-line comment in SQL?

Next Step #

Continue to SQL datatypes to understand how value types affect filtering, sorting, and calculations.