SQL Set 1: create a STUDENT table and use ALTER, UPDATE, ORDER BY, DELETE and GROUP BY
Expt. No.19
Page No.38
STUDENT table: DDL, DML, ORDER BY, GROUP BY
Aim
To create a STUDENT table, insert data and run SQL commands to alter the table, update data, sort it, delete rows and group it with aggregate functions.
Concepts used
Database CommandsTable CommandsALTER TABLEINSERT, UPDATE and DELETEORDER BYGROUP BYAggregate Functions
Queries
-- Create the database and the tableCREATE DATABASE school;USE school;CREATE TABLE student ( rollno INT PRIMARY KEY, name VARCHAR(20) NOT NULL, gender CHAR(1), section CHAR(1), marks INT);INSERT INTO student VALUES (1, 'Asha', 'F', 'A', 82), (2, 'Rohan', 'M', 'B', 67), (3, 'Kabir', 'M', 'A', 91), (4, 'Diya', 'F', 'B', 76), (5, 'Neel', 'M', 'A', 58), (6, 'Ira', 'F', 'B', 88);SELECT * FROM student;-- ALTER: add an attribute, modify a data type, drop an attributeALTER TABLE student ADD city VARCHAR(20);ALTER TABLE student MODIFY name VARCHAR(30);DESCRIBE student;ALTER TABLE student DROP city;-- UPDATE: add 5 marks for section BUPDATE student SET marks = marks + 5 WHERE section = 'B';-- ORDER BY: descending and ascendingSELECT * FROM student ORDER BY marks DESC;SELECT name, marks FROM student ORDER BY name;-- DELETE: remove students with marks below 60DELETE FROM student WHERE marks < 60;-- GROUP BY with MIN, MAX, SUM, COUNT and AVGSELECT section, MIN(marks), MAX(marks), SUM(marks), COUNT(*), AVG(marks)FROM student GROUP BY section;
A marked line continues the line above it. In your file, write it on the same line.
Output
mysql> CREATE DATABASE school;
Query OK, 1 row affected
mysql> USE school;
Database changed
mysql> CREATE TABLE student (
-> rollno INT PRIMARY KEY,
-> name VARCHAR(20) NOT NULL,
-> gender CHAR(1),
-> section CHAR(1),
-> marks INT
-> );
Query OK, 0 rows affected
mysql> INSERT INTO student VALUES
-> (1, 'Asha', 'F', 'A', 82),
-> (2, 'Rohan', 'M', 'B', 67),
-> (3, 'Kabir', 'M', 'A', 91),
-> (4, 'Diya', 'F', 'B', 76),
-> (5, 'Neel', 'M', 'A', 58),
-> (6, 'Ira', 'F', 'B', 88);
Query OK, 6 rows affected
mysql> SELECT * FROM student;
+--------+-------+--------+---------+-------+
| rollno | name | gender | section | marks |
+--------+-------+--------+---------+-------+
| 1 | Asha | F | A | 82 |
| 2 | Rohan | M | B | 67 |
| 3 | Kabir | M | A | 91 |
| 4 | Diya | F | B | 76 |
| 5 | Neel | M | A | 58 |
| 6 | Ira | F | B | 88 |
+--------+-------+--------+---------+-------+
6 rows in set
mysql> ALTER TABLE student ADD city VARCHAR(20);
Query OK, 0 rows affected
mysql> ALTER TABLE student MODIFY name VARCHAR(30);
Query OK, 0 rows affected
mysql> DESCRIBE student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| rollno | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| section | char(1) | YES | | NULL | |
| marks | int | YES | | NULL | |
| city | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set
mysql> ALTER TABLE student DROP city;
Query OK, 0 rows affected
mysql> UPDATE student SET marks = marks + 5 WHERE section = 'B';
Query OK, 3 rows affected
mysql> SELECT * FROM student ORDER BY marks DESC;
+--------+-------+--------+---------+-------+
| rollno | name | gender | section | marks |
+--------+-------+--------+---------+-------+
| 6 | Ira | F | B | 93 |
| 3 | Kabir | M | A | 91 |
| 1 | Asha | F | A | 82 |
| 4 | Diya | F | B | 81 |
| 2 | Rohan | M | B | 72 |
| 5 | Neel | M | A | 58 |
+--------+-------+--------+---------+-------+
6 rows in set
mysql> SELECT name, marks FROM student ORDER BY name;
+-------+-------+
| name | marks |
+-------+-------+
| Asha | 82 |
| Diya | 81 |
| Ira | 93 |
| Kabir | 91 |
| Neel | 58 |
| Rohan | 72 |
+-------+-------+
6 rows in set
mysql> DELETE FROM student WHERE marks < 60;
Query OK, 1 row affected
mysql> SELECT section, MIN(marks), MAX(marks), SUM(marks), COUNT(*), AVG(marks)
-> FROM student GROUP BY section;
+---------+------------+------------+------------+----------+------------+
| section | MIN(marks) | MAX(marks) | SUM(marks) | COUNT(*) | AVG(marks) |
+---------+------------+------------+------------+----------+------------+
| A | 82 | 91 | 173 | 2 | 86.5000 |
| B | 72 | 93 | 246 | 3 | 82.0000 |
+---------+------------+------------+------------+----------+------------+
2 rows in set
✓ Printed by running this query set, not typed by hand.
Result
The table was created with 6 rows. ALTER added
city, changed name to VARCHAR(30) (seen in DESCRIBE) and dropped city again. UPDATE raised section B's marks by 5 (3 rows), DELETE removed Neel (58), and GROUP BY gave one summary row for each section.Viva voce: what the examiner may ask
01What is the difference between DDL and DML?
DDL (CREATE, ALTER, DROP) defines the structure of tables; DML (INSERT, UPDATE, DELETE) changes the data in them.
02What is the difference between DELETE and DROP TABLE?
DELETE removes rows but keeps the table; DROP TABLE removes the table itself with its structure.
03Which order does ORDER BY use by default?
Ascending (ASC).
04Why can name not be shown with GROUP BY section here?
Each group has many names; only the grouped column and aggregate functions have one value per group.
05What does COUNT(*) count?
All rows in the group, including rows with NULL values.
Variations for the lab test
- Display the students of section A in descending order of marks.
- Find the number of girls and boys using GROUP BY gender.
- Increase the marks of all students by 2% (use marks * 1.02).
! Common mistake Writing UPDATE without a WHERE clause changes every row in the table.