Basic SQL Syntax Rules
You'll learn the fundamental formatting conventions and structural rules that make SQL queries work correctly.
SQL (Structured Query Language) might seem intimidating at first, but it follows a few simple syntax rules that make it predictable and readable. Think of SQL syntax like grammar rules in spoken language—once you know them, constructing statements becomes intuitive.
Core SQL Syntax Rules
Statement Terminators
Every SQL statement should end with a semicolon (;). This tells the database management system where one command ends and another begins:
SELECT * FROM customers;
Case Insensitivity (Mostly)
SQL keywords like SELECT, FROM, and WHERE are case-insensitive. You can write them in uppercase, lowercase, or mixed case:
SELECT name FROM customers; -- Works fine
select name from customers; -- Also works
SeLeCt name FrOm customers; -- Still works (but don't do this!)
Convention: Most SQL developers write keywords in UPPERCASE to distinguish them from table/column names, but lowercase works equally well.
Whitespace Flexibility
SQL ignores extra spaces, tabs, and line breaks. These are all equivalent:
SELECT name FROM customers;
SELECT name
FROM customers;
SELECT name FROM customers;
Use whitespace to make your queries readable, especially for longer statements.
Names Are Case-Sensitive (Usually)
While keywords aren't case-sensitive, table names and column names often are, depending on your DBMS. It's safest to match the exact capitalization used when the table was created.
Key Takeaway: SQL statements end with semicolons, keywords are case-insensitive but conventionally uppercase, whitespace is flexible for formatting, and table/column names should match their defined case exactly.