DBMS Programs
DBMS Programs
DBMS Programs
--Select all account numbers that are not in 2000 and 5000.
SELECT ACC_NO FROM BOOK WHERE ACC_NO NOT IN (2000, 5000);
--FIND TOTAL NUMBER OF ORDER FOR EACH PRODUCT & TOTAL SHOULD NOT BE MORE THAN 2.
SELECT P_NAME, COUNT(*) AS TOTAL_ORDERS
FROM PRODUCT
JOIN ORDER ON PRODUCT.P_NO = ORDER.P_NO
GROUP BY P_NAME
HAVING COUNT(*) <= 2;
--RETRIVE THE DETAILS OF THE EMPLOYEE IN ASCENDING ORDER WHOSE DEPARMENT BELONGS TO
"SALES‟.
SELECT EMP.EMP_NAME, EMP.SALARY, DEPT.DEPT_NAME
FROM EMP
INNER JOIN DEPT ON EMP.EMP_ID = DEPT.EMP_ID
WHERE DEPT.DEPT_NAME = 'SALES'
ORDER BY EMP.EMP_NAME ASC;
--FIND THE TOTAL NUMBER OF EMPLOYEES‟ SALARY WHO WORKS ON ACCOUNTING DEPARMENT.
SELECT SUM(EMP.SALARY) AS TOTAL_SALARY
FROM EMP
INNER JOIN DEPT ON EMP.EMP_ID = DEPT.EMP_ID
WHERE DEPT.DEPT_NAME = 'ACCOUNTING';
--FIND THE DEPARTMENT NAME AND TOTAL SALARIES OF EACH EMPLOYEE WHO IS WORKING ON THE
SPECIFIC DEPARTMENT.
SELECT DEPT.DEPT_NAME, EMP.EMP_NAME, EMP.SALARY
FROM EMP
INNER JOIN DEPT ON EMP.EMP_ID = DEPT.EMP_ID
WHERE DEPT.DEPT_NAME = 'SPECIFIC_DEPARTMENT'
ORDER BY DEPT.DEPT_NAME, EMP.EMP_NAME;
-- FIND PRODUCTS WHOSE SELLING PRICE IS GREATER THAN 2000 AND LESS THAN 4000
SELECT *
FROM PRODUCT_MASTER
WHERE SELL_PRICE > 2000 AND SELL_PRICE < 4000;
Q.1) CREATE A TABLE STUDENT
-- DISPLAY NAME OF ALL THE PRODUCTS WHOSE QTY IS GREATER THAN 30 AND RATE IS LESS THAN
800
SELECT P_NAME
FROM PRODUCT
WHERE P_QTY > 30 AND P_RATE < 800;
-- DISPLAY ALL ACCOUNT NUMBERS THAT ARE NOT IN 2000 AND 4000
SELECT ACC_NO
FROM BOOK
WHERE ACC_NO NOT IN (2000, 4000);
-- DISPLAY NAME OF ALL BOOKS WHICH STARTS FROM WITH LETTER "E"
SELECT BOOK_NAME
FROM BOOK
WHERE BOOK_NAME LIKE 'E%';
-- List the names and address of all doctors who work for K.K. Hospital
SELECT d_name, d_address FROM DOCTOR WHERE h_id = 1;
-- Display actor name & salary where salary is in range of 60,000 & 90,000
SELECT name, salary
FROM ACTOR
WHERE salary BETWEEN 60000 AND 90000;
-- Find products whose selling price is greater than 2000 and less than 4000
SELECT *
FROM PRODUCT_MASTER
WHERE sell_price BETWEEN 2000 AND 4000;
Q1) CREATE A TABLE STUDENT
-- Find products whose selling price is greater than 2000 and less than 4000
SELECT *
FROM PRODUCT_MASTER
WHERE sell_price BETWEEN 2000 AND 4000;
Q1) CREATE THE FOLLOWING TABLES. HOSPITAL AND DOCTOR TABLE
-- List the names and address of all doctors who work for K.K. Hospital
SELECT D_NAME, D_ADDRESS
FROM DOCTOR
WHERE H_ID = 1;
--Display ACTOR NAME & SALARY where SALARY is in range of 60,000 & 90,000
SELECT NAME, SALARY
FROM ACTOR
WHERE SALARY BETWEEN 60000 AND 90000;
--To find the total number of orders for each product, we can use the following query:
SELECT P_NAME, COUNT(*) AS TOTAL_ORDERS
FROM PRODUCT
JOIN ORDER_TABLE ON PRODUCT.P_NO = ORDER_TABLE.P_NO
GROUP BY P_NAME;
--To find the total number of orders for each product where the total is not more than 2,
we can
--modify the previous query as follows:
SELECT P_NAME, COUNT(*) AS TOTAL_ORDERS
FROM PRODUCT
JOIN ORDER_TABLE ON PRODUCT.P_NO = ORDER_TABLE.P_NO
GROUP BY P_NAME
HAVING COUNT(*) <= 2;
--To find the product name which is ordered today, we can use the following query:
SELECT P_NAME
FROM PRODUCT
JOIN ORDER_TABLE ON PRODUCT.P_NO = ORDER_TABLE.P_NO
WHERE O_DATE = DATE('now');
--To find the product name which is ordered maximum number of times, we can use the
following query:
SELECT P_NAME
FROM PRODUCT
JOIN ORDER_TABLE ON PRODUCT.P_NO = ORDER_TABLE.P_NO
GROUP BY P_NAME
ORDER BY COUNT(*) DESC
LIMIT 1;
--To create a view ORDER_VIEW and insert, update the view, we can use the following SQL
statements:
CREATE VIEW ORDER_VIEW AS
SELECT O_NO, P_NAME, QTY_ORDERED, O_DATE
FROM PRODUCT
JOIN ORDER_TABLE ON PRODUCT.P_NO = ORDER_TABLE.P_NO;
UPDATE ORDER_VIEW
SET QTY_ORDERED = 20
WHERE O_NO = 1;
Q1) CREATE A TABLE CLIENT_MASTER AND SALESMAN_MASTER