






SQL Syntax and Statements
Introduction to SQL Vocabulary
What is SQL Syntax?
SQL, like any language, has rules. These rules—called syntax—govern how commands must be structured to be understood by a database. Think of it like grammar in English. Say it wrong, and the database won’t know what you mean.
Basic SQL Syntax Rules
- SQL keywords are not case-sensitive (but uppercase is preferred for readability).
- Statements end with a
;
semicolon. - Strings must be enclosed in single quotes:
'Delhi'
.
1. SELECT Statement
The most fundamental SQL statement. It’s used to fetch data from a table.
SELECT *
FROM students;
roll_no | name | class | age | city
--------+---------------+-------+-----+---------
1 | Aarav Kapoor | 10A | 15 | Delhi
2 | Meera Nair | 9B | 14 | Kochi
3 | Rishi Malhotra| 10A | 15 | Mumbai
2. INSERT Statement
Used to add new records into a table. Let's insert another student:
INSERT INTO students (roll_no, name, class, age, city)
VALUES (4, 'Pooja Reddy', '9C', 14, 'Hyderabad');
3. UPDATE Statement
Use it to modify existing records. Suppose Pooja moved to Bengaluru:
UPDATE students
SET city = 'Bengaluru'
WHERE roll_no = 4;
4. DELETE Statement
To remove records from a table:
DELETE
FROM students
WHERE roll_no = 2;
5. CREATE TABLE Statement
Let’s build a new table called teachers
:
CREATE TABLE teachers (
teacher_id INT PRIMARY KEY,
name VARCHAR(50),
subject VARCHAR(30),
experience INT
);
6. DROP TABLE Statement
This command deletes an entire table. Use with caution!
DROP TABLE teachers;
7. ALTER TABLE Statement
To change a table’s structure – like adding a new column:
ALTER TABLE students ADD percentage FLOAT;
8. WHERE Clause
Filters results based on a condition:
SELECT name, city FROM students WHERE class = '10A';
name | city
----------------+--------
Aarav Kapoor | Delhi
Rishi Malhotra | Mumbai
9. ORDER BY Clause
To sort data by age in descending order:
SELECT name, age FROM students ORDER BY age DESC;
10. DISTINCT Keyword
To find unique values:
SELECT DISTINCT class FROM students;
10A
9C
SQL Statement Flow – A Real-World Analogy
Imagine managing your school database as a daily task list:
- Start your day:
CREATE TABLE
- Add students:
INSERT
- Update details if someone shifts school:
UPDATE
- Remove alumni:
DELETE
- Check class list:
SELECT
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';