Professional Documents
Culture Documents
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 | mouryaa424@gmail.com |
| 10 | raj | 1996-06-14 | m | 70 | surat | emp | 30000 | 6 | raj@gmail.com |
| 11 | ragini | 1996-06-01 | f | 90 | surat | emp | 30000 | 6 | ragini@gmail.com |
| 12 | rahul | 1996-05-01 | m | 100 | surat | emp | 45000 | 6 | rahul@gmail.com |
| 13 | nimesh | 1999-05-07 | m | 110 | surat | emp | 40000 | 6 | nimesh@gmail.com |
| 14 | yash | 1996-05-05 | m | 120 | surat | emp | 40000 | 6 | yash@gmail.com |
| 15 | lalu | 1996-05-05 | m | 140 | surat | emp | 40000 | 6 | lalu@gmail.com |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+----------------------
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 | ragini@gmail.com |
|9 | varsa | 1998-02-13 | f | 90 | surat | emp | 30000 | 6year | v1ds@gmail.com |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+------------------+
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 | sajan@gmail.com |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
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 | karan@gmail.com |
+--------+----------+------------+--------+---------+----------+-------------+--------+------------+-----------------+
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
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');
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);