SQL Practical File
SQL Practical File
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);
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 |
OUTPUT
| 6 | XI | COMMERCE | Male| Bhanu Prakash | 2006-07-24 | 55 |
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 |
+----+-------+---------+--------+-----------------+------------+-------+--------+