DBMS Practicle
DBMS Practicle
1323215
+-------------------+
| PRACTICAL 1 |
+-------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
● For product_master
I)client_master
+----------------------------------------+
DBMS-306
1323215
| CREATE TABLE client_master ( |
| client_no VARCHAR(6), |
| name VARCHAR(20), |
| address1 VARCHAR(30), |
| address2 VARCHAR(30), |
| city VARCHAR(15), |
| state VARCHAR(15), |
| pincode INT(6), |
| bal_due FLOAT(10,2) ); |
| |
+----------------------------------------+
ii) product_master
+----------------------------------------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+----------------------------------------+
ii) product_master
+--------------------------------------------------------------------------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+--------------------------------------------------------------------------+
DBMS-306
1323215
1.C) On the basis of above two tables answer the following Questionnaires:
QUERY FOR --> 1.C
i. Find out the names of all the clients.
ii. Retrieve the list of names and cities of all the clients.
iii. List the various products available from the product master table.
vi. Find the products with description as '1.44 drive' and '1.22 Drive'.
vii. Find all the products whose sell price is greater then 5000.
viii. Find the list of all clients who stay in city 'Bombay' or city 'Delhi' or 'Madras'.
ix. Find the product whose selling price is greater than 2000 and less than or equal to 5000.
DBMS-306
1323215
x. List the name, city and state of clients not in the state of 'Maharashtra'.
DBMS-306
1323215
+-------------------+
| PRACTICAL 2 |
+-------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Using the table client master and product master answer the following Questionnaires.
II. Delete the record with client 0001 from the client master table.
V. Find the products whose selling price is more than 1500 and also find the new selling price as original
selling price *15.
VI. Find out the clients who stay in a city whose second letter is a.
DBMS-306
1323215
VII. Find out the name of all clients having 'a' as the second letter in their names.
XI. Count the number of products having price greater than or equal to 1500.
DBMS-306
1323215
+-------------------+
| PRACTICAL 3 |
+-------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
------------------------------------------------------------------------------
SOLUTION
+-------------+
DBMS-306
1323215
Insert the following data into the table:
------------------------------------------------------------------------------
SOLUTION
+-------------+
SOLUTION
+-------------+
DBMS-306
1323215
------------------------------------------------------------------------------
SOLUTION
+-------------+
DBMS-306
1323215
------------------------------------------------------------------------------
SOLUTION
+-------------+
------------------------------------------------------------------------------
SOLUTION
+-------------+
iii. List the order number and day on which clients placed their order
iv. List the month (in alphabets) and date when the orders must be delivered.
DBMS-306
1323215
vii. Print the description and total qty sold for each product.
DBMS-306
1323215
+-------------------+
| PRACTICAL 4 |
+-------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TABLE: Challan_Header
+---------------------------------------------
SOLUTION
+-------------+
TABLE: Challan_Details
DBMS-306
1323215
-
SOLUTION
+-------------+
2. Add the not null constraint in the product_master table with the
columns description, profit percent , sell price and cost price
+-------------------+
| PRACTICAL 5 |
+-------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1. find out the product which has been sold to 'ivan bayroos'
2. find out the product and their quantities that will have to delivered in the current
month.
4. find the names of the clients who have purchased 'CD Drive'.
SELECT c.client_name
FROM client_master c
JOIN orders o ON c.client_no = o.client_no
JOIN product_master p ON o.product_no = p.product_no
WHERE p.description = 'CD Drive';
5. List the product_no and s_order_no of customers having qty_ordered less than 5
from the order detail Table for the product '1.44 Floppies'.
6. Find the products and their quantities for the orders placed by 'Vandana Saitwal'
and 'Ivan Bayross'.
+-------------------+
| PRACTICAL 6 |
+-------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SELECT *
FROM sales
ORDER BY sales_amount DESC;
+-------------------+
| PRACTICAL 7 |
+-------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SELECT customer_id
FROM orders
UNION
SELECT customer_id
FROM customers;
SELECT customer_id
FROM orders
INTERSECT
SELECT customer_id
FROM customers;
SELECT customer_id
FROM customers
MINUS
SELECT customer_id
FROM orders;
+-------------------+
| PRACTICAL 8 |
+-------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1. Create an Index
Create an index on the category_id column of the products table to optimize queries that
frequently filter products by category.
2. Drop an Index
Drop the previously created index idx_category_id.
3. Create a View
Create a view named expensive_products that shows the product_name, price, and stock_quantity
of products with a price greater than ₹25,000.
SELECT *
FROM expensive_products;
5. Drop a View
Drop the view expensive_products.
+-------------------+
| PRACTICAL 9 |
+-------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PL/SQL Functions
+--------------------+
| PRACTICAL 10 |
+--------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Transaction Management and Security
BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance - 2000
WHERE account_id = 1; -- Deduct from Alice
BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance - 1000
WHERE account_id = 3; -- Deduct from Charlie
UPDATE accounts
SET balance = balance + 1000
WHERE account_id = 4; -- Add to Diana
COMMIT;
4. Grant Privileges
Grant and revoke privileges to control user access to the accounts table.
Grant Privileges:
Revoke Privileges:
+--------------------+
| PRACTICAL 11 |
+--------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Complex queries
1. Multi-table Join
Write a query to display the employee name, department name, and project name for employees
working in departments assigned to projects.
SELECT
e.name AS employee_name,
d.department_name,
p.project_name
FROM
employees e
JOIN departments d ON e.department_id = d.department_id
JOIN projects p ON d.department_id = p.department_id;
DBMS-306
1323215
2. Nested Subquery
Find the name and salary of employees who earn more than the average salary of their
department.
3. Using EXISTS
Retrieve the names of employees who are managers (i.e., their employee_id is referenced in the
manager_id column).
SELECT name
FROM employees e1
WHERE EXISTS (
SELECT 1
FROM employees e2
WHERE e2.manager_id = e1.employee_id
);
DBMS-306
1323215
4. Using CASE
Write a query to display the employee name and a performance rating based on their salary:
● High: Salary > ₹70,000
● Medium: ₹50,000 ≤ Salary ≤ ₹70,000
● Low: Salary < ₹50,000
SELECT
name,
CASE
WHEN salary > 70000 THEN 'High'
WHEN salary BETWEEN 50000 AND 70000 THEN 'Medium'
ELSE 'Low'
END AS performance_rating
FROM employees;
5. Complex Filtering
Retrieve the names of employees working in departments with more than one project.
SELECT DISTINCT e.name
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.department_id IN (
SELECT department_id
FROM projects
GROUP BY department_id
HAVING COUNT(project_id) > 1
);