SQL syntax is pretty straightforward. Unlike programming languages that use curly braces and semicolons everywhere, SQL reads more like English sentences. But there are a few rules you need to know.
SQL Statements
Every SQL command is called a statement. A statement starts with a keyword
like SELECT, INSERT, UPDATE, or CREATE,
and it ends with a semicolon.
SELECT name, age FROM students;
INSERT INTO students VALUES (6, 'Adam', 25, 'Kaduna', 90);
UPDATE students SET score = 96 WHERE name = 'Musa';
See how each one ends with a semicolon? That is how SQL knows where one command ends and the next begins. Forgetting the semicolon is probably the most common mistake beginners make. I still do it sometimes, honestly.
Case Sensitivity
Here is something that trips up a lot of people โ SQL keywords are not case-sensitive.
SELECT, select, and even SeLeCt all work the same way.
select * from students;
SELECT * FROM STUDENTS;
Select * From Students;
All three of those will run and give you the same result. But here is my advice โ stick to writing SQL keywords in UPPERCASE. It is a convention that makes your queries easier to read, especially when they get long and complicated.
Whitespace Does Not Matter
SQL does not care about spaces or new lines. You can write a query all on one line or spread it across multiple lines:
SELECT name, age FROM students WHERE age > 21;
-- This works the same:
SELECT name, age
FROM students
WHERE age > 21;
I prefer spreading queries across multiple lines once they get longer than a few words. It just makes everything cleaner.
Comments
You can add comments to your SQL code using two dashes --:
-- This is a comment
SELECT * FROM students; -- this is also a comment
Comments are ignored when the query runs. Use them to explain what your query does, especially if it is something complex. Future you will thank present you.