SQL Syntax and Statements - Introduction to SQL Vocabulary
SQL Syntax
SQL syntax defines the structure and format of commands you write to interact with a database. Think of it as grammar rules for the SQL language — it tells you how to write statements so the database understands what you want.
Basic SQL Syntax Rules
For reference, consider the following SQL statements.
SELECT column1, column2
FROM table_name
WHERE condition;
SELECT * FROM students;
- Keywords like
SELECT
,FROM
,WHERE
, etc., are not case-sensitive, but writing them in uppercase improves readability. - Statements end with a semicolon
;
— this marks the end of a complete command. In the above code, we have two commands. - Strings must be in single quotes: for example,
'Delhi'
. - Column names and table names are written in lowercase or snake_case by convention, but they can be any valid identifier.
- Spaces between keywords and elements (like table names, column names, etc.) are required for clarity and correctness. Don't write
SELECT*FROM
— instead useSELECT * FROM
.
Spacing and Formatting
- Always use a space between keywords:
SELECT
andFROM
must be separated. - Use commas between column names:
SELECT name, age, city
- You can write SQL on one line or across multiple lines — indentation is optional but helpful for readability.
SELECT name, age
FROM students
WHERE age>12;
SELECT name, age FROM students WHERE age>12;
Symbols in SQL Syntax
Symbol | Meaning | Example |
---|---|---|
* |
Select all columns | SELECT * FROM students; |
, |
Separate columns in a list | SELECT name, age |
' ' |
Wrap string values | WHERE city = 'Delhi' |
= |
Equality condition | WHERE age = 15 |
; |
End of SQL statement | SELECT * FROM students; |
Identifiers: Table and Column Names
- Table and column names must begin with a letter (A–Z).
- They can include letters, numbers, and underscores
_
. - Avoid using SQL keywords like
SELECT
orWHERE
as table or column names. - If a name contains spaces or special characters, wrap it in double quotes (e.g.,
"student name"
).
Good Practices
- Use consistent indentation and line breaks for better readability.
- Use uppercase for SQL keywords and lowercase for table/column names.
- End every statement with a semicolon
;
to avoid syntax errors.
QUIZ
Question 1:Which of the following SQL statements correctly creates a table named 'students'?
Question 2:In SQL, every statement must end with a semicolon.
Question 3:Which of the following are valid SQL data types?
Question 4:What does the following SQL statement do?
DELETE FROM students WHERE class = '10A';
DELETE FROM students WHERE class = '10A';
Comments
Loading comments...