Structured Query Language (SQL) - DDL & DML Commands Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- SQL Classification: DDL (Data Definition Language: CREATE, ALTER, DROP - modifies schema structure) vs DML (Data Manipulation Language: INSERT, SELECT, UPDATE, DELETE - manipulates row instances).
- Table Constraints: PRIMARY KEY (unique + not null), NOT NULL (cannot be left empty), UNIQUE (all values distinct, allows nulls), DEFAULT (fallback value), CHECK (validates condition), FOREIGN KEY ... REFERENCES (referential integrity).
- DML Query Clauses: SELECT column_list FROM table WHERE condition ORDER BY col [ASC|DESC] LIMIT n.
- Pattern Matching & Operators: `LIKE '%abc'` (ends with abc), `LIKE '_a%'` (second char is 'a'), `BETWEEN low AND high` (inclusive), `IN (val1, val2)`, `IS NULL` / `IS NOT NULL` (checking missing data).
Chapter Formulas & Equations (10 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | CREATE TABLE DDL Syntax Template |
|
Defines new relational structure with column specifications and integrity constraints. |
| 2 | ALTER TABLE Schema Modifications |
|
Modifies table definition without destroying existing stored records. |
| 3 | DROP TABLE vs DELETE FROM Distinction |
|
DROP is DDL (cannot rollback); DELETE is DML (can rollback). |
| 4 | INSERT INTO Syntax Template |
|
Adds a new tuple to the relation; strings and dates must be enclosed in single quotes. |
| 5 | UPDATE Statement Syntax |
|
Modifies attribute values in existing rows; omitting WHERE updates ALL rows. |
| 6 | DELETE Statement Syntax |
|
Removes specific tuples meeting condition; omitting WHERE empties the table. |
| 7 | SQL Wildcard Symbols in LIKE Operator |
|
Used for pattern matching in string attributes. |
| 8 | BETWEEN Operator Boundary Inclusivity |
|
Both endpoints a and b are strictly INCLUSIVE in SQL. |
| 9 | Testing for NULL in SQL |
|
NULL represents an unknown value; arithmetic comparisons with = NULL return UNKNOWN (falsy). |
| 10 | DISTINCT Keyword Deduplication |
|
Eliminates duplicate output rows in query results. |
Step-by-Step Worked Board Exam Examples
Example 1: Write SQL queries for the following requirements on table `STUDENT(RollNo, Name, Stream, Marks, City)`: (a) Display details of students whose Name starts with 'S'. (b) Display unique cities where students reside. (c) Increase marks by 5 for all students in 'Science' stream. (d) Display Name and Marks of students scoring between 80 and 95 (inclusive), ordered by Marks descending.
1. (a) Pattern matching starting with 'S': `SELECT * FROM STUDENT WHERE Name LIKE 'S%';`
2. (b) Unique cities: `SELECT DISTINCT City FROM STUDENT;`
3. (c) Update marks by 5 for Science students: `UPDATE STUDENT SET Marks = Marks + 5 WHERE Stream = 'Science';`
4. (d) Between operator and sorting: `SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 80 AND 95 ORDER BY Marks DESC;`
Example 2: Write SQL DDL commands to: (i) Add a new column `Email` of type `VARCHAR(50)` to table `TEACHER`. (ii) Remove the table `OLD_EXAM` completely from database.
1. Adding column: Use `ALTER TABLE ... ADD`: `ALTER TABLE TEACHER ADD Email VARCHAR(50);`
2. Deleting entire table: Use `DROP TABLE`: `DROP TABLE OLD_EXAM;`
Common Mistakes to Avoid in Exam
- Using `= NULL` instead of `IS NULL`: Writing `WHERE Grade = NULL` returns zero rows; always use `WHERE Grade IS NULL`.
- Confusing CHAR and VARCHAR: `CHAR(n)` is fixed length (pads trailing spaces to n bytes); `VARCHAR(n)` is variable length (uses only required characters + length byte).
- Using double quotes for strings: SQL standards prefer single quotes for string literals (`'Delhi'`), while double quotes in some dialects signify column identifiers.
- Omitting the WHERE clause in UPDATE or DELETE: Omitting `WHERE` updates or deletes EVERY SINGLE ROW in the table.
Target Practice Questions
Q1. Write a query to display all employees whose name has 'a' as the second letter.
Correct Answer: `SELECT * FROM EMPLOYEE WHERE Name LIKE '_a%';`
Q2. Write a command to delete records of all students who scored less than 33 marks.
Correct Answer: `DELETE FROM STUDENT WHERE Marks < 33;`
Q3. Which constraint prevents duplicate values in a column but still allows NULLs?
Correct Answer: The `UNIQUE` constraint.
Frequently Asked Questions
Q. What is the difference between DDL and DML commands?
DDL (Data Definition Language) commands (CREATE, ALTER, DROP) define, modify, and delete the database schema/structure itself. DML (Data Manipulation Language) commands (INSERT, SELECT, UPDATE, DELETE) manage and query the actual row records stored within the tables.
Q. What is the difference between CHAR(10) and VARCHAR(10)?
`CHAR(10)` is a fixed-length string data type; storing 'RAM' (3 chars) still consumes all 10 bytes padded with spaces. `VARCHAR(10)` is variable-length; storing 'RAM' consumes only 3 bytes plus storage overhead.
Q. How does `DROP TABLE` differ from `DELETE FROM`?
`DROP TABLE` removes the table structure, its schema definition, and all its data permanently from the dictionary. `DELETE FROM` removes records from the table while retaining the table structure for future insertions.
Q. What is the purpose of the PRIMARY KEY constraint?
It uniquely identifies each record in a database table. It automatically enforces two rules: 1) Uniqueness (no two rows can have identical primary key values), and 2) NOT NULL (primary key cannot be left blank).
Q. What does the `ORDER BY` clause do and what is its default sorting order?
`ORDER BY` sorts the retrieved query result set by one or more specified columns. The default sort order is ASCENDING (ASC); append `DESC` for descending order.