SQL Topics
SQL Syntax
title: SQL Syntax
SQL syntax refers to the set of rules used to write SQL statements. Every SQL query follows a specific structure that allows the database management system (DBMS) to understand and execute commands correctly.
Understanding SQL syntax is the first step toward writing efficient and error-free SQL queries.
What is SQL Syntax?
SQL syntax is the grammar of SQL.
Just as English follows grammatical rules, SQL follows syntax rules.
For example:
SELECT * FROM students;This statement follows the correct SQL syntax and retrieves all records from the students table.
Basic SQL Statement Structure
Most SQL queries follow this structure:
SELECT column_name
FROM table_name
WHERE condition;Example
SELECT name
FROM students
WHERE age > 18;SQL Keywords
Keywords are reserved words that have special meanings in SQL.
Common SQL keywords include:
SELECT
FROM
WHERE
INSERT
UPDATE
DELETE
CREATE
ALTER
DROPExample
SELECT * FROM employees;Here:
SELECTis a keywordFROMis a keywordemployeesis a table name
SQL Statements End with a Semicolon
A semicolon (;) indicates the end of an SQL statement.
SELECT * FROM students;Although some database systems allow execution without a semicolon, using one is considered a best practice.
SQL is Case-Insensitive
SQL keywords can be written in uppercase or lowercase.
Uppercase
SELECT * FROM students;Lowercase
select * from students;Mixed Case
SeLeCt * FrOm students;All are valid.
However, using uppercase for keywords improves readability.
SQL Naming Conventions
Table Names
Good:
students
employee_details
customer_ordersBad:
Students Table
123employees
employee-detailsColumn Names
Good:
first_name
last_name
emailBad:
First Name
123email
email-addressSQL Syntax Rules
Rule 1: Use Valid Keywords
Correct:
SELECT * FROM students;Incorrect:
GET * FROM students;Rule 2: Reference Existing Tables
Correct:
SELECT * FROM students;Incorrect:
SELECT * FROM unknown_table;Rule 3: Use Quotes for Strings
Correct:
SELECT *
FROM students
WHERE city = 'Delhi';Incorrect:
SELECT *
FROM students
WHERE city = Delhi;Rule 4: Use Commas Between Columns
Correct:
SELECT id, name, email
FROM students;Incorrect:
SELECT id name email
FROM students;Example Database Table
Students Table
| id | name | age | city |
|---|---|---|---|
| 1 | Rahul | 20 | Delhi |
| 2 | Priya | 22 | Mumbai |
| 3 | Amit | 19 | Patna |
SELECT Statement Syntax
Retrieve all records:
SELECT * FROM students;Output:
| id | name | age | city |
|---|---|---|---|
| 1 | Rahul | 20 | Delhi |
| 2 | Priya | 22 | Mumbai |
| 3 | Amit | 19 | Patna |
SELECT Specific Columns
SELECT name, city
FROM students;Output:
| name | city |
|---|---|
| Rahul | Delhi |
| Priya | Mumbai |
| Amit | Patna |
WHERE Clause Syntax
Filter records based on a condition.
SELECT *
FROM students
WHERE age > 20;Output:
| id | name | age | city |
|---|---|---|---|
| 2 | Priya | 22 | Mumbai |
INSERT Statement Syntax
Add new data into a table.
INSERT INTO students (id, name, age, city)
VALUES (4, 'Rohit', 21, 'Kolkata');UPDATE Statement Syntax
Modify existing records.
UPDATE students
SET city = 'Bangalore'
WHERE id = 1;DELETE Statement Syntax
Remove records.
DELETE FROM students
WHERE id = 4;CREATE TABLE Syntax
Create a new table.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
city VARCHAR(50)
);SQL Formatting Best Practices
Recommended Style
SELECT name, city
FROM students
WHERE age > 18
ORDER BY name;Benefits:
- Easy to read
- Easy to debug
- Professional coding style
Common SQL Syntax Errors
Missing Semicolon
Incorrect:
SELECT * FROM studentsCorrect:
SELECT * FROM students;Misspelled Keyword
Incorrect:
SELEKT * FROM students;Correct:
SELECT * FROM students;Missing Quotes
Incorrect:
SELECT *
FROM students
WHERE city = Delhi;Correct:
SELECT *
FROM students
WHERE city = 'Delhi';SQL Syntax Cheat Sheet
Retrieve Data
SELECT * FROM table_name;Filter Data
SELECT * FROM table_name
WHERE condition;Insert Data
INSERT INTO table_name
VALUES (...);Update Data
UPDATE table_name
SET column = value
WHERE condition;Delete Data
DELETE FROM table_name
WHERE condition;Best Practices
- Use uppercase for SQL keywords.
- Use meaningful table names.
- End statements with semicolons.
- Use proper indentation.
- Avoid reserved keywords as identifiers.
- Keep queries simple and readable.
Summary
- SQL syntax is the set of rules used to write SQL statements.
- SQL keywords are reserved words such as
SELECT,FROM, andWHERE. - SQL is generally case-insensitive.
- Statements typically end with a semicolon.
- Proper formatting improves readability and maintainability.
Practice Questions
Beginner
- Write a query to retrieve all students.
- Select only the
namecolumn from the students table. - Filter students whose age is greater than 18.
Intermediate
- Insert a new student record.
- Update a student's city.
- Delete a student record.
Advanced
- Create a table named
employees. - Write a query using
WHEREandORDER BY. - Identify syntax errors in a given SQL query.
Interview Questions
What is SQL syntax?
SQL syntax is the set of rules used to write valid SQL statements.
Is SQL case-sensitive?
Most SQL databases treat keywords as case-insensitive.
Why is a semicolon used in SQL?
It marks the end of an SQL statement.
What happens if SQL syntax is incorrect?
The database returns a syntax error and the query is not executed.
Name some common SQL keywords.
- SELECT
- FROM
- WHERE
- INSERT
- UPDATE
- DELETE
- CREATE
Next Topic
➡️ SQL Comments
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for SQL Syntax.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, basics, syntax
Related SQL Topics