Csl33 Database Management Lab-Exercises (SQL)
Csl33 Database Management Lab-Exercises (SQL)
EXERCISES
EXERCISE:1 - STUDENT DATABASE
Aim:
Create a table student with following information:
Student ( rollno, name ,dob, dept, marks)
a) Insert rows into the table.Using various ways of insertion(insert)command.
b) Alter the student table to contain address of the student,and also modify the size of
name.
c) Display the content of the table.
d) Insert values into address field and display the content.
e) Retrieve names of students whose age is less than 20.
f) Arrange table in ascending order of marks.
g) Retrieve names of students with department CS and marks greater than 70.
h) Retrieve names of students with marks greater than average marks.
i) drop the student table.
d)Insert some values into address field and display the content.
NAME DOB
---------- ---------
Gourikripa 28-NOV-89
Pranav 27-NOV-89
g) Retrieve names of students with department CS and marks greater than 500.
SELECT name FROM student WHERE dept= „CS‟ and marks>500;
NAME
----------
Pranav
Aim:
Create table emp(emp_id char(8) check(emp_id like 'E%') primary key, emp_name
varchar(18),street_no int,city varchar(18));
insert into emp values(„E-101‟,'Adarsh',101,'MG Road');
insert into emp values(„E-102‟,'Bonny',101, 'MG Road');
insert into emp values(„E-103‟,'Catherine', 102, 'Cochin');
insert into emp values(„E-104‟,'Glenn', 104, 'Ernakulam');
insert into emp values(„E-105‟,'George', 201,'MG Road');
insert into emp values(„E-106‟,'Hayes', 101, 'MG Road');
insert into emp values(„E-107‟,'Johnson',102,‟Cochin‟);
insert into emp values(„E-108‟,'Jones', 101, 'Cochin');
insert into emp values(„E-109‟,„Karthik‟, 101, 'Ernakulam');
insert into emp values(„E-110‟,'Lavanya', 101, 'Palace Road');
insert into emp values(„E-111‟,'Niharika', 102, „Ernakulam‟);
Create table company(company_name varchar(18) primary key, city varchar(18));
insert into company values('SBI', 'MG Road');
insert into company values('SBT', 'MG Road' );
insert into company values('Federal',‟Broadway‟);
insert into company values('Indian Bank', „Cochin‟);
insert into company values('SIB', 'Ernakulam');
insert into company values('HDFC', 'Palace Road');
insert into company values('Axis',‟Cochin‟);
insert into company values('City bank', 'Ernakulam');
EMP_NAME
------------------
Adarsh
b) Find all employees in the database who live in the same cities as the companies for
which they work.
EMP_NAME
------------------
Adarsh
George
c) Find all employees and their managers in the database who live in the same cities and on the
same street number as do their managers.
d) Find all employees who earn more than the average salary of all employees of their company.
e). Find the company that pay least total salary along with the salary paid.
SELECT company_name,sum(salary) “SALARY PAID” from Works GROUP
BY company_name HAVING sum(salary) <= all (SELECT sum(salary) FROM
Works GROUP BY company_name)
COMPANY_NAME
------------------
SBT
h) Find those companies whose employees earn a higher salary, on average than the
average salary at Indian Bank.
COMPANY_NAME
------------------
SBI
Federal
SBT
i).Query to find name and salary of all employees who earn more than each employee
of „Indian Bank‟
SELECT emp_name,salary FROM works,emp
WHERE salary > (SELECT MAX(salary) FROM works WHERE company_name =
„Indian Bank‟ GROUP BY company_name) and emp.emp_id=works.emp_id;
EMP_NAME SALARY
------------------ ----------
Adarsh 71000
Bonny 99000
Catherine 40000
Glenn 37000
Lavanya 250000
Niharika 40000
g) Display the name of faculties who take more than one subject.
Select f_name name from Faculty where (select count(subjectcode) from Subject where
Subject.facultycode=Faculty.f_code)>1 group by Faculty.f_name;
or
select Faculty.f_name,count(subject.SubjectCode) “NO OF SUBJECTS” from
Faculty,subject where (select count(*) from Subject where
Subject.facultycode=Faculty.f_code)>1 and Subject.facultycode=Faculty.f_code group
by Faculty.f_name;
F_NAME NO OF SUBJECTS
--------------- -----------------------
Vidhya 2
h) Display name,subject,mark, % of mark in ascending order of mark
select studentname,subjectname,mark from Student1,Subject,M_mark where
Student1.studentcode=M_mark.studentcode and Subject.subjectcode=
M_mark.subjectcode order by mark;
EXERCISE:4 -DEPARTMENT DATABASE
Aim:
Create two tables
Dept(Department_Id, Department_Name , Manager_id, Loc)
Emp(Emp_no , Emp_name,Job , Salary , Hiredate,Comm , Depno )
MANAGER_ID is the empno of the employee whom the employee reports to. DEPTNO is a
foreign key.Insert these values into department table
1) Display the name and salary for all employees whose salary is not in the range of 5000 and
35000
2) Display the employee name, job ID, and start date of employees hired between February 20,
1990, and May 1, 1998. Order the query in ascending order by start date.
3) list the name and salary of employees who earn between 5,000 and12,000, and are in
department 2 or 4. Label the columns Employee and Monthly Salary,respectively.
4)Display the name and hire date of every employee who was hired in 1994.
5). Display the name, salary, and commission for all employees who earn commissions. Sort
data in descending order of salary and commissions.
6) Display the name and job title of all employees who do not have a manager.
7). Display the names of all employees where the third letter of the name is an a.
8). Display the name of all employees who have an a and an e in their name.
9). Display the name, job, and salary for all employees whose job is sales representative or stock
clerk and whose salary is not equal to 2,0000, 4000, or 7,000.
10)Write a query that displays the employee‟s names with the first letter capitalized and all other
letters lowercase and the length of the name for all employees whose name starts with J, A, or M.
Give each column an appropriate label. Sort the results by the employees‟ names.
11)For each employee, display the employee‟s name, and calculate the number of months
between today and the date the employee was hired and years worked. Label the column
MONTHS_WORKED. Order your results by the number of months employed. Round the
number of months and year up to the closest whole number.
12). Write a query to display the name, department number, and department name for all
employees.
13) Create a query to display the name and hire date of any employee hired after employee
Mathew
14) Display the names and hire dates for all employees who were hired before their managers,
along with their manager‟s names and hire dates. Label the columns Employee, EmpHired,
Manager, and Mgr Hired, respectively.
15) Write a query to display the number of people with the same job.
16). Display the manager number and the salary of the lowest paid employee for that
manager.Exclude anyone whose manager is not known. Exclude any groups where the minimum
salary is less than 6,000. Sort the output in descending order of salary.
17. Write a query to display each department‟s name, location, number of employees, and the
average salary for all employees in that department. Label the columns Name, Location, Number
of People, and Salary, respectively. Round the average salary to two decimal places.
18). Write a query to display the name and hire date of any employee in the same
department as amit. Exclude JOHN.
19. Write a query that displays the employee numbers names of all employees who work in a
department with any employee whose name contains a u.
20)display employee name and department name of all employees that work in a department that
has at least 3 employees. Order the list in alphabetical order first by department name, then by
employee name.
21. Write a query to list the length of service of the employees (of the form n years and m
months).
1) Display the name and salary for all employees whose salary is not in the range of 5000 and
35000
SELECT emp_name, salary FROM emp WHERE salary NOT BETWEEN 5000 AND
35000;
EMP_NAME SALARY
---------- ----------
Lex 240000
Ismael 4000
Mathew 46000
Henin 240000
2) Display the employee name, job ID, and start date of employees hired between February 20,
1990, and May 1, 1998. Order the query in ascending order by start date.
SELECT emp_name, job, hiredate FROM emp WHERE hiredate BETWEEN '20-Feb-
1990' AND '01-May-1998' ORDER BY hiredate
3) list the name and salary of employees who earn between 5,000 and12,000, and are in
department 2 or 4. Label the columns Employee and Monthly Salary,respectively.
4)Display the name and hire date of every employee who was hired in 1994.
SELECT emp_name, hiredate FROM emp WHERE hiredate LIKE '%94';
EMP_NAME HIREDATE
========== =========
Ismael 29-MAR-94
5). Display the name, salary, and commission for all employees who earn commissions. Sort
data in descending order of salary and commissions.
6) Display the name and job title of all employees who do not have a manager.
EMP_NAME JOB
========== ==========
Greesh Clerk
7). Display the names of all employees where the third letter of the name is an a.
EMP_NAME
==========
Diana
8). Display the name of all employees who have an a and an e in their name.
SELECT emp_name FROM emp WHERE emp_name LIKE '%a%' AND emp_name
LIKE '%e%';
EMP_NAME
==========
Neena
Alexander
Ismael
Mathew
Hayes
9). Display the name, job, and salary for all employees whose job is sales representative or stock
clerk and whose salary is not equal to 2,0000, 4000, or 7,000.
SELECT emp_name, job, salary FROM emp WHERE job IN ('Sa_rep', 'CLERK') AND
salary NOT IN (2000, 4000, 7000);
10)Write a query that displays the employee‟s names with the first letter capitalized and all other
letters lowercase and the length of the name for all employees whose name starts with J, A, or M.
Give each column an appropriate label. Sort the results by the employees‟ names.
Name Length
------------------------------ ----------
Alexander 9
John 4
Mathew 6
11)For each employee, display the employee‟s name, and calculate the number of months
between today and the date the employee was hired and years worked. Label the column
MONTHS_WORKED. Order your results by the number of months employed. Round the
number of months and year up to the closest whole number.
.
SELECT emp_name, ROUND(MONTHS_BETWEEN(SYSDATE, hiredate))
MONTHS_WORKED, round(MONTHS_BETWEEN(SYSDATE, hiredate)/12,2) “NO: Of
YEARS” FROM emp ORDER BY MONTHS_BETWEEN(SYSDATE, hiredate);
12). Write a query to display the name, department number, and department name for all
employees.
13) Create a query to display the name and hire date of any employee hired after employee
Mathew
EMP_NAME HIREDATE
------------------------- ----- ---------
Hayes 21-APR-98
Ismael 29-MAR-94
Steven 18-MAY-93
John 01-DEC-92
14) Display the names and hire dates for all employees who were hired before their managers,
along with their manager‟s names and hire dates. Label the columns Employee, EmpHired,
Manager, and Mgr Hired, respectively.
select emp_name “manager”, (select min(salary) from emp e where (emp.depno=e.depno) group by
e.depno having min(salary)>13000) “salary” from emp,dept where emp.emp_no=dept. MANAGER_ID
and emp.depno=dept. DEPARTMENT_ID
17. Write a query to display each department‟s name, location, number of employees, and the
average salary for all employees in that department. Label the columns Name, Location, Number
of People, and Salary, respectively. Round the average salary to two decimal places.
18). Write a query to display the name and hire date of any employee in the same
department as amit. Exclude JOHN.
EMP_NAME HIREDATE
------------------------------ ---------------
Neena 06-FEB-87
Lex 06-JAN-80
Henin 06-AUG-80
19. Write a query that displays the employee numbers names of all employees who work in a
department with any employee whose name contains a u.
21. Write a query to list the length of service of the employees (of the form n years and m
months).
EMP_NAME SALARY
arun 20000
anil 15000
beena 11000