1. SQL, DDL and DML; data types and constraints
SQL is the standard, declarative language of relational databases such as MySQL.
- DDL (structure):
CREATE,ALTER,DROP. - DML (rows):
INSERT,UPDATE,DELETE,SELECT.
Data types: CHAR(n) fixed length (padded), VARCHAR(n) variable length, INT, FLOAT, DATE ('YYYY-MM-DD').
Constraints: NOT NULL (value required), UNIQUE (no repeats; several NULLs allowed), PRIMARY KEY (unique and not null; one per table).
| Category | Works on | Commands |
|---|---|---|
| DDL (Data Definition Language) | Structure of databases and tables | CREATE, ALTER, DROP |
| DML (Data Manipulation Language) | Rows of data in tables | INSERT, UPDATE, DELETE, SELECT |
| Data type | Stores | Example |
|---|---|---|
CHAR(n) | Fixed-length text of n characters (padded) | Class CHAR(3) → 12A |
VARCHAR(n) | Variable-length text, up to n characters | Name VARCHAR(20) → Aarav |
INT | Whole numbers | Marks INT → 88 |
FLOAT | Numbers with a decimal part | Rating FLOAT → 4.5 |
DATE | A date written YYYY-MM-DD | StartDate DATE → 2026-11-14 |
| Constraint | Rule it enforces | Typical column |
|---|---|---|
NOT NULL | A value must be entered | Name |
UNIQUE | No two rows may have the same value (several NULLs allowed) | Email |
PRIMARY KEY | Unique and not null; identifies each row; one per table | RollNo |
Key terms
- DDL
Commands that define or change the structure of databases and tables: CREATE, ALTER, DROP.
- DML
Commands that insert, change, delete or retrieve rows: INSERT, UPDATE, DELETE, SELECT.
- Constraint
A rule on a column that the database enforces, such as NOT NULL, UNIQUE or PRIMARY KEY.
2. Database and table commands
CREATE DATABASE vidyalaya;
SHOW DATABASES;
USE vidyalaya;
SHOW TABLES;
DESCRIBE STUDENT;
ALTER TABLE STUDENT ADD Phone CHAR(10);
ALTER TABLE STUDENT DROP Phone;
ALTER TABLE ROOM ADD PRIMARY KEY (RoomNo);
ALTER TABLE ROOM DROP PRIMARY KEY;
DROP TABLE ROOM;
DROP DATABASE hostel;
INSERT: INSERT INTO CLUB VALUES (5, 'Art', 'Ms Das', 250); or with a column list: INSERT INTO CLUB (ClubID, ClubName) VALUES (6, 'Yoga'); (unlisted columns become NULL).
| Task | Command |
|---|---|
| Add a column | ALTER TABLE STUDENT ADD Phone CHAR(10); |
| Remove a column | ALTER TABLE STUDENT DROP Phone; |
| Add a primary key | ALTER TABLE ROOM ADD PRIMARY KEY (RoomNo); |
| Remove the primary key | ALTER TABLE ROOM DROP PRIMARY KEY; |
| Delete a table | DROP TABLE ROOM; |
| Delete a database | DROP DATABASE hostel; |
| Column | Type | Constraint |
|---|---|---|
| RollNo | INT | PRIMARY KEY |
| Name | VARCHAR(20) | NOT NULL |
| Class | CHAR(3) | |
| Stream | VARCHAR(12) | |
| Marks | INT | |
| City | VARCHAR(15) | |
| ClubID | INT |
Key terms
- DESCRIBE
Shows the columns, data types and keys of a table.
- ALTER TABLE
Changes the structure of a table: adds or removes columns or keys.
3. SELECT with WHERE, IN, BETWEEN and LIKE
General form: SELECT columns FROM table WHERE condition ORDER BY column;
*selects all columns;DISTINCTremoves repeated rows;ASgives an alias; arithmetic (Price * Stock) is allowed.- Relational:
=,<>,>,<,>=,<=; logical:AND,OR,NOT. IN ('Pune', 'Nashik')matches any value in the list;BETWEEN 70 AND 90includes both ends.LIKE:%any run of characters,_exactly one character.
SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 70 AND 90 AND City IN ('Pune', 'Nagpur') ORDER BY Name;
Name Marks
Aarav 88
Bhavna 74
Farah 79
Hina 83
| Name | Stream | Marks |
|---|---|---|
| Diya | Humanities | 91 |
| Hina | Commerce | 83 |
| PName |
|---|
| Geometry Box |
| Lunch Box |
| Name | Revised |
|---|---|
| Diya | 96 |
| Gaurav | 100 |
Key terms
- Alias
A temporary name for a column or table in a query, given with AS.
- Wildcard
A LIKE symbol: % for any number of characters, _ for exactly one.
4. NULL and ORDER BY
NULL means missing or unknown. Any comparison with NULL using = or <> is never true, so test with IS NULL or IS NOT NULL.
ORDER BY sorts ascending by default; DESC sorts descending. With two columns, the second sorts within ties of the first. In ascending order MySQL puts NULL first.
SELECT PName, Stock FROM PRODUCT ORDER BY Stock DESC;
PName Stock
Gel Pen 300
Notebook 120
Sketch Pens 80
Water Bottle 45
Lunch Box 30
Extension Board 20
Desk Lamp 12
Geometry Box NULL
In descending order the NULL stock comes last.
| Name | Stream |
|---|---|
| Chirag | Science |
| Farah | Humanities |
| Name | Class | Marks |
|---|---|---|
| Gaurav | 12A | 95 |
| Aarav | 12A | 88 |
| Chirag | 12A | 65 |
| Hina | 12B | 83 |
| Bhavna | 12B | 74 |
| Eshan | 12B | 58 |
| Diya | 12C | 91 |
| Farah | 12C | 79 |
Key terms
- NULL
A missing or unknown value; not zero and not an empty string.
5. Aggregate functions, GROUP BY and HAVING
COUNT, SUM, AVG, MIN, MAX give one value from many rows and skip NULL (only COUNT(*) counts every row). MySQL shows averages with decimal places (for example 85.0000).
GROUP BY col gives one result row per group; HAVING filters groups and may use aggregates; WHERE filters rows before grouping and may not.
Written order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
SELECT City, COUNT(*), MAX(Marks) FROM STUDENT WHERE Stream <> 'Commerce' GROUP BY City HAVING COUNT(*) >= 2 ORDER BY City;
City COUNT(*) MAX(Marks)
Nashik 2 95
Pune 2 88
| Stream | COUNT(*) | AVG(Marks) |
|---|---|---|
| Commerce | 3 | 71.6667 |
| Humanities | 2 | 85 |
| Science | 3 | 82.6667 |
| COUNT(*) | MIN(Marks) | MAX(Marks) | SUM(Marks) | AVG(Marks) |
|---|---|---|---|---|
| 8 | 58 | 95 | 633 | 79.125 |
| COUNT(*) | COUNT(ClubID) | COUNT(DISTINCT ClubID) |
|---|---|---|
| 8 | 6 | 3 |
| WHERE | HAVING | |
|---|---|---|
| Works on | Single rows | Groups |
| When | Before GROUP BY | After GROUP BY |
| Aggregate functions allowed? | No | Yes |
| Example | WHERE Marks > 60 | HAVING COUNT(*) >= 2 |
Key terms
- Aggregate function
A function that returns one value from a set of rows: COUNT, SUM, AVG, MIN, MAX.
- HAVING
The clause that keeps only the groups satisfying a condition, written after GROUP BY.
6. Joins, UPDATE and DELETE
Cartesian product: SELECT * FROM A, B; pairs every row of A with every row of B. Degree = degree(A) + degree(B); cardinality = cardinality(A) × cardinality(B).
Equi-join: WHERE A.col = B.col; the common column appears twice with SELECT *.
Natural join: A NATURAL JOIN B matches the column with the same name and shows it once.
SELECT S.Name, C.ClubName, C.Fee FROM STUDENT S, CLUB C WHERE S.ClubID = C.ClubID AND S.Marks > 80 ORDER BY C.Fee DESC, S.Name;
Name ClubName Fee
Gaurav Robotics 500
Aarav Music 300
Diya Debate 200
Hina Debate 200
UPDATE table SET col = value WHERE …; changes rows; DELETE FROM table WHERE …; removes rows (all rows without WHERE; the table remains).
| STUDENT | CLUB | Cartesian product |
|---|---|---|
| 8 rows × 7 columns | 4 rows × 4 columns | 32 rows × 11 columns |
| PName | SupName | City |
|---|---|---|
| Gel Pen | Swastik Supplies | Indore |
| Geometry Box | Kaveri Traders | Chennai |
| Notebook | Kaveri Traders | Chennai |
| ClubName | Fee before | Fee after |
|---|---|---|
| Robotics | 500 | 600 |
| Music | 300 | 400 |
| Debate | 200 | 200 |
| Chess | 150 | 150 |
| Command | Removes | Table remains? | Category |
|---|---|---|---|
DELETE FROM T WHERE …; | Matching rows | Yes | DML |
DELETE FROM T; | All rows | Yes (empty) | DML |
DROP TABLE T; | Table, structure and data | No | DDL |
Key terms
- Equi-join
A join that keeps pairs of rows with equal values in the joining columns.
- Natural join
A join on the column with the same name in both tables, which appears once in the result.
- Cardinality
The number of rows of a table or result; degree is the number of columns.
Exam tips
Common mistakes
= NULLinstead ofIS NULL.- An aggregate in WHERE instead of HAVING.
- Forgetting the join condition (giving a Cartesian product), or not prefixing a column both tables share.
- Treating BETWEEN as excluding its ends.
- Mixing up DELETE (rows, DML) and DROP (table, DDL).
How marks are usually awarded
- 1 mark: the correct option or keyword.
- 2–3 marks: one mark per correct query (or per query with its output).
- 5 marks: five correct queries or statements, each with the right clauses; outputs in the right order with column headings.
About the data here: all tables are imagined. Results were produced by running each query; MySQL may show averages with trailing zeros (85.0000).