0% found this document useful (0 votes)
557 views15 pages

DB

The document describes creating and populating tables for a department and employee database. It includes steps to: 1. Create tables for departments and employees with necessary fields and constraints 2. Insert sample data into the tables, including at least 5 records per table 3. Describe the structure of the tables and list records

Uploaded by

Ankit Mourya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
557 views15 pages

DB

The document describes creating and populating tables for a department and employee database. It includes steps to: 1. Create tables for departments and employees with necessary fields and constraints 2. Insert sample data into the tables, including at least 5 records per table 3. Describe the structure of the tables and list records

Uploaded by

Ankit Mourya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SET-1

DEPARTMENT (dept_no, dept_name, location)


Fff 1 Create the Simple DEPARTMENT Table.
C create table department(
dept_no varchar(20),
dept_name varchar(20),
location varchar(20));
2 Display structure of department table.
describe department;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_no | varchar(20) | YES | | NULL | |
| dept_name | varchar(20) | YES | | NULL | |
| location | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 Insert below records into Department Table
insert into department values('10','Account','NY');
insert into department values('20','HR','NY');
insert into department values('30','Production','DL');
insert into department values('40','sales','NY');
insert into department values('50','EDP','MU');
insert into department values('60','TRG',NULL);
insert into department values('110','RND','AH');
4 Display all records of Department table
select * from department;
+---------+------------+----------+
| dept_no | dept_name | location |
+---------+------------+----------+
| 10 | Account | NY |
| 20 | HR | NY |
| 30 | Production | DL |
| 40 | sales | NY |
| 50 | EDP | MU |
| 60 | TRG | NULL |
| 110 | RND | AH |
+---------+------------+----------+
5 Display all department belonging to location 'NY'
select * from department where location="NY";
+---------+-----------+----------+
| dept_no | dept_name | location |
+---------+-----------+----------+
| 10 | Account | NY |
| 20 | HR | NY |
| 40 | sales | NY |
+---------+-----------+----------+
3 rows in set (0.00 sec)
6 Display details of Department 10
select * from department where dept_no="10";
+---------+-----------+----------+
| dept_no | dept_name | location |
+---------+-----------+----------+
| 10 | Account | NY |
+---------+-----------+----------+
1 row in set (0.00 sec)
7 List all department names starting with 'A'
select * from department where dept_name LIKE 'A%';
+---------+-----------+----------+
| dept_no | dept_name | location |
+---------+-----------+----------+
| 10 | Account | NY |
+---------+-----------+----------+
8 List all departments whose number is between 1 and 100
select * from department where dept_no <= 100;
+---------+------------+----------+
| dept_no | dept_name | location |
+---------+------------+----------+
| 10 | Account | NY |
| 20 | HR | NY |
| 30 | Production | DL |
| 40 | sales | NY |
| 50 | EDP | MU |
| 60 | TRG | NULL |
+---------+------------+----------+
9 Delete 'TRG' department
delete from department where dept_name ="TRG";
10 Change department name 'EDP' to 'IT
update department set dept_name ="IT" where dept_name ="EDP";
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
SET-2
EMPLOYEE (emp_id, emp_name, birth_date, gender, dept_no, address, designation, salary, experience,
email) DEPARTMENT (dept_no, dept_name, location)
11 Create the EMP Table with all necessary constraints such as In EMP TABLE: Employee id
should be primary key, Department no should be
Foreign key, employee age (birth_date) should be greater than 18 years, salary should be
greater than zero, email should have (@ and dot) sign in address, designation of employee
can be “manager”, “clerk”, “leader”, “analyst”, “designer”, “coder”, “tester”.
create table employe(
emp_id int primary key,
emp_name varchar2(20),
birth_date date,
gender varchar2(6),
DEPT_NO number(38) REFERENCES department(DEPT_NO),
address varchar2(100),
designation varchar2(15),
salary decimal(9,2),
experience varchar2(5),
email varchar2(255) CONSTRAINT chk_emp_email CHECK (REGEXP_LIKE (email,'^[A-Za-z0-
9._%+]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$')) );
1 Create DEPT table with neccessary constraint such as Department no should be primary
key, department name should be unique.
create table department(
dept_no varchar(20),
dept_name varchar(20) unique,
location varchar(20));
12 After creation of above tables, modify Employee table by adding the constraints as ‘Male’
or ‘Female’ in gender field and display the structure.
ALTER TABLE employe ADD CONSTRAINT gender_check CHECK(gender IN('male','female'));
13 Insert proper data (at least 5 appropriate records) in all the tables.
insert into employe values('8','karan','1998-02-11','m','90','antapur','emp','20000','3year','[email protected]');
insert into employe values('9','varsa','1998-02-13','f','90','surat','emp','30000','6year','[email protected]');
insert into employe values('10','raj','1996-06-14','m','70','surat','emp','30000','6year','[email protected]');
insert into employe values('11','ragini','1996-06-1','f','90','surat','emp','30000','6year','[email protected]');
insert into employe values('12','rahul','1996-05-1','m','100','surat','emp','45000','6year','[email protected]');

insert into department values('10','BCA','Valod');


insert into department values('20','MCA','Valod');
insert into department values('30','BCA','Bardoli');
insert into department values('40','MCA','Bardoli');
insert into department values('50','BSC','Bardoli');
14 Describe the structure of table created
describe department;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_no | varchar(20) | YES | | NULL | |
| dept_name | varchar(20) | YES | | NULL | |
| location | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+

describe employe;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| emp_id | varchar(20) | NO | PRI | NULL | |
| emp_name | varchar(20) | YES | | NULL | |
| birth_date | date | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| dept_no | varchar(20) | YES | | NULL | |
| adderess | varchar(50) | YES | | NULL | |
| designation | varchar(20) | YES | | NULL | |
| salary | varchar(20) | YES | | NULL | |
| experience | varchar(20) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
15 List all records of each table in ascending order.
select * from employe;
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+----------------------
| emp_id | emp_name | birth_date | gender | dept_no | adderess | designation | salary | experience | email |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+----------------------
|1 | arkit | 1999-07-09 | m | 10 | valod | manager | 40000 | 1 | [email protected] |
| 10 | raj | 1996-06-14 | m | 70 | surat | emp | 30000 | 6 | [email protected] |
| 11 | ragini | 1996-06-01 | f | 90 | surat | emp | 30000 | 6 | [email protected] |
| 12 | rahul | 1996-05-01 | m | 100 | surat | emp | 45000 | 6 | [email protected] |
| 13 | nimesh | 1999-05-07 | m | 110 | surat | emp | 40000 | 6 | [email protected] |
| 14 | yash | 1996-05-05 | m | 120 | surat | emp | 40000 | 6 | [email protected] |
| 15 | lalu | 1996-05-05 | m | 140 | surat | emp | 40000 | 6 | [email protected] |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+----------------------
16 Delete the department whose loction is Ahmedabad.
delete from department where location ="Ahmedabad";
17 Display female employee list.
Select * from employe where gender="f";
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+------------------+
| emp_id | emp_name | birth_date | gender | dept_no | adderess | designation | salary | experience | email
|
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+------------------+
| 11 | ragini | 1996-06-01 | f | 90 | surat | emp | 30000 | 6year | [email protected] |
|9 | varsa | 1998-02-13 | f | 90 | surat | emp | 30000 | 6year | [email protected] |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+------------------+
18 Display Departname wise employee Names
select d.dept_name, e.emp_name from employe e, department d where e.dept_no=d.dept_no order by d.dept_name;
+-----------+----------+
| dept_name | emp_name |
+-----------+----------+
| BBA | nimesh |
| BBA | yash |
| BCA | arkit |
| BCA | sajan |
| BCOM | raj |
| BSC | vishal |
| IT | bhavik |
| MBA | lalu |
| MCA | ankit |
| MCA | ashutosh |
19 Find the names of the employee who has salary less than 5000 and greater than 2000.
select * from employe where salary<5000 AND salary>2000;
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
| emp_id | emp_name | birth_date | gender | dept_no | adderess | designation | salary | experience | email
|
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
|6 | sajan | 1999-02-10 | m | 80 | vakaner | emp | 4000 | 2year | [email protected] |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
1 row in set (0.00 sec)
20 Display the names and the designation of all female employee in descending order.
select emp_name,designation from employe where gender="f" ORDER BY emp_name DESC;
+----------+-------------+
| emp_name | designation |
+----------+-------------+
| varsa | emp |
| ragini | emp |
+----------+-------------+
21 Display the names of all the employees who names starts with ‘A’ ends with ‘A’.
select * from employe where emp_name like '%a' and emp_name like 'a%';
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
| emp_id | emp_name | birth_date | gender | dept_no | adderess | designation | salary | experience | email
|
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
|8 | ana | 1998-02-11 | m | 90 | antapur | emp | 20000 | 3year | [email protected] |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
22 Find the name of employee and salary for those who had obtain minimum salary.
select emp_name, salary from employe where salary=(select min(salary)from employe);
+----------+--------+
| emp_name | salary |
+----------+--------+
| chirag | 22000 |
| ana | 22000 |
+----------+--------+
2 rows in set (0.03 sec)
23 Add 10% raise in salary of all employees whose department is ‘IT’.
update employe set salary=salary+(salary*0.10) where dept_no=(select dept_no from department where dept_name='IT');
24 Count total number of employees of ‘IT’ department.
select count(dept_no) from employe where dept_no=(select dept_no from department where dept_name='IT');
+----------------+
| count(dept_no) |
+----------------+
| 1|
+----------------+
25 List all employees who born in the current month.
select emp_name from employe where MONTH(birth_date) = MONTH(CURRENT_DATE);
26 Print the record of employee and dept table as “Employee works in department ‘MBA’.
select d.dept_name, e.emp_name from employe e, department d where e.dept_no=d.dept_no and
dept_name="MBA" ;
+-----------+----------+
| dept_name | emp_name |
+-----------+----------+
| MBA | lalu |
+-----------+----------+
27 List names of employees who are fresher’s (less than 1 year of experience).
select emp_name from employe where experience <1;
+----------+
| emp_name |
+----------+
| ankit |
| sajan |
+----------+
28 List department wise names of employees who has more than 5 years of experience.
select d.dept_name, e.emp_name from employe e, department d where e.dept_no=d.dept_no and experience >5 ORDER BY
d.dept_name ASC;
+-----------+----------+
| dept_name | emp_name |
+-----------+----------+
| BBA | nimesh |
| BBA | yash |
| BCOM | raj |
| BSC | vishal |
| MBA | lalu |
| MCA | ashutosh |
| MCA | ragini |
| MCA | varsa |
| Msc | rahul |
+-----------+----------+
29 Crete Sequence to generate department ID

30 List department having no employees


select d.dept_name from department d WHERE NOT EXISTS(select * from employe e where e.dept_no=d.dept_no);
+-----------+
| dept_name |
+-----------+
| BCA |
| MCA |
| BBA |
| MCOM |
+-----------+
SET-3
STUDENT (rollno, name, class, birthdate)
COURSE (courseno, coursename, max_marks, pass_marks)
SC (rollno, courseno, marks)
1 Create the above three tables along with key constraints.
create table STUDENT(
rollno varchar(20) Primary key,
name varchar(20),
class varchar(20) ,
birthdate date);

create table COURSE(


courseno varchar(20) Primary key,
coursename varchar(20),
max_marks varchar(20) ,
pass_marks varchar(20));

create table SC(


rollno varchar(20),
Foreign Key(rollno) REFERENCES STUDENT(rollno),
courseno varchar(20),
Foreign Key(courseno) REFERENCES COURSE(courseno),
marks varchar(20));

2 Write an Insert script for insertion of rows with substitution variables and insert appropriate data.
insert into STUDENT values('1','ANKIT','A','1995-07-05');
insert into STUDENT values('2','ARKIT','A','1999-07-09');
insert into STUDENT values('3','RAJU','B','1980-09-09');
insert into STUDENT values('4','YASH','B','1981-08-05');
insert into STUDENT values('5','LALU','B','1982-09-03');

insert into COURSE values('C1','BCA','70','25');


insert into COURSE values('C2','MCA','70','35');
insert into COURSE values('C3','BSC','70','25');
insert into COURSE values('C4','MSC','70','35');

INSERT INTO SC VALUES('1','C1','35');


INSERT INTO SC VALUES('2','C2','55');
INSERT INTO SC VALUES('3','C1','30');
INSERT INTO SC VALUES('4','C3','30');
INSERT INTO SC VALUES('5','C4','60');
INSERT INTO SC VALUES('6','C2','35');

3 Add a constraint that the marks entered should strictly be between 0 and 100.
alter table SC add constraint check (marks between 0 and 100);
Query OK, 10 rows affected (1.22 sec)
Records: 10 Duplicates: 0 Warnings: 0

4 While creating SC table, composite key constraint was forgotten. Add the composite keynow.
alert table student add Foreign Key(rollno) REFERENCES STUDENT(rollno)
5 Display details of student who takes ‘Database Management System’ course.
select a.name,b.*,c.* from student a , course b , sc c where b.coursename='DBMS' and c.courseno=b.courseno and
c.rollno=a.rollno;
+---------+----------+------------+-----------+------------+--------+----------+-------+
| name | courseno | coursename | max_marks | pass_marks | rollno | courseno | marks |
+---------+----------+------------+-----------+------------+--------+----------+-------+
| ANKIT | C1 | DBMS | 70 | 25 |1 | C1 | 35 |
| RAJU | C1 | DBMS | 70 | 25 |3 | C1 | 30 |
| MEHUL | C1 | DBMS | 70 | 25 |9 | C1 | 64 |
| JAYDEEP | C1 | DBMS | 70 | 25 | 10 | C1 | 54 |
6 Display the names of students who have scored more than 70% in Computer Networksand
have not failed in any subject.
select a.*,b.*,c.* from STUDENT a , course b,sc c where c.marks>70 and b.coursename='MSC'and c.rollno in(select
rollno from sc where rollno not in(select rollno from sc,course where marks<course.pass_marks))and a.rollno=c.rollno;
+--------+------+-------+------------+----------+------------+-----------+------------+--------+----------+-------+
| rollno | name | class | birthdate | courseno | coursename | max_marks | pass_marks | rollno | courseno | marks |
+--------+------+-------+------------+----------+------------+-----------+------------+--------+----------+-------+
|5 | LALU | B | 1982-09-03 | C4 | MSC | 100 | 35 |5 | C4 | 75 |
+--------+------+-------+------------+----------+------------+-----------+------------+--------+----------+-------+
1 row in set (0.03 sec)
7 Display the average marks obtained by each student.
select avg(marks) from student a,sc c where c.rollno=a.rollno group by c.rollno;
+------------+
| avg(marks) |
+------------+
| 35 |
| 55 |
| 30 |
| 30 |
| 55 |
+------------+
5 rows in set (0.06 sec)
8 Select all courses where passing marks are more than 30% of average maximum mark.
select * from COURSE where pass_marks >30;
+----------+------------+-----------+------------+
| courseno | coursename | max_marks | pass_marks |
+----------+------------+-----------+------------+
| C2 | MCA | 100 | 35 |
| C4 | MSC | 100 | 35 |
+----------+------------+-----------+------------+
2 rows in set (0.00 sec)
9 Display details of students who are born in 1980 or 1982.
SELECT * FROM STUDENT WHERE year(birthdate) = 1980 or year(birthdate) = 1982;
+--------+-------+-------+------------+
| rollno | name | class | birthdate |
+--------+-------+-------+------------+
|3 | RAJU | B | 1980-09-09 |
|5 | LALU | B | 1982-09-03 |
|6 | YATIN | A | 1982-06-08 |
+--------+-------+-------+------------+
3 rows in set (0.00 sec)
10 Create a view that displays student courseno and its corresponding marks.
SELECT * FROM SC;
+--------+----------+-------+
| rollno | courseno | marks |
+--------+----------+-------+
|1 | C1 | 35 |
|2 | C2 | 55 |
|3 | C1 | 30 |
|4 | C3 | 30 |
|5 | C4 | 60 |
+--------+----------+-------+
SET-4
Create the database COMPANY and create given tables with all necessary constraints such as primary
key, foreign key, unique key, not null and check constraints.
EMPLOYEE (emp_id, emp_name, birth_date, gender, dept_no, address, designation, salary, experience,
email)
DEPART (dept_no, dept_name, total_employees, location)
PROJECT (proj_id, type_of_project, status, start_date, emp_id)
1 Delete the department whose total number of employees less than 1.
delete from depart where total_employees <1;
Query OK, 0 rows affected (0.11 sec)
2 Display the names and the designation of all female employee in descending order
select emp_name,designation,gender from employee where gender='f'order by emp_name desc;
+----------+-------------+--------+
| emp_name | designation | gender |
+----------+-------------+--------+
| swati | designer | f |
| pari | testor |f |
| krupa | coder |f |
+----------+-------------+--------+
3 rows in set (0.00 sec)
3 Display the names of all the employees who names starts with ‘A’ ends with ‘A’.
Select * from employee where emp_name LIKE 'a%a';
Empty set (0.05 sec)
4 Find the name of employee and salary for those who had obtain minimum salary.
select emp_name, salary from employee where salary=(select min(salary)from employee);
+----------+--------+
| emp_name | salary |
+----------+--------+
| swati | 10000 |
+----------+--------+
1 row in set (0.05 sec)
5 Add 10% raise in salary of all employees whose department is ‘CIVIL’.
update employee set salary=salary+(salary*0.10)where dept_no=(select dept_no from depart where
dept_name='CIVIL');
Query OK, 2 rows affected (0.17 sec)
Rows matched: 2 Changed: 2 Warnings: 0
6 Count total number of employees of ‘MCA’ department.
select count(dept_no) from employee where dept_no=(select dept_no from depart where dept_name='MCA');
+----------------+
| count(dept_no) |
+----------------+
| 0|
+----------------+
1 row in set (0.03 sec)
7 List all employees who born in the current month.
Select emp_name from employee where MONTH(birth_date) = MONTH(CURRENT_DATE);
+----------+
| emp_name |
+----------+
| paresh |
| swati |
+----------+
2 rows in set (0.03 sec)
8 Print the record of employee and dept table as “Employee works in department ‘CE’.
select d.dept_name, e.emp_name from employee e, depart d where e.dept_no=d.dept_no and dept_name="CE" ;
+-----------+----------+
| dept_name | emp_name |
+-----------+----------+
| CE | pari |
+-----------+----------+
1 row in set (0.06 sec)
9 List names of employees who are fresher’s(less than 1 year of experience).
select emp_name from employee where experience <=1;
+----------+
| emp_name |
+----------+
| swati |
| jayesh |
| prem |
+----------+
3 rows in set (0.00 sec)
10 List department wise names of employees who has more than 5 years of experience.
select d.dept_name, e.emp_name from employee e, depart d where e.dept_no=d.dept_no and experience >5 ORDER
BY d.dept_name ASC;
+-----------+----------+
| dept_name | emp_name |
+-----------+----------+
| CE | pari |
+-----------+----------+
SET-5
Create the database STUD and create given tables with all necessary constraints such as primary key,
foreign key, unique key, not null and check constraints.
HOSTEL (HNO, HNAME, HADDR, TOTAL_CAPACITY, WARDEN)
ROOM (HNO, RNO, RTYPE, LOCATION, NO_OF_STUDENTS, STATUS)
CHARGES (HNO, RTYPE, CHARGES)
STUDENT (SID, SNAME, MOBILE-NO, GENDER, FACULTY, DEPT, CLASS, HNO, RNO)
FEES (SID, FDATE, FAMOUNT)
1 Display the total number of rooms that are presently vacant.
select count(rno) from room where status = 'vacant';
+------------+
| count(rno) |
+------------+
| 5|
+------------+
1 row in set (0.00 sec)
2 Display number of students of each faculty and department wise staying in each hostel
select count(sid),faculty from student group by faculty;
+------------+------------+
| count(sid) | faculty |
+------------+------------+
| 1 | khusbhoo |
| 1 | neha mam |
| 1 | Shashi sir |
| 2 | Tejash sir |
+------------+------------+
3 Display hostels, which have at least one single-seated room.
select a.* from hostel a, room b where b.hno = a.hno and b.rtype = 's';
+-----+----------+---------+----------------+--------+
| HNO | HNAME | HADDR | TOTAL_CAPACITY | WARDEN |
+-----+----------+---------+----------------+--------+
| 1 | Tejash P | navsari | 500 | vidhi |
| 2 | Shashi P | surat | 100 | vidhi |
+-----+----------+---------+----------------+--------+
4 Display the warden name and hostel address of students of Computer Science department.
select warden, haddr from hostel a, student b where b.hno = a.hno and b.dept = 3;
+--------+--------+
| warden | haddr |
+--------+--------+
| kamini | surat |
| pooja | bombay |
+--------+--------+
2 rows in set (0.00 sec)
5 Display those hostel details where single seated or double-seated rooms are vacant.
select * from hostel
where hno in (
select b.hno from hostel a, room b
where a.hno = b.hno and b.rtype in( 's','f') and status = 'vacant' group by b.hno
);
+-----+----------+---------+----------------+--------+
| HNO | HNAME | HADDR | TOTAL_CAPACITY | WARDEN |
+-----+----------+---------+----------------+--------+
| 1 | Tejash P | navsari | 500 | vidhi |
| 2 | Shashi P | surat | 100 | vidhi |
+-----+----------+---------+----------------+--------+
2 rows in set (0.05 sec)
6 Display details of hostels occupied by medical students.
select a.* from hostel a, room b where b.hno = a.hno and b.status = 'occupied by medical';
Empty set (0.00 sec)
7 Display hostels, which are totally occupied to its fullest capacity.
select a.* from hostel a, room b where b.hno = a.hno and b.status = 'occupied';
Empty set (0.00 sec)
8 List details about students who are staying in the double-seated rooms of Chanakya Hostel.
select * from student where hno in(
select a.hno from hostel a, room b
where b.hno = a.hno and a.hname = 'Shashi P' and rtype = 'f');
+-----+-------+------------+--------+------------+------+-------+------+------+
| SID | SNAME | MOBILENO | gender | FACULTY | DEPT | CLASS | HNO | RNO |
+-----+-------+------------+--------+------------+------+-------+------+------+
| 2 | karan | 9123476789 | m | Shashi sir | 2 | mca | 2 | 2 |
+-----+-------+------------+--------+------------+------+-------+------+------+
9 Display the total number of students staying in each room type of each hostel.
select sum(no_of_students), rtype from room group by rtype;
+---------------------+-------+
| sum(no_of_students) | rtype |
+---------------------+-------+
| 1000 | f |
| 1000 | s |
| 500 | t |
+---------------------+-------+
3 rows in set (0.00 sec)
10 Display details about students who have paid fees in the month of Nov. 2017
select * from student where sid in(
select sid from fees where MONTH(FDATE) =11 and year(FDATE)=2017);
Empty set (0.02 sec)
11 For those hostels where total capacity is more than 300, display details of students studying
in Science faculty.
select a.*, b.* from student a, hostel b
where a.hno = b.hno and a.faculty = 'Tejash sir' and b.TOTAL_CAPACITY> 300;
+-----+-------+------------+--------+------------+------+-------+------+------+-----+----------+---------+----------------+--------+
| SID | SNAME | MOBILENO | gender | FACULTY | DEPT | CLASS | HNO | RNO | HNO | HNAME | HADDR |
TOTAL_CAPACITY | WARDEN |
+-----+-------+------------+--------+------------+------+-------+------+------+-----+----------+---------+----------------+--------+
| 1 | meet | 9123456789 | m | Tejash sir | 1 | mca | 1 | 2 | 1 | Tejash P | navsari | 500 | vidhi |
+-----+-------+------------+--------+------------+------+-------+------+------+-----+----------+---------+----------------+--------+
12 Display hostel details where there are at least 10 vacant rooms
select a.*, b.rtype from hostel a, room b where a.hno = b.hno and b.status = 'vacant' and b.no_of_students>10;
+-----+----------+---------+----------------+--------+-------+
| HNO | HNAME | HADDR | TOTAL_CAPACITY | WARDEN | rtype |
+-----+----------+---------+----------------+--------+-------+
| 1 | Tejash P | navsari | 500 | vidhi | s |
| 2 | Shashi P | surat | 100 | vidhi | t |
| 2 | Shashi P | surat | 100 | vidhi | s |
| 2 | Shashi P | surat | 100 | vidhi | f |
| 2 | Shashi P | surat | 100 | vidhi | f |
+-----+----------+---------+----------------+--------+-------+
5 rows in set (0.00 sec)
13 Display details of students who have still not paid fees
select * from student where sid not in (select sid from fees where MONTH(FDATE) =10 and
year(FDATE)=2018);

Empty set (0.00 sec)


14 Display those hostels where single-seated room is the costliest.
select a.* from hostel a, charges b where a.hno = b.hno and rtype = 's' and charges = (select max(charges)
from charges);
+-----+----------+-------+----------------+--------+
| HNO | HNAME | HADDR | TOTAL_CAPACITY | WARDEN |
+-----+----------+-------+----------------+--------+
| 2 | Shashi P | surat | 100 | vidhi |
| 3 | Dhaval S | surat | 500 | kamini |
+-----+----------+-------+----------------+--------+
2 rows in set (0.00 sec)
SET-6
Create the database HOSPITAL and create given tables with all necessary constraints such as primary
key, foreign key, unique key, not null and check constraints.
DOCTOR (DNO, DNAME, SPECIALIZATION, CLINIC_ADDR)
MEDICINE (MNO, MNAME, TYPE, CONTENT, MANUFACTURER)
DISEASE (DISEASE_NAME, SYMPTOM1, SYMPTOM2, SYMPTOM3)
TREATMENT (TNO, DNO, DISEASE_NAME, MNO, DOSAGE, AVG_CURE_TIME)
1 Display records of each table in ascending order.
select * from doctor ORDER BY dno;
+-----+----------+----------------------+-------------+
| DNO | DNAME | SPECIALIZATION | CLINIC_ADDR |
+-----+----------+----------------------+-------------+
| 1 | Dr.Prem | Anesthesiology | chennai |
| 2 | Dr.ram | cancer | delhi |
| 3 | Dr.raj | Dermatologists | surat |
| 4 | Dr.rahul | DIAGNOSTIC RADIOLOGY | delhi |
| 5 | Dr.yash | Hematology | delhi |
+-----+----------+----------------------+-------------+
5 rows in set (0.00 sec)

select * from MEDICINE ORDER BY mno;


+-----+------------+-------------+----------+--------------+
| MNO | MNAME | TYPE | CONTENT | MANUFACTURER |
+-----+------------+-------------+----------+--------------+
| 10 | penicillin | Depressants | MEDICINE | surat |
| 20 | Adderall | Stimulants | MEDICINE | surat |
| 30 | Ativan | Opioids | MEDICINE | rajkot |
| 40 | Amlodipine | Opioids | MEDICINE | surat |
| 50 | lamivudine | Opioids | MEDICINE | rajkot |
+-----+------------+-------------+----------+--------------+
5 rows in set (0.00 sec)

select * from DISEASE ORDER BY DISEASE_NAME;


+--------------+------------+--------------+-------------+
| DISEASE_NAME | SYMPTOM1 | SYMPTOM2 | SYMPTOM3 |
+--------------+------------+--------------+-------------+
| cancer | Hoarseness | weightloss | genitalarea |
| depression | Hopeless | Lostinterest | Anxiety |
| migraines | fever | vomiting | headache |
+--------------+------------+--------------+-------------+
3 rows in set (0.05 sec)

select * from TREATMENT ORDER BY tno;


+-----+------+--------------+------+--------+---------------+
| TNO | DNO | DISEASE_NAME | MNO | DOSAGE | AVG_CURE_TIME |
+-----+------+--------------+------+--------+---------------+
| 111 | 1 | cancer | 10 | 3 |5 |
| 222 | 2 | migraines | 20 | 5 |3 |
| 333 | 3 | depression | 30 | 5 |3 |
| 444 | 4 | cancer | 40 | 5 |3 |
| 555 | 5 | migraines | 50 | 5 |3 |
+-----+------+--------------+------+--------+---------------+
5 rows in set (0.00 sec)
2 Count total number of doctors which has not given any treatment.
SELECT * FROM `doctor` WHERE DNO NOT IN (SELECT DNO FROM treatment);
Empty set (0.06 sec)
3 Display all Chennai doctors who treat cancer.
SELECT * FROM `doctor` WHERE CLINIC_ADDR = 'chennai' AND SPECIALIZATION = 'cancer';
+-----+----------+----------------------+-------------+
| DNO | DNAME | SPECIALIZATION | CLINIC_ADDR |
+-----+----------+----------------------+-------------+
| 1 | Dr.Prem | cancer | chennai |
+-----+----------+----------------------+-------------+
5 rows in set (0.00 sec)
4 Remove disease “polio” from disease table as well as treatment table.
DELETE FROM disease WHERE DISEASE_NAME = ' polio ';
DELETE FROM treatment WHERE DISEASE_NAME = ' polio';
5 Delete all those treatment related to liver of Dr.Shah.
DELETE FROM `treatment` WHERE `DNO` = (SELECT DNO FROM doctor WHERE DNAme = '
Dr.Shah ');
6 Create index on dno, Disease name in the treatment table
CREATE INDEX in_dno on disease(DISEASE_NAME);
Query OK, 0 rows affected (0.26 sec)
Records: 0 Duplicates: 0 Warnings: 0
7 Display details of doctors who treat migraines.
SELECT * FROM `doctor` WHERE DNO = (SELECT DNO FROM treatment WHERE disease_name =
'migraines');
+-----+----------+----------------------+-------------+
| DNO | DNAME | SPECIALIZATION | CLINIC_ADDR |
+-----+----------+----------------------+-------------+
| 2 | Dr.ram | cancer | delhi |
+-----+----------+----------------------+-------------+
1 rows in set (0.00 sec)
8 What is the maximum dosage of “penicillin” prescribe by the doctor for the treatment of any
disease?
SELECT MAX(DOSAGE) FROM treatment WHERE MNO = (SELECT MNO FROM medicine WHERE
mname = 'penicillin');
+-------------+
| MAX(DOSAGE) |
+-------------+
|3 |
+-------------+
1 row in set (0.04 sec)
9 Display total number of disease treated by every doctor.
SELECT COUNT(DISEASE_NAME) as NumberOfDisease FROM `treatment`;
+-----------------+
| NumberOfDisease |
+-----------------+
| 5|
+-----------------+
1 row in set (0.03 sec)
10 Which doctor have no treatment for “depression”?
SELECT * FROM `doctor` WHERE dno not in (SELECT dno FROM treatment WHERE disease_name =
'depression');
+-----+----------+----------------------+-------------+
| DNO | DNAME | SPECIALIZATION | CLINIC_ADDR |
+-----+----------+----------------------+-------------+
| 1 | Dr.Prem | Anesthesiology | chennai |
| 2 | Dr.ram | cancer | delhi |
| 4 | Dr.rahul | DIAGNOSTIC RADIOLOGY | delhi |
| 5 | Dr.yash | Hematology | delhi |
+-----+----------+----------------------+-------------+
4 rows in set (0.00 sec)
SET-7
Create the database SHOPPING and create given tables with all necessary constraints such as primary
key, foreign key, unique key, not null and check constraints.
CUSTOMER (cno, cust_name, cust_phone, location,gender)
ITEM (itemno, itemname, color, weight, expire_date, price, shop_name)
CUST_ITEM (cno, itemno, quantity_purchased, date_purchase)
1 Delete the items whose price is more than 50000.
delete from cust_item where itemno in(select itemno from item where price > 50000);
Query OK, 1 row affected (0.13 sec)

delete from item where price > 50000;


Query OK, 1 row affected (0.05 sec)
2 Find the names of the customer who is located in same location as that of other customer.
select cust_name,location from CUSTOMER where location='valod';
+-----------+----------+
| cust_name | location |
+-----------+----------+
| ankit | valod |
| arkit | valod |
| yash | valod |
+-----------+----------+
3 rows in set (0.00 sec)
3 Display the names of items which is black, white & brown in color.
select * from item where color in ('black','white','brown');
+--------+----------+-------+--------+-------------+-------+-----------+
| itemno | itemname | color | weight | expire_date | price | shop_name |
+--------+----------+-------+--------+-------------+-------+-----------+
| 10 | pumps | black | 500 | 2019-07-10 | 5000 | bs pumps |
| 30 | mobile | white | 300 | 2019-07-10 | 15000 | rajmobile |
| 40 | card | brown | 100 | 2019-07-10 | 1000 | rajmobile |
| 50 | chargers | black | 200 | 2019-07-10 | 600 | rajmobile |
+--------+----------+-------+--------+-------------+-------+-----------+
4 rows in set (0.03 sec)
4 Display the names of all the items whose names lies between ‘p’ and‘s’.
select * from item where itemname between 'P' and 'S';
+--------+----------+-------+--------+-------------+-------+-----------+
| itemno | itemname | color | weight | expire_date | price | shop_name |
+--------+----------+-------+--------+-------------+-------+-----------+
| 10 | pumps | black | 500 | 2019-07-10 | 5000 | bs pumps |
+--------+----------+-------+--------+-------------+-------+-----------+
1 row in set (0.04 sec)
5 Find the item which is having less weight.
select * from item where weight = (select min(weight) from item);
+--------+----------+-------+--------+-------------+-------+-----------+
| itemno | itemname | color | weight | expire_date | price | shop_name |
+--------+----------+-------+--------+-------------+-------+-----------+
| 40 | card | brown | 100 | 2019-07-10 | 1000 | rajmobile |
+--------+----------+-------+--------+-------------+-------+-----------+
1 row in set (0.04 sec)
6 Add one month more to those items whose item no =40.
SELECT expire_date, DATE_ADD(expire_date, INTERVAL 1 MONTH) from item where itemno = 40;
+-------------+-----------------------------------------+
| expire_date | DATE_ADD(expire_date, INTERVAL 1 MONTH) |
+-------------+-----------------------------------------+
| 2019-07-10 | 2019-08-10 |
+-------------+-----------------------------------------+
1 row in set (0.00 sec)
7 Count total number of items which is going to expire in next month
select count(itemno) from item where MONTH(expire_date) = MONTH(CURDATE());
+---------------+
| count(itemno) |
+---------------+
| 0|
+---------------+
1 row in set (0.22 sec)
8 List all customers whose phone number starts with ‘99’.
select * from customer where cust_phone like '99%';
+-----+-----------+------------+----------+--------+
| cno | cust_name | cust_phone | location | gender |
+-----+-----------+------------+----------+--------+
| 1 | ankit | 9912345678 | valod | m |
| 2 | arkit | 9912344478 | valod | m |
| 4 | raj | 9999345678 | surat | m |
| 5 | yash | 9912345688 | valod | m |
+-----+-----------+------------+----------+--------+
4 rows in set (0.06 sec)
9 Display total value (qty*price) for all items.
select b.quantity_purchased,a.price,(quantity_purchased * PRICE) as value from item a ,cust_item b where
a.itemno = b.itemno;
+--------------------+-------+-------+
| quantity_purchased | price | value |
+--------------------+-------+-------+
|2 | 5000 | 10000 |
|4 | 15000 | 60000 |
|2 | 1000 | 2000 |
|2 | 600 | 1200 |
+--------------------+-------+-------+
4 rows in set (0.00 sec)
10 List customer details who has purchased maximum number of items
select * from CUST_ITEM where quantity_purchased = (select max(quantity_purchased) from
CUST_ITEM);
+------+--------+--------------------+---------------+
| cno | itemno | quantity_purchased | date_purchase |
+------+--------+--------------------+---------------+
| 3 | 30 | 4 | 2019-08-05 |
+------+--------+--------------------+---------------+
1 row in set (0.04 sec)
11 Display total price item wise.
select itemname, price from item;
+----------+-------+
| itemname | price |
+----------+-------+
| pumps | 5000 |
| mobile | 15000 |
| card | 1000 |
| chargers | 600 |
+----------+-------+
4 rows in set (0.00 sec)
12 List name of items, customer details and qty purchased.
select i.itemname, c.*, t.quantity_purchased from customer c, item i, cust_item t where c.cno = t.cno and
i.itemno = t.itemno;
+----------+-----+-----------+------------+----------+--------+--------------------+
| itemname | cno | cust_name | cust_phone | location | gender | quantity_purchased |
+----------+-----+-----------+------------+----------+--------+--------------------+
| pumps | 1 | ankit | 9912345678 | valod | m |2 |
| mobile | 3 | vishal | 7712345678 | bardoli | m |4 |
| card | 4 | raj | 9999345678 | surat | m |2 |
| chargers | 5 | yash | 9912345688 | valod | m |2 |
+----------+-----+-----------+------------+----------+--------+--------------------+
4 rows in set (0.03 sec)

You might also like