Rdbms Lab Report(d)090
Rdbms Lab Report(d)090
1. Create table following tables with the attributes mentioned below STUDENT (USN,SNAME,
ADDRESS ,PHONE) SEMSEC (SSID , SEM ,SEC ) , IAMARKS (USN ,SUBID,SSID
,TEST1,TEST2,TEST3 ,FINALIA) SUBJECT (SUBID,TITLE,SEM,CREDIT) CLASS
(USN,SSID
SHOW TABLES;
2. Add new column with name GENDER with datatype varchar and size 1 in table STUDENT
ALTER TABLE STUDENT ADD GENDER VARCHAR(1);
SELECT * FROM STUDENT;
3. Add two new columns FEES as integer and DOB as varchar in table STUDENT
4. Change the data type of column DOB to Date type in student table
Program No:2
------------------------------------------------------------------------------------------------------
Title: Design and implement a database and apply at least 10 different DML queries for the
following task. For a given input string display only those records which match the given pattern
or a phrase in the search string. Make use of wild characters and LIKE operator for the same.
Make use of Boolean and arithmetic operators wherever necessary.
-------------------------------------------------------------------------------------------------------
1.Create table emp_chk with attributes EMP_ID varchar(10), ENAME varchar (15), JOB varchar
(15), HIREDATE DATE, SAL varchar(10), COMM varchar(10), DEPT_NO varchar(10).
2.Insert the records into the emp_chk table and display all the records of the emp_chk table.
3.Create another table emp with all the attributes of emp_chk table and insert all the records of
emp_chk table into emp table at a time.
4.Display all the information of emp table whose salary is greater than 2000
/*OR*/
SET SQL_SAFE_UPDATES = 0;
update emp set SAL = 3000 where EMP_ID = 5;
SET SQL_SAFE_UPDATES = 1;
SELECT * FROM emp;
10.Display the salary of employee whose salary is in the range of 2000 to 5000.
11.Display the records of employee whose jobs are ‘Manager’,’Supervisor’ and ‘Worker’.
SELECT * FROM emp WHERE ENAME LIKE 'A%' or ENAME LIKE 'S%';
13.Display the name of employees whose name does not start with A.
14.Write a SQL statement to increase the salary of employees under the department 2, 5 and 8
according to the company rules that, salary will be increased by 25% for the department 2, 15%
for department 5 and 10% for the department 8 and the rest of the departments will remain same.
---------------------------------------------------------------------------------------------------------------
Table Creation -
CREATE TABLE PUBLISHER (PUBL_NAME VARCHAR (20) PRIMARY KEY,
PHONE BIGINT,
ADDRESS VARCHAR (20));
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each branch, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to
Jun 2017.
SELECT CARD_NO
FROM BOOK_LENDING
WHERE DATE_OUT BETWEEN '2017-01-21' AND '2017-09-15'
GROUP BY CARD_NO
HAVING COUNT(*)>3;
SELECT CARD_NO
FROM BOOK_LENDING
WHERE DATE_OUT BETWEEN '2017-01-21' AND '2017-09-15'
GROUP BY CARD_NO
HAVING COUNT(*)>3;
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working with a simple
query.
6. List the book id, title ,publisher name, author name along with total number of copies of each
textbook and sort the records in the descending order of book_id.
SELECT B.BOOK_ID, B.TITLE, B.PUBL_NAME, A.AUTHOR_NAME,
SUM(C.NO_OF_COPIES) AS Total_Copies
FROM BOOK B
JOIN BOOK_AUTHORS A ON B.BOOK_ID = A.BOOK_ID
JOIN BOOK_COPIES C ON B.BOOK_ID = C.BOOK_ID
JOIN LIBRARY_BRANCH L ON C.BRANCH_ID = L.BRANCH_ID
GROUP BY B.BOOK_ID, B.TITLE, B.PUBL_NAME, A.AUTHOR_NAME
ORDER BY B.BOOK_ID DESC
7. List the book id, title, publisher name, along with total number of copies of each textbook
where number of copies is greater than 2 and sort the records in the ascending order of book_id.
9. Create / Alter table by ADD/ DROP CONSTRAINT – like NOT NULL, Unique, CHECK,
Primary Key, INDEX, AUTO INCREMENT, Foreign Key, DEFAULT
CREATE INDEX idx_BOOKS ON BOOK_AUTHORS (AUTHOR_NAME);
SHOW INDEXES FROM BOOK_AUTHORS;
Lab Programs – 4 AND 5 (On Aggregate Functions and Nested / Sub Queries) Title: Implement
nested sub queries (Single Row Query / Multiple Row Queries) While Creating Table Declare
constraints where ever required
1. Create Table Employees, using constraints on required columns (PK, NOT NULL, CHECK,
FK)
CREATE TABLE IF NOT EXISTS LOCATION (
LOCATION_ID INT NOT NULL PRIMARY KEY,
ADRESS VARCHAR(20) DEFAULT NULL,
POSTAL_CODE INT NOT NULL,
CITY VARCHAR(20) NOT NULL,
STATE_PROVINCE VARCHAR(20) DEFAULT NULL,
COUNTRY_ID VARCHAR(20) NOT NULL
);
DESC LOCATION;
CREATE TABLE IF NOT EXISTS Department (
DEPARTMENT_ID INT NOT NULL PRIMARY KEY,
DEPARTMENT_NAME VARCHAR(20) DEFAULT NULL,
MANAGER_ID INT UNIQUE NOT NULL,
LOCATION_ID INT DEFAULT NULL,
FOREIGN KEY (LOCATION_ID) REFERENCES LOCATION (LOCATION_ID)
);
DESC Department;
DESC employees;
4. Write a SQL query to find those employees whose salary matches the lowest salary of any of
the departments. Return first name, last name and department ID.
6. write a SQL query to find all those employees who work in the IT department. Return
department ID, name (first), job ID and department name.
8. write a SQL query to find those employees who do not work in the departments where
managers’ IDs are between 100 and 110 (Begin and end values are included.). Return all the
fields of the employees.
9. write a SQL query to find those employees who get second-highest salary. Return all the fields
of the employees.
11. write a SQL query to calculate total salary of the departments where at least one employee
works. Return department ID, total salary
SELECT employee_id, first_name, last_name, CASE job_id WHEN ' FI_MGR' THEN
‘MANAGER’ WHEN 'IT_PROG' THEN 'DEVELOPER' ELSE job_id END AS designation,
salary FROM employees;
13. write a SQL query to find those employees whose salaries exceed 50% of their department's
total salary bill. Return first name, last name.
15. write a SQL query to find those managers who supervise four or more employees. Return
manager name, department ID.
17. write a SQL query to find the employees and their managers. These managers do not work
under any manager. Return the first name of the employee and manager .
19. write a SQL query to find the department name, department ID, and number of employees in
each department.
PRACTICE :
20. write a SQL query to find those employees who earn more than the average salary. Sort the
result-set in descending order by salary. Return first name, last name, salary, and department ID.
SELECT first_name, last_name , salary, department_id FROM employees WHERE salary > (
SELECT AVG(salary) FROM employees ) ORDER BY salary DESC;
21. write a SQL query to find the first name, last name, department number, and department
name for each employee
Note use previously created Employees table to perform the below Programs
1. Write a SQL query to find those employees whose salary is lowest / Highest / Avg salary by
the departments
a) Lowest Salary
USE Employees;
SHOW PROCEDURE STATUS WHERE Db = 'Employees';
DELIMITER //
CREATE PROCEDURE my_proc_Min_Sal()
BEGIN
SELECT department_id, MIN(salary) AS Min_Salary
FROM employees -- Make sure "employees" table exists
GROUP BY department_id;
END//
DELIMITER ;
CALL my_proc_Min_Sal();
b) Highest salary
DELIMITER //
CREATE PROCEDURE my_proc_Max_Sal()
BEGIN
SELECT department_id, MAX(salary) AS Max_Salary
FROM employees
GROUP BY department_id;
END//
DELIMITER ;
CALL my_proc_Max_Sal();
c) Average salary
DELIMITER //
CREATE PROCEDURE my_proc_Avg_Sal()
BEGIN
SELECT department_id, AVG(salary) AS Avg_Salary
FROM employees
GROUP BY department_id;
END//
DELIMITER ;
CALL my_proc_Avg_Sal();
2. Write a SQL query to find those employees who earn more than the average salary. Sort the
result-set in descending order by salary. Return first name, last name, salary, and department ID.
DELIMITER //
DELIMITER ;
CALL my_proc_Morethan_AVG_Sal();
3. Write a SQL query to find the first name, last name, department number, and department name
for each employee
DELIMITER //
DELIMITER ;
CALL my_proc_emp_dept_details();
4. Write a SQL query to find those employees who receive a higher salary than the employee
with ID 114. Return first name, last name (/* IN Parameter*/)
DELIMITER //
DELIMITER ;
CALL empl_info(114);
5. Write a SQL query to calculate the number of employees receiving salary more than 10000.
Return number of employees. (/* OUT Parameter*/)
DELIMITER //
DELIMITER ;
/* To store the value returned by the procedure, needs to be passed to a session variable named
@Emp_Count */
/* Expected Solution– Query should return, total number of employees having salary >=10000 */
6. Write a SQL query to find those employees who receive a higher salary than the employee
with ID declared IN Parameter. Return Count of Employees whose salary is greater than
Employee ID passed in the IN parameter (/* INOUT Parameter*/)
DELIMITER //
DELIMITER ;
-- Calling the procedure and storing result in a session variable
CALL empl_info_InOut(@Emp_Sal, 114);
SELECT @Emp_Sal AS No_of_Emp;
/* Expected Solution – Count of Employees whose salary is greater than the salary of Parameter
passed Employee Id is displayed. */
4. Create Simple Stored Procedure to return all records from Student Marks table
DELIMITER $$
DELIMITER ;
CALL stored_proc.GetStudentData();
5. Create a procedure to fetch the details of students with the student ID being passed as an Input
parameter.
DELIMITER //
DELIMITER ;
CALL stored_proc.spGetDetailsByStudentName(1);
6. To calculate the average marks of all the students from the studentMarks table and return the
average as an OUT field.
DELIMITER //
DELIMITER ;
CALL stored_proc.spGetAverageMarks(@average_marks);
SELECT @average_marks;
7. Find the count of students who is having marks below the average marks of all the students
using Local variables
DELIMITER //
DELIMITER ;
8. To fetch the highest marks from a student data table. We can have one like this with the
highest marks stored in an OUT parameter.
DELIMITER //
DELIMITER ;
(Nested Procedure) Call a procedure from another procedure to return the overall result of a
student. If student marks are above average – then the result would be PASS else – FAIL
DELIMITER $$
DELIMITER ;
DELIMITER $$
DELIMITER ;
10. Write a procedure to take studentId and depending on the studentMarks we need to return the
class according to the below criteria. ( Using Conditional statements)
DELIMITER $$
CREATE PROCEDURE stored_proc.spGetStudentClass(IN studentId INT, OUT class
VARCHAR(20))
BEGIN
DECLARE marks INT DEFAULT 0;
DELIMITER ;
a. For student ID – 1 – total_marks are 450 – hence the expected result is FIRST CLASS.
b. For student ID – 6 – total_marks is 380 – Hence the expected result is SECOND CLASS
CALL stored_proc.spGetStudentClass(6, @class);
SELECT @class;
11. Create a procedure that would insert a record in the studentMarks table and have IN
parameters as studentId, total_marks, and grade. We are also adding an OUT parameter named
rowCount which would return the total count of records in the studentMarks table. Add the EXIT
Error Handler for Duplicate Key record i.e. if someone invokes it for inserting a record with an
existing studentID, then the Error handler would be invoked and will return an appropriate error.
DELIMITER $$
DELIMITER ;
– The output would display an error message defined in the error handler. Since it’s an exit
handler, the main procedure flow would not continue.
– Hence, in this case the rowCount OUT parameter would not be updated as this statement is
after the INSERT statement that had generated the error condition.
DROP this procedure, and re-create with CONTINUE action instead of EXIT for the error
handler.
DELIMITER $$
-- Count total records in the table (This will execute even if an error occurs)
SELECT COUNT(*) INTO rowCount FROM stored_proc.studentMarks;
END $$
DELIMITER ;
-- Fetch the updated value of rowCount (Will still update despite error)
SELECT @rowCount;