Chapter 2: SQL Syntax Basics
SQL Syntax Basics
Alright, class! Welcome to Chapter 2 — the place where we stop talking about SQL and actually start speaking it. Think of this as learning the grammar and pronunciation of a new language. If you get these basics right, everything else in SQL will feel much easier and natural.
Today we’re going to cover four super-important foundational things:
- The basic structure of SQL statements
- How to write comments (so you can explain your code to yourself and others)
- Case sensitivity (does uppercase/lowercase matter?)
- Semicolon usage (when and why you need that little ; at the end)
I’m going to explain everything like we’re sitting together in a Mumbai café, with me drawing on a napkin and showing you real examples. Let’s go step by step!
1. Basic Structure of SQL Statements
Every SQL statement follows a very predictable pattern. It’s almost like filling in a template. Most statements have these parts:
|
0 1 2 3 4 5 6 |
[COMMAND] [WHAT] [FROM/WHERE/etc.] [CONDITIONS] [ORDER BY / LIMIT / etc.]; |
Here are the most common types of SQL statements you’ll write:
- SELECT (to read data) Most common statement in the world!
SQL012345678910SELECT column1, column2, column3FROM table_nameWHERE conditionORDER BY column_nameLIMIT 10;
- INSERT (to add new data)
SQL01234567INSERT INTO table_name (column1, column2)VALUES ('value1', 'value2');
- UPDATE (to change existing data)
SQL012345678UPDATE table_nameSET column1 = 'new_value'WHERE condition;
- DELETE (to remove data)
SQL01234567DELETE FROM table_nameWHERE condition;
- CREATE (to make new tables, databases, etc.)
SQL012345678910CREATE TABLE employees (id INT PRIMARY KEY,name VARCHAR(100),salary DECIMAL(10,2));
Golden Rules for Structure:
- Keywords like SELECT, FROM, WHERE, INSERT, UPDATE, etc. are written in uppercase by convention (though not required).
- You can write the entire query on one line or spread it over multiple lines — the database doesn’t care.
- Indentation and line breaks are purely for human readability — highly recommended!
Real-life example (imagine we have a table called students):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
-- Bad (hard to read) SELECT name,age,city FROM students WHERE age>18 ORDER BY name; -- Good (very readable) SELECT name, age, city FROM students WHERE age > 18 ORDER BY name ASC; |
Always write it the “good” way — your future self (and teammates) will thank you!
2. Comments in SQL
Comments are notes in your code that the database completely ignores. They are only for humans — to explain what the query does, why you wrote it a certain way, or to temporarily disable parts of code.
There are two ways to write comments in SQL:
A. Single-line comment → use — (two dashes) Everything after — on that line is ignored.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
-- This query finds all active customers from Mumbai SELECT customer_id, customer_name, email FROM customers WHERE city = 'Mumbai' AND status = 'ACTIVE'; -- Only active customers |
B. Multi-line comment → use /* … */ Everything between /* and */ is ignored, even across many lines.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* This is a big report query written by Raj on 26-Jan-2026 Purpose: Monthly sales summary for management Last modified: 25-Jan-2026 */ SELECT product_category, SUM(sales_amount) AS total_sales, COUNT(*) AS number_of_orders FROM sales WHERE sale_date >= '2026-01-01' GROUP BY product_category; |
Pro Tip: Use comments generously! Especially when:
- The query is complex
- You’re doing something tricky
- You’re about to share the query with your team
3. Case Sensitivity
Here’s the golden rule that confuses almost everyone at first:
SQL keywords are NOT case-sensitive.
You can write SELECT, select, SeLeCt, sElEcT — the database treats them all the same.
BUT — the actual data (values inside tables) IS case-sensitive in most databases!
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
-- These are ALL the same to the database SELECT * FROM employees; select * from employees; SeLeCt * FrOm EmPlOyEeS; -- But these are DIFFERENT when searching data! SELECT * FROM employees WHERE city = 'Mumbai'; SELECT * FROM employees WHERE city = 'mumbai'; -- Will NOT match if data is stored as 'Mumbai' |
Important differences between popular databases (2026 status):
| Database | Keywords case-sensitive? | Table/Column names case-sensitive? | Data (string values) case-sensitive? |
|---|---|---|---|
| MySQL | No | Depends on OS (Linux = Yes, Windows = No) | Yes |
| PostgreSQL | No | Yes (by default) | Yes |
| SQL Server | No | No (unless you use quoted identifiers) | Yes (unless collation is case-insensitive) |
| Oracle | No | No (unless quoted) | Yes |
| SQLite | No | Yes (unless PRAGMA case_sensitive_like) | Yes |
Best Practice (what most professionals do):
- Write keywords in UPPERCASE (SELECT, FROM, WHERE, etc.)
- Write table and column names exactly as they were created (usually lowercase or snake_case)
- Be careful with string comparisons — use functions like LOWER() or UPPER() if you want case-insensitive search:
|
0 1 2 3 4 5 6 7 8 |
-- Case-insensitive search in any database SELECT * FROM employees WHERE LOWER(city) = 'mumbai'; |
4. Semicolon Usage (;)
The semicolon ; is like the full stop (period) at the end of an English sentence.
When do you need it?
| Situation | Do you need ; ? | Example |
|---|---|---|
| Most tools & clients (MySQL Workbench, pgAdmin, DBeaver, VS Code) | Yes — almost always | SELECT * FROM employees; |
| Inside stored procedures / functions / triggers | Yes between statements | SET @count = 0; SELECT @count; |
| Multiple statements in one script | Yes between each | INSERT …; UPDATE …; DELETE …; |
| Some web apps / ORMs (like Python’s SQLAlchemy) | Sometimes optional | Depends on the driver |
| Oracle SQL*Plus (older tool) | Yes (or use / instead) | |
| SQLite command-line tool | Optional (but recommended) |
Rule of Thumb for 2026: Always put a semicolon at the end of every SQL statement. It’s safe, portable, and prevents 90% of “why isn’t my query running?” errors.
Funny real-life story: A junior developer once spent 3 hours wondering why his query failed… only to realize he forgot the ; in a production script. The database silently waited for more input — classic!
Quick Recap with a Full Example
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
-- Author: Webliance -- Date: 26-Jan-2026 -- Purpose: Find top 5 highest-paid employees in Mumbai SELECT employee_id, -- Unique employee number first_name || ' ' || last_name AS full_name, -- Combine first + last name salary, department FROM employees WHERE city = 'Mumbai' AND salary > 80000 -- Only well-paid employees ORDER BY salary DESC -- Highest salary first LIMIT 5; -- Top 5 only |
See? Clean, commented, properly cased, and ends with ;.
That’s it for SQL Syntax Basics! You now know how to write clean, readable, and correct SQL.
