0% found this document useful (0 votes)
16 views

DBMS_Lab QBank (1)

The document outlines SQL operations for managing employee and customer databases, including creating tables, inserting records, and performing queries. It covers various operations such as creating views, updating records, displaying specific employee details, and managing customer orders. Additionally, it includes creating relationships between tables using foreign keys and performing data retrieval based on specific conditions.

Uploaded by

sdkomejwar2110
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)
16 views

DBMS_Lab QBank (1)

The document outlines SQL operations for managing employee and customer databases, including creating tables, inserting records, and performing queries. It covers various operations such as creating views, updating records, displaying specific employee details, and managing customer orders. Additionally, it includes creating relationships between tables using foreign keys and performing data retrieval based on specific conditions.

Uploaded by

sdkomejwar2110
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/ 18

1) Consider the relation employee (emp_id, e_name, salary, Date_of_joining, Dept_no, Designation) perform basic

SQL operations.

1. Create table employee.


2. Insert 10 records in table.
3. Create a view emp_vl of table employee which has emp_id, name and dept-attributes.
4. Create view of table.
5. Update dept of any employee in view. Check whether it gets updated or not.
6. Create emp_id as primary key and show indices on table employee.
7. Show indices on table.
8. Create user defined index on any column.

Answer:

CREATE DATABASE XYZ;

USE XYZ;

CREATE TABLE employee (

emp_id INT PRIMARY KEY,

e_name VARCHAR (50),

Salary INT,

Date_of_joining DATE,

Dept_no INT,

Designation VARCHAR (20)

);

INSERT INTO employee VALUES

(1,"A",250000,'2019-03-23',203,"Developer"),

(2,"B",250000,'2019-01-20',203,"Developer"),

(3,"C",270000,'2015-09-02',201,"Management"),

(4,"D",200000,'2020-08-24',202,"Testing"),

(5,"E",260000,'2015-12-24',203,"Developer"),

(6,"F",230000,'2018-03-11',203,"Developer"),

(7,"G",180000,'2023-02-04',202,"Testing"),

(8,"H",150000,'2021-10-21',202,"Testing"),

(9,"I",220000,'2021-09-27',201,"Management"),

(10,"J",190000,'2022-05-17',201,"Management");

SELECT * FROM employee;


CREATE VIEW emp_v1 AS

SELECT emp_id, e_name, Dept_no

FROM employee;

SELECT * FROM emp_v1;

UPDATE emp_v1

SET Dept_no = 206

WHERE emp_id=9;

SELECT * FROM emp_v1;

SHOW INDEX FROM employee;

CREATE INDEX index2

ON employee (e_name);

SHOW INDEX FROM employee;

SELECT * FROM employee;

----------------------------------------------------------------------------------------
2) Consider the relation employee (emp_id, e_name, salary, Date_of_joining, Dept_no, Designation) perform basic
SQL operations.

1. Display employees whose name contains letter ‘e’.


2. Display different types of designation
3. Display name and salary of employee whose location is Mumbai
4. Display name and department of employee working in Manager or Marketing department
5. Display the department name whose employees are more than one
6. Rename employee table as emp1
7. Add a new column city in the employee table.

Answer :

CREATE DATABASE XYZ;

USE XYZ;
CREATE TABLE employee(

emp_id INT PRIMARY KEY,

e_name VARCHAR(50),

Salary INT,

Date_of_joining DATE,

Dept_no INT,

Designation VARCHAR(20)

);

INSERT INTO employee VALUES

(1,"A",250000,'2019-03-23',203,"Developer"),

(2,"B",250000,'2019-01-20',203,"Developer"),

(3,"C",270000,'2015-09-02',201,"Management"),

(4,"D",200000,'2020-08-24',202,"Testing"),

(5,"E",260000,'2015-12-24',203,"Developer"),

(6,"F",230000,'2018-03-11',203,"Developer"),

(7,"G",380000,'2023-02-04',200,"Counceller"),

(8,"H",150000,'2021-10-21',202,"Testing"),

(9,"I",220000,'2021-09-27',201,"Marketing"),

(10,"J",190000,'2022-05-17',201,"Management");

SELECT * FROM employee;

ALTER TABLE employee

ADD city VARCHAR(50);

SELECT * FROM employee;

SET SQL_SAFE_UPDATES = 0;

UPDATE employee

SET city="Mumbai";

SELECT * FROM employee;


UPDATE employee

SET city="Pune"

WHERE Designation="Counceller";

SELECT * FROM employee;

UPDATE employee

SET city="Banglore"

WHERE Designation="Testing";

SELECT * FROM employee;

SELECT * FROM employee

WHERE e_name LIKE "%e%";

SELECT Designation

FROM employee

GROUP BY Designation;

SELECT e_name, Salary

FROM employee

WHERE City="Mumbai";

SELECT e_name, Dept_no

FROM employee

WHERE Designation = 'Management' OR Designation = 'Marketing';

SELECT Designation

FROM employee

GROUP BY Designation
HAVING COUNT(emp_id)>1;

ALTER TABLE employee

RENAME TO emp1;

SELECT * FROM emp1;

----------------------------------------------------------------------------------------
3)Consider the relation employee (emp_id, e_name, salary, Date of Joining, Dept_no, Designation) perform basic
SQL operations.

1. Find department in which maximum employees work.


2. Display name, designation and department no of employees whose name starts with either ‘A’ or ‘P’.
3. Display max. salary from department 2 and min. salary from department 4.
4. Display employee data where salary is less than average salary from department 3.
5. Display employees who were hired earliest or latest.
6. Display name and department no of employees who are manager, market analysts. Use prediactes
7. List employees hired in August.
8. List employees who are hired after 31/12/2006.
9. Find average annual salary per department.

Answer:

CREATE DATABASE XYZ;

USE XYZ;

CREATE TABLE employee(

emp_id INT PRIMARY KEY,

e_name VARCHAR(50),

Salary INT,

Date_of_joining DATE,

Dept_no INT,

Designation VARCHAR(20)

);

INSERT INTO employee VALUES

(1,"A",250000,'2009-03-23',203,"Developer"),

(2,"B",250000,'2009-01-20',203,"Developer"),

(3,"C",270000,'2005-09-02',201,"Management"),

(4,"D",200000,'2000-08-24',202,"Testing"),

(5,"E",260000,'2005-12-24',203,"Developer"),
(6,"F",230000,'2008-03-11',203,"Developer"),

(7,"G",380000,'2003-02-04',200,"Counceller"),

(8,"H",150000,'2001-10-21',202,"Testing"),

(9,"I",220000,'2011-09-27',204,"Marketing"),

(10,"J",190000,'2012-05-17',201,"Management");

SELECT * FROM employee;

SELECT Dept_no, COUNT(emp_id) AS emp_count

FROM employee

GROUP BY Dept_no

ORDER BY emp_count DESC

LIMIT 1;

SELECT e_name, Designation, Dept_no

FROM employee

WHERE e_name LIKE "A%" OR e_name LIKE "K%";

SELECT

MAX(CASE WHEN Dept_no=202 THEN salary END) AS max_dept_202_salary,

MIN(CASE WHEN Dept_no=203 THEN salary END) AS min_dept_203_salary

FROM employee;

SELECT *

FROM employee

WHERE salary<(

SELECT AVG(salary)

FROM employee

WHERE Dept_no=203

);

SELECT *
FROM employee

WHERE Date_of_joining = (SELECT MIN(Date_of_joining) FROM employee)

OR Date_of_joining = (SELECT MAX(Date_of_joining) FROM employee);

SELECT e_name, Dept_no

FROM employee

WHERE Designation IN ("Management","Marketing");

SELECT *

FROM employee

WHERE MONTH(Date_of_joining)=8;

SELECT *

FROM employee

WHERE Date_of_joining > '2006-12-31';

SELECT Dept_no, AVG(salary) as avg_salary

FROM employee

GROUP BY Dept_no;

----------------------------------------------------------------------------------------
4)Consider two tables Customer(c_id, c_name , email , city , pincode)Order(order_id , date , amount , cust_id.

1. Create both the tables with primary key and foreign key constraints.
2. insert 10 records each.
3. Find all orders placed by customers with cust_id 2
4. Find list of customers who placed their order and details of order
5. List of customers who haven’t placed order
6. List all orders and append to customer table
7. Display all records
8. Display customer that are from same city 8

Answer:

CREATE DATABASE XYZ;

USE XYZ;

CREATE TABLE customer(

c_id INT PRIMARY KEY,


c_name VARCHAR(50),

email VARCHAR(50),

city VARCHAR(20),

pincode INT

);

CREATE TABLE orders(

order_id INT PRIMARY KEY,

order_date DATE,

amount INT,

cust_id INT,

FOREIGN KEY(cust_id) REFERENCES customer(c_id)

);

INSERT INTO customer VALUES

(1, 'A', 'A@example.com', 'Mumbai', '10001'),

(2, 'B', 'B@example.com', 'Mumbai', '10001'),

(3, 'C', 'C@example.com', 'Pune', '60007'),

(4, 'D', 'D@example.com', 'Pune', '60007'),

(5, 'E', 'E@example.com', 'Pune', '60006'),

(6, 'F', 'F@example.com', 'Satara', '40001'),

(7, 'G', 'G@example.com', 'Mumbai', '10001'),

(8, 'H', 'H@example.com', 'Satara', '40001'),

(9, 'I', 'I@example.com', 'Kolhapur', '30001'),

(10, 'J', 'J@example.com', 'Mumbai', '10001');

SELECT * FROM customer;

INSERT INTO orders VALUES

(101, '2014-03-21', 10000,1),

(102, '2012-05-04', 15000,2),

(103, '2012-10-19', 13500,3),

(104, '2015-03-11', 17500,4),


(105, '2016-12-18', 19000,2),

(106, '2017-02-22', 18500,7),

(107, '2015-11-15', 13000,7),

(108, '2016-08-28', 15000,1),

(109, '2011-10-14', 13500,9),

(110, '2018-07-23', 21000,10);

SELECT * FROM orders;

SELECT * FROM orders

WHERE cust_id=2;

SELECT *

FROM customer

INNER JOIN orders ON customer.c_id=orders.cust_id;

SELECT *

FROM customer

LEFT JOIN orders ON customer.c_id=orders.cust_id

WHERE orders.order_id IS NULL;

SELECT *

FROM customer

LEFT JOIN orders ON customer.c_id=orders.cust_id;

SELECT c1.c_name AS customer1_name,

c2.c_name AS customer2_name,

c1.city

FROM customer c1

JOIN customer c2 ON c1.city=c2.city AND c1.c_id<c2.c_id;


----------------------------------------------------------------------------------------

5) Consider tables Borrower (RollNo, Name, DateofIssue, NameofBook, Status) and


Fine (Roll_no,Date,Amt). Status is either Issued or Returned.
1. Create both the tables with primary key.
2. Insert 10 records each.
3. Find count of books with Issued status.
4. Display all records.
5. Display RollNo whose date of issue is same.
Answer:
CREATE DATABASE XYZ;
USE XYZ;

CREATE TABLE borrower(


rollno INT PRIMARY KEY,
name VARCHAR(50),
DateofIssue DATE,
NameofBook VARCHAR(50),
status ENUM ("Issued","Returned")
);

CREATE TABLE fine(


roll_no INT,
Date DATE,
Amt INT,
PRIMARY KEY(roll_no, Date),
FOREIGN KEY (roll_no) REFERENCES borrower(rollno)
);

INSERT INTO borrower VALUES


(1,"A",'2024-03-11',"Book1","Issued"),
(2,"B",'2024-04-01',"Book2","Issued"),
(3,"C",'2024-02-27',"Book3","Returned"),
(4,"D",'2024-03-11',"Book4","Issued"),
(5,"E",'2024-05-17',"Book5","Issued"),
(6,"F",'2024-02-29',"Book3","Issued"),
(7,"G",'2024-05-16',"Book6","Returned"),
(8,"H",'2024-08-03',"Book7","Issued"),
(9,"I",'2024-06-26',"Book8","Returned"),
(10,"J",'2024-10-29',"Book9","Issued");

INSERT INTO fine VALUES


(1, '2024-03-25', 50),
(2, '2024-04-24', 60),
(5, '2024-05-29', 25),
(6, '2024-03-21', 40),
(7, '2024-05-29', 35),
(8, '2024-08-23', 45),
(10, '2024-10-24', 65);

SELECT COUNT(*) AS Issued_count


FROM borrower
WHERE status="Issued";

SELECT *
FROM borrower
LEFT JOIN fine ON borrower.rollno=fine.roll_no;

SELECT b1.rollno, b1.name, b1.DateofIssue


FROM borrower b1
JOIN borrower b2 ON b1.DateofIssue=b2.DateofIssue AND b1.rollno <> b2.rollno
ORDER BY b1.DateofIssue;

----------------------------------------------------------------------------------------
6) Consider student (roll_no, name, marks, class) table. Column roll_no is primary key. Perform any 3 DLL and any
3 DML operations on the table.

Answer:

CREATE DATABASE XYZ;

USE XYZ;

CREATE TABLE student(

roll_no INT PRIMARY KEY,

name VARCHAR(50),

marks INT,

class VARCHAR(10)

);

ALTER TABLE student

ADD COLUMN age INT;

INSERT INTO student VALUES

(1,"A",11,"10",16),

(2,"B",17,"10",16),

(3,"C",14,"10",16);
ALTER TABLE student

MODIFY COLUMN class INT;

UPDATE student

SET marks=15

WHERE roll_no=3;

DELETE FROM student

WHERE marks<15;

ALTER TABLE student

RENAME TO stud_info;

SELECT * FROM stud_info;

----------------------------------------------------------------------------------------
7) Write a SQL statement to create a table job_history including columns employee_id, start_date, end_date, job_id
and department_id and make sure that, the employee_id column does not contain any duplicate value at the time of
insertion and the foreign key column job_id contain only those values which are exists in the jobs table . Consider
table Job (job_id,job_title.min_sal,max_sal)

Answer:

CREATE TABLE job_history (

employee_id INT PRIMARY KEY,

start_date DATE NOT NULL,

end_date DATE,

job_id INT NOT NULL,

department_id INT,

UNIQUE (employee_id),

FOREIGN KEY (job_id) REFERENCES jobs(job_id)

);

----------------------------------------------------------------------------------------

8) For the given relation schema: employee(employee-name, street, city)


works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Give an expression in SQL for each of the following queries:
a) Find the names, street address, and cities of residence for all employees who work for same company and earn
more than $10,000.
b) Find the names of all employees in the database who live in the same cities as the companies for which they
work.
c) Find the names of all employees who earn more than the average salary of all employees of their company.
Assume that all people work for at most one company.
Answer :

CREATE DATABASE XYZ;

USE XYZ;

CREATE TABLE employee (

employee_name VARCHAR(50) PRIMARY KEY,

street VARCHAR(100),

city VARCHAR(50)

);

CREATE TABLE works (

employee_name VARCHAR(50),

company_name VARCHAR(50),

salary INT,

FOREIGN KEY (employee_name) REFERENCES employee(employee_name)

);

CREATE TABLE company (

company_name VARCHAR(50) PRIMARY KEY,

city VARCHAR(50)

);

CREATE TABLE manages (


employee_name VARCHAR(50),

manager_name VARCHAR(50),

FOREIGN KEY (employee_name) REFERENCES employee(employee_name),

FOREIGN KEY (manager_name) REFERENCES employee(employee_name)

);

INSERT INTO employee (employee_name, street, city) VALUES

('Alice Johnson', '123 Maple St', 'New York'),

('Bob Smith', '456 Oak Ave', 'Los Angeles'),

('Carol Williams', '789 Pine Blvd', 'New York'),

('David Brown', '321 Elm St', 'Chicago'),

('Eve Davis', '654 Cedar Rd', 'Los Angeles');

INSERT INTO company (company_name, city) VALUES

('Tech Innovations', 'New York'),

('Creative Solutions', 'Los Angeles'),

('Future Enterprises', 'Chicago');

INSERT INTO works (employee_name, company_name, salary) VALUES

('Alice Johnson', 'Tech Innovations', 12000),

('Bob Smith', 'Creative Solutions', 9500),

('Carol Williams', 'Tech Innovations', 15000),

('David Brown', 'Future Enterprises', 11000),

('Eve Davis', 'Creative Solutions', 8000);

INSERT INTO manages (employee_name, manager_name) VALUES

('Bob Smith', 'Alice Johnson'),

('Carol Williams', 'Alice Johnson'),


('David Brown', 'Eve Davis');

SELECT e.employee_name, e.street, e.city

FROM employee e

JOIN works w ON e.employee_name = w.employee_name

WHERE w.salary > 10000;

SELECT e.employee_name

FROM employee e

JOIN works w ON e.employee_name = w.employee_name

JOIN company c ON w.company_name = c.company_name

WHERE e.city = c.city;

SELECT w.employee_name

FROM works w

JOIN (

SELECT company_name, AVG(salary) AS avg_salary

FROM works

GROUP BY company_name

) AS avg_salaries ON w.company_name = avg_salaries.company_name

WHERE w.salary > avg_salaries.avg_salary;

----------------------------------------------------------------------------------------

9) For the given relation schema: employee(employee-name, street, city)


works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Give an expression in SQL for each of the following queries:
a) Find the name of the company that has the smallest payroll.
b) Find the names of all employees in the database who live in the same cities and on the same streets as do
their managers.

10) Implement CRUD operations. SAVE method. Use following Collection. Perform Map Reduce to count quantity
of each item.
Item: Item ID, Item quantity, price, brand.

11) Implement CRUD operations. SAVE method. Use following Collection.


Item: Item ID, Item quantity, price, brand.

12) Implement CRUD operations. SAVE method. Use following Collection.


Item: Item ID, Item quantity, price, brand, discount
1. Display the count of item brand wise.
2. Dsiplay item with minimum price.
3. Display maximum discount given for item.

13) Implement Map reduces operation for counting the marks of students.

Use: student (roll_no, name marks, class)

Expected output: student name or roll no and total marks.

14) Implement Map reduces operation for displaying persons with same profession.

Use: person (person_id, name, addr, profession)

15) Perform CRUD operation in mongo db –

Use : person( person_id, name, addr, profession )

1.Create Collection.

2.Inserting data in collection.

3.Reading data of collection.

4.Updating data of collection.

5.Deleting data from collection.

16) Perform CRUD operation and Aggregation in mongo db


employee(emp_id,e_name,salary ,Date of Joining,Dapt_no,Designation)

1. Display the count of employee department wise.

2. Dsiplay the average salary of employee in sales department.

3. Dsiplay minimum salary to employees joins in June 2016

4. Display maximum salary given to employee in production department.

5. Display record of first and last employee department wise.

17) Consider student ( roll_no, name ,marks, class) table. Perform add update and delete operation on same table
through java program. Write menu driven program.

18) Implement Stored Procedure namely proc_Grade for the categorization of student. If marks scored by students in
examination is <=1500 and marks>=990 then student will be placed in distinction category if marks scored are
between 989 and900 category is first class, if marks 899 and 825 category is Higher Second Class. Write a PL/SQL
block for using procedure created with above requirement. Stud_Marks(name, total_marks) Result(Roll,Name,
Class).

19) Write a database trigger on customer( cust_id, c_name, addr) table. The System should keep track of the records
that are being updated or deleted. The old value of updated or deleted records should be added in cust_Audit table.

20) Implement a database trigger on client_master( c_id, c_name, acc_no) table. The System should keep track of
the records that are being updated or inserted. The old value of updated or deleted records should be added in
client_Audit table.

21) Implement a PL/SQL block of code using explicit Cursor, that will merge the data available in the newly created
table N_RollCall with the data available in the table O_RollCall. If the data in the first table already exist in the
second table then that data should be skipped.

22) Write a PL/SQL block of code for the following requirements:- Schema: Borrower(Rollin, Name, DateofIssue,
NameofBook, Status) 2. Fine(Roll_no,Date,Amt)  Accept roll_no & name of book from user.  Check the number
of days (from date of issue), if days are between 15 to 30 then fine amount will be Rs 5per day. If condition of fine
is true, then details will be stored into fine table.

23) Implement Basic SQL queries.

1. Create table employee.

2. Insert 10 records in table.

3. Create a view emp_vl of table employee which has emp_id , name and dept-attributes.
4. Display name and department of employee working in Manager or Marketing department
5. Display employees who were hired earliest or latest.

6. Display name and department no of employees who are manager, market analysts. Use

Predicates

List employees hired in August.

List employees who are hired after 31/12/2006.

24) ) Indexing and join: Consider the relation

employee (emp_id,e_name,salary ,Date of Joining,Dapt_no,Designation)

Customer(c_id, c_name , email , city , pincode)Order(order_id , date , amount , cust_id.

a. create empid as primary key and indices on table employee.

b. create user defined index on any column

c. create sequence using auo-increment.

d. truncate table.

e. find list of customers who placed order and details of their orders.

f. find info of customers and append order details to the table/

g. list down customers who haven’t placed order.

25) Implement aggregation and indexing with suitable example in mongodb.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy