SQL Syntax

SQL · Introduction to SQL

Theory

A SQL statement is a sentence made of keywords (SELECT, FROM, WHERE), identifiers (table and column names), literals ('IT', 72000), operators (=, >, AND) and clauses. Each clause has one job, and clauses must be written in a fixed order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, then LIMIT/FETCH. A semicolon ends the statement, which matters when you run several at once.

The written order is not the order the engine thinks in. Logically it starts with FROM (which rows exist), then WHERE, GROUP BY, HAVING, SELECT, ORDER BY and finally LIMIT. This explains common surprises, such as why a column alias made in SELECT cannot be used in WHERE. Keywords are case-insensitive, text values go in single quotes, and comments start with -- or sit inside /* */.

Why we use it

Knowing the grammar and the logical order of clauses lets you read any query, place each clause correctly and predict errors before running the query, instead of guessing at syntax messages.

When to use it

Use this mental model every time you write or debug a query. When a query errors out or returns odd rows, first check clause order, quotes (single for text, double or brackets for names) and the logical execution order.

How it works

  1. FROM: pick the source table(s)
  2. WHERE: keep rows that pass the condition
  3. GROUP BY: form groups (if any)
  4. HAVING: keep groups that pass
  5. SELECT: compute output columns and aliases
  6. ORDER BY: sort the result
  7. LIMIT / FETCH: cut to N rows

Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

Example

SELECT name, salary FROM employees WHERE department = 'IT';

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic