SQL PROGRAMS
1. To create a database of School and create student table with the student id, class, section, gender, name, dob, and
marks as attributes where the student id is the primary key.
Solution:
create database school;
use school;
create table student(id int primary key, class char(5),section char(5), gender enum("Male","Female"), st_name char(25),
dob date, marks int);
desc student;
OUTPUT:
| Field | Type | Null | Key | Default | Extra |
| id | int(11) | NO | PRI | NULL | |
| class | char(25) | YES | | NULL | |
| section | char(25) | YES | | NULL | |
| gender | enum('Male','Female') | YES | | NULL | |
| st_name | char(25) | YES | | NULL | |
| dob | date | YES | | NULL | |
| marks | int(11) | YES | | NULL | |
2. To insert the details of at least 10 students in the above table and display the entire content of table.
Solution:
insert into student values(1,"VIII","A","Male","Pawan Gupta","2009-02-02",85), (2, "IX", "B", "Female","Parul
Rathore","2007-05-14",99), (3, "X", "C", "Male", "ABHISHEK SHARMA", "2007-07-27", 87), (4, "X", "D","Female",
"Kashish Sharma", "2007-09-30", 55), (5, "XI", "E","Male","Piyush kumar", "2007-11-26", 63), (6, "XI", "F","Male","Bhanu
Prakash", "2006-07-24", 67), (7, "XI", "G","Female","Raksha Mathur", "2005-11-12", 56), (8, "XI", "H","Female","Pooja
Kashyap", "2007-06-14", 43), (9, "XI", "I","Male","Madan kumar", "2007-09-17", 89), (10, "XI", "J","Female","Gauri
Sharma", "2006-11-15", 75);
select * from student;
OUTPUT:
| id | class | section | gender | st_name | dob | marks |
| 1 | VIII | A | Male | Pawan Gupta | 2009-02-02 | 85 |
| 2 | IX | B | Female | Parul Ratore | 2007-05-14 | 99 |
| 3|X |C | Male | ABHISHEK SHARMA | 2007-07-27 | 87 |
| 4|X |D | Female | Kashish Sharma | 2007-09-30 | 55 |
| 5 | XI | COMMERCE | Male| Piyush kumar | 2007-11-31 | 63 |
| 6 | XI | COMMERCE | Male| Bhanu Prakash | 2006-07-24 | 67 |
| 7 | XI | HUMANITIES | Female| Raksha Mathur | 2005-11-12 | 56 |
| 8 | XI | SCIENCE | Female| Pooja Kashyap | 2007-06-14 | 43 |
| 9 | XI | COMMERCE | Male| Madan kumar | 2007-09-17 | 89 |
| 10 | XI | HUMANITIES |Female| Gauri Sharma | 2006-11-15 | 75 |
3. To display Id, Name and Marks of those students who are scoring marks more than 75.
Solution:
select id, st_name, marks from student where marks > 75;
OUTPUT:
| id | st_name | marks |
| 1 | Pawan Gupta | 85 |
| 2 | Parul Rathore | 99 |
| 3 | ABHISHEK SHARMA | 87 |
| 9 | Madan kumar | 89 |
4. To display the information all the students, whose name starts with ‘P’
Solution:
select * from student where st_name like 'P%';
OUTPUT:
| id | class | section | gender | st_name | dob | marks |
| 1 | VIII | A | Male | Pawan Gupta | 2009-02-02 | 85 |
| 2 | IX | B | Female | Parul Ratore | 2007-05-14 | 99 |
| 5 | XI | COMMERCE | Male| Piyush kumar | 2007-11-31 | 63 |
| 8 | XI | SCIENCE | Female| Pooja Kashyap | 2007-06-14 | 43 |
5. To display Id, Name, DOB of those students who are born between ‘1980- 01-01’ and ‘2007-12-31’.
Solution:
SELECT ID,ST_NAME,DOB FROM STUDENT WHERE DOB BETWEEN '1980-01-01' AND '2007-12-31';
OUTPUT:
| id | st_name | dob |
| 1 | Pawan Gupta | 2009-02-02 |
| 2 | Parul Ratore | 2007-05-14 |
| 3 | ABHISHEK SHARMA | 2007-07-27 |
| 4 | Kashish Sharma | 2007-09-30 |
| 5 | Piyush kumar | 2007-11-31 |
| 6 | Bhanu Prakash | 2006-07-24 |
| 7 | Raksha Mathur | 2008-11-12 |
| 9 | Madan kumar | 2007-09-17 |
| 10 | Gauri Sharma | 2006-11-15 |
6. To display Id, Name, DOB, Marks, of students in ascending order of their names.
Solution:
select id, st_name, dob, marks from student order by st_name asc;
OUTPUT:
| id | st_name | dob | marks |
| 3 | ABHISHEK SHARMA | 2007-07-27 | 87 |
| 6 | Bhanu Prakash | 2006-07-24 | 67 |
| 10 |Gauri Sharma | 2006-11-15 | 75 |
| 4 | Kashish Sharma | 2007-09-30 | 55 |
| 9 | Madan kumar | 2007-09-17 | 89 |
| 2 | Parul Ratore | 2007-05-14 | 99 |
| 1 | Pawan Gupta | 2009-02-02 | 85 |
| 5 | Piyush kumar | 2007-11-31 | 63 |
| 8 | Pooja Kashyap | 2007-06-14 | 43 |
| 7 | Raksha Mathur | 2008-11-12 | 56 |
7. To display Id, Name, DOB, Marks, in descending order of their marks.
Solution:
select id, st_name, dob, marks from student order by marks desc;
OUTPUT:
id | st_name | dob | marks |
+----+-----------------+------------+-------+
| 2 | Parul Rathore | 2007-05-14 | 99 |
| 9 | Madan kumar | 2007-09-17 | 89 |
| 3 | ABHISHEK SHARMA | 2007-07-27 | 87 |
| 1 | Pawan Gupta | 2009-02-02 | 85 |
| 10 | Gauri Sharma | 2006-11-15 | 75 |
| 6 | Bhanu Prakash | 2006-07-24 | 67 |
| 5 | Piyush kumar | 2007-11-26 | 63 |
| 7 | Raksha Mathur | 2008-11-12 | 56 |
| 4 | Kashish Sharma | 2007-09-30 | 55 |
| 8 | Pooja Kashyap | 2007-06-14 | 43 |
+----+-----------------+------------+-------+
8. To delete detail of Abhishek Sharma student.
Solution:
Delete from student where st_name = “ABHISHEK SHARMA”;
OUTPUT:
| id | class | section | gender | st_name | dob | marks |
| 1 | VIII | A | Male | Pawan Gupta | 2009-02-02 | 85 |
| 2 | IX | B | Female | Parul Ratore | 2007-05-14 | 99 |
| 4|X |D | Female | Kashish Sharma | 2007-09-30 | 55 |
| 5 | XI | COMMERCE | Male| Piyush kumar | 2007-11-31 | 63 |
| 6 | XI | COMMERCE | Male| Bhanu Prakash | 2006-07-24 | 67 |
| 7 | XI | HUMANITIES | Female| Raksha Mathur | 2008-11-12 | 56 |
| 8 | XI | SCIENCE | Female| Pooja Kashyap | 2007-06-14 | 43 |
| 9 | XI | COMMERCE | Male| Madan kumar | 2007-09-17 | 89 |
| 10 | XI | HUMANITIES |Female| Gauri Sharma | 2006-11-15 | 75 |
9. To change marks of those roll no. have 6
Solution:
Update student set marks = 55 where roll_no = 6;
OUTPUT
| 6 | XI | COMMERCE | Male| Bhanu Prakash | 2006-07-24 | 55 |
10. To add mob no. column in student table
Solution:
Alter table student add mob_no int;
Select * from student;
OUTPUT:
id | class | section | gender | st_name | dob | marks | mob_no |
+----+-------+---------+--------+-----------------+------------+-------+--------+
| 1 | VIII | A | Male | Pawan Gupta | 2009-02-02 | 85 | NULL |
| 2 | IX | B | Female | Parul Rathore | 2007-05-14 | 99 | NULL |
| 3|X |C | Male | ABHISHEK SHARMA | 2007-07-27 | 87 | NULL |
| 4|X |D | Female | Kashish Sharma | 2007-09-30 | 55 | NULL |
| 5 | XI | E | Male | Piyush kumar | 2007-11-26 | 63 | NULL |
| 6 | XI | F | Male | Bhanu Prakash | 2006-07-24 | 67 | NULL |
| 7 | XI | G | Female | Raksha Mathur | 2008-11-12 | 56 | NULL |
| 8 | XI | H | Female | Pooja Kashyap | 2007-06-14 | 43 | NULL |
| 9 | XI | I | Male | Madan kumar | 2007-09-17 | 89 | NULL |
| 10 | XI | J | Female | Gauri Sharma | 2006-11-15 | 75 | NULL |
+----+-------+---------+--------+-----------------+------------+-------+--------+
11. To rename mob no. column by contact no.
Solution:
Alter table student change column mob_no contact_no int;
Desc student;
OUTPUT:
------------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-----------------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| class | char(5) | YES | | NULL | |
| section | char(5) | YES | | NULL | |
| gender | enum('Male','Female') | YES | | NULL | |
| st_name | char(25) | YES | | NULL | |
| dob | date | YES | | NULL | |
| marks | int | YES | | NULL | |
| contact_no | int | YES | | NULL | |
+------------+-----------------------+------+-----+---------+-------+
12. To delete column of contact no
Solution:
Alter table student drop column contact_no;
Desc student;
OUTPUT:
Field | Type | Null | Key | Default | Extra |
+---------+-----------------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| class | char(5) | YES | | NULL | |
| section | char(5) | YES | | NULL | |
| gender | enum('Male','Female') | YES | | NULL | |
| st_name | char(25) | YES | | NULL | |
| dob | date | YES | | NULL | |
| marks | int | YES | | NULL | |
+---------+-----------------------+------+-----+---------+-------+