DBMS Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Q1) CREATE TABLE BOOK AND INSERT RECORDS TO SOLVE QUERY.

-- Create the BOOK table


CREATE TABLE BOOK (
ACC_NO INT PRIMARY KEY,
BOOK_NAME VARCHAR(20) NOT NULL,
AUTHOR_NAME VARCHAR(20) UNIQUE,
ISSUE_DATE DATETIME,
SUBJECT CHAR(20) NOT NULL,
NO_OF_PAGES INT
);

-- Insert records into the BOOK table


INSERT INTO BOOK (ACC_NO, BOOK_NAME, AUTHOR_NAME, ISSUE_DATE, SUBJECT, NO_OF_PAGES)
VALUES
(1001, 'The Catcher in the Rye', 'J.D. Salinger', '2023-04-01', 'Fiction', 224),
(2002, 'To Kill a Mockingbird', 'Harper Lee', '2023-04-02', 'Fiction', 281),
(3003, 'The Great Gatsby', 'F. Scott Fitzgerald', '2023-04-03', 'Fiction', 180),
(4004, 'Sapiens: A Brief History of Humankind', 'Yuval Noah Harari', '2023-04-04', 'Non-
_fiction', 464),
(5005, 'The Selfish Gene', 'Richard Dawkins', '2023-04-05', 'Non-fiction', 360),
(6006, 'The Structure of Scientific Revolutions', 'Thomas S. Kuhn', '2023-04-06', 'Non-
_fiction', 212),
(7007, 'The Origin of Species', 'Charles Darwin', '2023-04-07', 'Non-fiction', 502),
(8008, 'The Social Contract', 'Jean-Jacques Rousseau', '2023-04-08', 'Philosophy', 208),
(9009, 'The Republic', 'Plato', '2023-04-09', 'Philosophy', 416),
(10010, 'The Prince', 'Niccolò Machiavelli', '2023-04-10', 'Philosophy', 140);

SELECT * FROM BOOK

--Select all account numbers that are not in 2000 and 5000.
SELECT ACC_NO FROM BOOK WHERE ACC_NO NOT IN (2000, 5000);

--Display name of all books which starts with letter “s”.


SELECT BOOK_NAME FROM BOOK WHERE BOOK_NAME LIKE 'S%';

--Add one more field as “no_of_pages” to the table.


ALTER TABLE BOOK ADD NO_OF_PAGES INT;

--Update “no_of_pages” column to 200 for all records.


UPDATE BOOK SET NO_OF_PAGES = 200;

--Destroy the table.


DROP TABLE BOOK;
Q1. CREATE TABLES AS PRODUCT AND ORDER

-- CREATE THE PRODUCT TABLE


CREATE TABLE PRODUCT (
P_NO VARCHAR(20) PRIMARY KEY,
P_NAME VARCHAR(30),
RATE INT
);

-- CREATE THE ORDER TABLE


CREATE TABLE ORDER(S
O_NO INT PRIMARY KEY,
P_NO VARCHAR(20) FOREIGN KEY REFERENCES PRODUCT(P_NO),
QTY_ORDERED INT,
O_DATE DATETIME
);

--FIND TOTAL NUMBER OF ORDER FOR EACH PRODUCT.


SELECT P_NAME, COUNT(*) AS TOTAL_ORDERS
FROM PRODUCT
JOIN ORDER ON PRODUCT.P_NO = ORDER.P_NO
GROUP BY P_NAME;

--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;

--FIND THE PRODUCT NAME WHICH IS ORDERED TODAY.


SELECT P_NAME
FROM PRODUCT
JOIN ORDER ON PRODUCT.P_NO = ORDER.P_NO
WHERE O_DATE = CURDATE();

--FIND THE PRODUCT NAME WHICH IS ORDERED MAXIMUM NUMBER OF TIMES.


SELECT P_NAME, COUNT(*) AS TOTAL_ORDERS
FROM PRODUCT
JOIN ORDER ON PRODUCT.P_NO = ORDER.P_NO
GROUP BY P_NAME
ORDER BY TOTAL_ORDERS DESC
LIMIT 1;

--Create view order_view and insert , update view.


-- Create the view
CREATE VIEW ORDER_VIEW AS SELECT O_NO, P_NAME, QTY_ORDERED, O_DATE
FROM PRODUCT
JOIN ORDER ON PRODUCT.P_NO = ORDER.P_NO;
-- Insert into the view
INSERT INTO ORDER_VIEW (O_NO, P_NAME, QTY_ORDERED, O_DATE) VALUES
(1, 'Product A', 10, '2023-04-11');
-- Update the view
UPDATE ORDER_VIEW
SET QTY_ORDERED = 20 WHERE O_NO = 1;
Q1) CREATE TABLES AS EMPLOYEE AND DEPARTMENT

-- CREATE THE EMPLOY TABLE


CREATE TABLE EMPLOY (
EMP_ID INT PRIMARY KEY,
EMP_NAME VARCHAR(50) NOT NULL,
SALARY DECIMAL(10, 2) NOT NULL
);

-- CREATE THE DEPT TABLE


CREATE TABLE DEPT (
DEPT_ID INT PRIMARY KEY,
DEPT_NAME VARCHAR(50) NOT NULL,
EMP_ID INT NOT NULL,
EMP_ID REFERENCES EMP (EMP_ID)
);

--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 WHICH IS MADE TODAY.


SELECT DEPT_NAME
FROM DEPT
WHERE CREATION_DATE = CURRENT_DATE;

--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;

--CREATE VIEW EMPLOYEE_VIEW AND INSERT , UPDATE VIEW.


-- CREATE THE VIEW
CREATE VIEW EMPLOYEE_VIEW AS
SELECT EMP.EMP_ID, EMP.EMP_NAME, DEPT.DEPT_NAME, EMP.SALARY
FROM EMP INNER JOIN DEPT ON EMP.EMP_ID = DEPT.EMP_ID;

-- INSERT DATA INTO THE VIEW


INSERT INTO EMPLOYEE_VIEW (EMP_ID, EMP_NAME, DEPT_NAME, SALARY)
VALUES (1001, 'JOHN DOE', 'SALES', 50000.00);
-- UPDATE DATA IN THE VIEW
UPDATE EMPLOYEE_VIEW
SET SALARY = 55000.00 WHERE EMP_ID = 1001;
Q1. CREATE THE TABLES AS STUDENT_MASTER AND TEACHER_MASTER

-- CREATE THE STUDENT_MASTER TABLE


CREATE TABLE STUDENT_MASTER (
STUDENT_ID INT PRIMARY KEY,
STUDENT_NAME VARCHAR(50) NOT NULL,
ADDRESS VARCHAR(100) NOT NULL
);

-- CREATE THE TEACHER_MASTER TABLE


CREATE TABLE TEACHER_MASTER (
TEACHER_ID INT PRIMARY KEY,
TEACHER_NAME VARCHAR(50) NOT NULL,
EMAIL VARCHAR(50) NOT NULL
);

-- INSERT DATA INTO THE STUDENT_MASTER TABLE


INSERT INTO STUDENT_MASTER (STUDENT_ID, STUDENT_NAME, ADDRESS) VALUES
(1, 'JOHN DOE', '123 MAIN ST'),
(2, 'JANE SMITH', '456 OAK AVE'),
(3, 'BOB JOHNSON', '789 ELM ST');

-- INSERT DATA INTO THE TEACHER_MASTER TABLE


INSERT INTO TEACHER_MASTER (TEACHER_ID, TEACHER_NAME, EMAIL) VALUES
(1, 'MR. BROWN', '[email protected]'),
(2, 'MS. GREEN', '[email protected]'),
(3, 'MRS. WHITE', '[email protected]');

-- DISPLAY ALL RECORDS FOR STUDENT_MASTER AND TEACHER_MASTER


SELECT * FROM STUDENT_MASTER;
SELECT * FROM TEACHER_MASTER;

-- CONCATENATE THE NAME AND ADDRESS OF ALL THE STUDENTS


SELECT CONCAT(STUDENT_NAME, ' - ', ADDRESS) AS STUDENT_INFO
FROM STUDENT_MASTER;

-- DISPLAY STUDENT AND TEACHER INFO LIVING IN MUMBAI


SELECT STUDENT_NAME, ADDRESS
FROM STUDENT_MASTER
WHERE ADDRESS LIKE '%MUMBAI%';
SELECT TEACHER_NAME, EMAIL
FROM TEACHER_MASTER
WHERE EMAIL LIKE '%GMAIL.COM' AND EMAIL LIKE '%MUMBAI%';

-- DISPLAY TEACHER INFO WHO HAVE GMAIL ACCOUNT


SELECT *
FROM TEACHER_MASTER
WHERE EMAIL LIKE '%GMAIL.COM%';

-- DESTROY TABLES STUDENT_MASTER AND TEACHER_MASTER


DROP TABLE STUDENT_MASTER;
DROP TABLE TEACHER_MASTER;
Q1. CREATE TABLE AS PRODUCT_MASTER WITH FIELD AS

-- CREATE THE PRODUCT_MASTER TABLE


CREATE TABLE PRODUCT_MASTER (
PNO INT,
PNAME VARCHAR(30),
QUANTITY INT,
COST_PRICE INT,
SELL_PRICE INT
);

-- APPLY CONSTRAINT TO PNO AS NOT NULL USING ALTER COMMAND


ALTER TABLE PRODUCT_MASTER
MODIFY PNO INT NOT NULL;

-- APPLY CONSTRAINTS TO PNO AS PRIMARY KEY USING ALTER COMMAND


ALTER TABLE PRODUCT_MASTER
ADD CONSTRAINT PK_PNO PRIMARY KEY (PNO);

-- FIND AVERAGE SELLING PRICE OF THE PRODUCT


SELECT AVG(SELL_PRICE) AS AVG_SELL_PRICE
FROM PRODUCT_MASTER;

-- FIND THE COST OF PRODUCTS 'MONITOR' AND 'KEYBOARD'


SELECT COST_PRICE
FROM PRODUCT_MASTER
WHERE PNAME IN ('MONITOR', 'KEYBOARD');

-- 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

-- CREATE THE STUDENT TABLE


CREATE TABLE STUDENT (
ROLLNO INT PRIMARY KEY,
NAME VARCHAR(30),
CITY VARCHAR(30),
GENDER CHAR(1) CHECK (GENDER = 'M' OR GENDER = 'F'),
CLASS VARCHAR(30),
SUBJECT VARCHAR(30)
);

-- DISPLAY STUDENTS INFORMATION WHO BELONGS TO PUNE CITY


SELECT *
FROM STUDENT
WHERE CITY = 'PUNE';

-- UPDATE ALL CLASSES WITH 'IMCA' & SUBJECT WITH 'SOFTWARE_ENGG'


UPDATE STUDENT
SET CLASS = 'IMCA', SUBJECT = 'SOFTWARE_ENGG';

-- LIST OF ALL STUDENTS FOR CITY AS JALGAON & GENDER AS MALE


SELECT *
FROM STUDENT
WHERE CITY = 'JALGAON' AND GENDER = 'M';

-- COUNT TOTAL NUMBER OF STUDENTS


SELECT COUNT(*) AS TOTAL_STUDENTS
FROM STUDENT;

-- CREATE VIEW STUDENT_VIEW AND UPDATE THE VIEW


CREATE VIEW STUDENT_VIEW AS
SELECT ROLLNO, NAME, CITY, GENDER, CLASS, SUBJECT
FROM STUDENT;
-- UPDATE THE VIEW
UPDATE STUDENT_VIEW
SET CLASS = 'MCA'
WHERE CITY = 'PUNE';
Q1 . CREATE TABLE EMP AND DEPT

-- CREATE THE EMP TABLE


CREATE TABLE EMP (
EMP_NO INT PRIMARY KEY,
E_NAME VARCHAR(30),
SALARY INT,
HIRE_DATE DATETIME,
JOB VARCHAR(20),
DEPT_NO INT,
FOREIGN KEY (DEPT_NO) REFERENCES DEPT(DEPT_NO)
);

-- CREATE THE DEPT TABLE


CREATE TABLE DEPT (
DEPT_NO INT PRIMARY KEY,
D_NAME VARCHAR(30),
LOC VARCHAR(20)
);
-- INSERT RECORDS INTO THE EMP TABLE
INSERT INTO EMP (EMP_NO, E_NAME, SALARY, HIRE_DATE, JOB, DEPT_NO) VALUES
(1, 'JOHN DOE', 50000, '2022-01-01', 'MANAGER', 1),
(2, 'JANE SMITH', 40000, '2022-02-01', 'DEVELOPER', 2),
(3, 'BOB JOHNSON', 45000, '2022-03-01', 'ANALYST', 1);

-- INSERT RECORDS INTO THE DEPT TABLE


INSERT INTO DEPT (DEPT_NO, D_NAME, LOC) VALUES
(1, 'FINANCE', 'PUNE'),
(2, 'IT', 'MUMBAI');

-- WRITE A QUERY TO COUNT THE NUMBER OF EMPLOYEES IN EACH DEPT


SELECT D.D_NAME, COUNT(*) AS NUM_EMPLOYEES
FROM EMP E
INNER JOIN DEPT D ON E.DEPT_NO = D.DEPT_NO
GROUP BY D.D_NAME;

-- FIND THE MAX SALARY


SELECT MAX(SALARY) AS MAX_SALARY
FROM EMP;

-- FIND THE AVERAGE SALARY


SELECT AVG(SALARY) AS AVG_SALARY
FROM EMP;
-- CREATE A VIEW EMP_VIEW AND INSERT AND UPDATE VIEW WITH NEW VALUES
CREATE VIEW EMP_VIEW AS
SELECT EMP_NO, E_NAME, SALARY, HIRE_DATE, JOB, DEPT_NO
FROM EMP;
-- INSERT NEW VALUES INTO THE VIEW
INSERT INTO EMP_VIEW (EMP_NO, E_NAME, SALARY, HIRE_DATE, JOB, DEPT_NO)
VALUES (4, 'ALICE BROWN', 55000, '2022-04-01', 'DEVELOPER', 2);
-- UPDATE THE VIEW WITH NEW VALUES
UPDATE EMP_VIEW
SET SALARY = 60000
WHERE EMP_NO = 4;
-- FIND THE EMPLOYEE NAMES WHO ARE WORKING IN FINANCE DEPT
SELECT E_NAME
FROM EMP
WHERE DEPT_NO = (SELECT DEPT_NO FROM DEPT WHERE D_NAME = 'FINANCE');
Q1) CREATE A TABLE AS CRICKET_TEAMS

-- CREATE THE CRICKET_TEAMS TABLE


CREATE TABLE CRICKET_TEAMS (
TEAM_ID INT PRIMARY KEY,
TEAM_NAME VARCHAR(30),
TEAM_CAPTAIN VARCHAR(30),
TEAM_RANKING SMALLINT
);

-- INSERT RECORDS INTO THE CRICKET_TEAMS TABLE


INSERT INTO CRICKET_TEAMS (TEAM_ID, TEAM_NAME, TEAM_CAPTAIN, TEAM_RANKING) VALUES
(1, 'INDIA', 'VIRAT KOHLI', 2),
(2, 'AUSTRALIA', 'AARON FINCH', 3),
(3, 'ENGLAND', 'EOIN MORGAN', 1);

-- FIND THE NAMES OF ALL CAPTAINS HAVING "M" AS FIRST LETTER


SELECT TEAM_CAPTAIN
FROM CRICKET_TEAMS
WHERE TEAM_CAPTAIN LIKE 'M%';

-- FIND THE NAMES OF ALL CAPTAINS HAVING "O" AS SECOND LETTER


SELECT TEAM_CAPTAIN
FROM CRICKET_TEAMS
WHERE TEAM_CAPTAIN LIKE '_O%';

-- FIND ALL TEAMS WHOSE RANKING IS BETWEEN 1 AND 5


SELECT *
FROM CRICKET_TEAMS
WHERE TEAM_RANKING BETWEEN 1 AND 5;

-- FIND THE TEAM NAME WITH HIGHEST RANKING


SELECT TEAM_NAME
FROM CRICKET_TEAMS
WHERE TEAM_RANKING = (SELECT MAX(TEAM_RANKING) FROM CRICKET_TEAMS);

-- DEMONSTRATE TRUNCATE AND DROP TABLE


TRUNCATE TABLE CRICKET_TEAMS; -- DELETES ALL RECORDS FROM THE TABLE
DROP TABLE CRICKET_TEAMS; -- DELETES THE TABLE ITSELF
Q1) CREATE A TABLE AS PRODUCT WITH

-- CREATE THE PRODUCT TABLE


CREATE TABLE PRODUCT (
P_CODE INT PRIMARY KEY,
P_NAME VARCHAR(30) UNIQUE,
P_QTY INT CHECK (P_QTY BETWEEN 25 AND 50),
P_RATE INT CHECK (P_RATE > 0)
);

-- INSERT RECORDS INTO THE PRODUCT TABLE


INSERT INTO PRODUCT (P_CODE, P_NAME, P_QTY, P_RATE) VALUES
(1, 'PRODUCT A', 30, 500),
(2, 'PRODUCT B', 40, 700),
(3, 'PRODUCT C', 20, 900);

-- DISPLAY ALL RECORDS


SELECT * FROM PRODUCT;

-- 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 THE PRODUCT NAME WITH MINIMUM COST/RATE


SELECT P_NAME
FROM PRODUCT
WHERE P_RATE/P_QTY = (SELECT MIN(P_RATE/P_QTY) FROM PRODUCT);

-- DESTROY THE TABLE


DROP TABLE PRODUCT;

-- CREATE VIEW PRO_VIEW AND INSERT & UPDATE THE VIEW


CREATE VIEW PRO_VIEW AS
SELECT P_CODE, P_NAME, P_QTY, P_RATE
FROM PRODUCT;

-- INSERT NEW VALUES INTO THE VIEW


INSERT INTO PRO_VIEW (P_CODE, P_NAME, P_QTY, P_RATE)
VALUES (4, 'PRODUCT D', 35, 600);

-- UPDATE THE VIEW WITH NEW VALUES


UPDATE PRO_VIEW
SET P_RATE = 800
WHERE P_CODE = 4;
Q1) CREATE TABLE BOOK INSERT RECORDS TO SOLVE QUERY

-- CREATE THE BOOK TABLE


CREATE TABLE BOOK (
ACC_NO INT PRIMARY KEY,
BOOK_NAME VARCHAR(20) NOT NULL UNIQUE,
AUTHOR_NAME VARCHAR(20),
ISSUE_DATE DATETIME,
SUBJECT CHAR(20) NOT NULL
);

-- INSERT RECORDS INTO THE BOOK TABLE


INSERT INTO BOOK (ACC_NO, BOOK_NAME, AUTHOR_NAME, ISSUE_DATE, SUBJECT) VALUES
(1, 'BOOK A', 'AUTHOR A', '2016-10-25', 'SUBJECT A'),
(2, 'BOOK B', 'AUTHOR B', '2016-10-26', 'SUBJECT B'),
(3, 'BOOK C', 'AUTHOR C', '2016-10-27', 'SUBJECT C');

-- 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%';

-- ADD ONE MORE FIELD AS "NO_OF_PAGES" TO THE TABLE


ALTER TABLE BOOK
ADD NO_OF_PAGES INT;

-- DISPLAY THE BOOK INFO WHICH WERE ISSUED ON 26-OCT-2016


SELECT *
FROM BOOK
WHERE ISSUE_DATE = '2016-10-26';

-- APPLY NOT NULL CONSTRAINT TO AUTHOR_NAME USING ALTER COMMAND


ALTER TABLE BOOK
ALTER COLUMN AUTHOR_NAME VARCHAR(20) NOT NULL
Q1) CREATE A TABLE CLIENT_MASTER AND SALESMAN_MASTER

-- CREATE THE CLIENT_MASTER TABLE


CREATE TABLE CLIENT_MASTER (
CLIENT_NO INT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
CITY VARCHAR(30),
ADDRESS VARCHAR(20)
);
-- INSERT RECORDS INTO THE CLIENT_MASTER TABLE
INSERT INTO CLIENT_MASTER (CLIENT_NO, NAME, CITY, ADDRESS) VALUES
(1, 'CLIENT A', 'MUMBAI', 'ADDRESS A'),
(2, 'CLIENT B', 'DELHI', 'ADDRESS B'),
(3, 'CLIENT C', 'MUMBAI', 'ADDRESS C');

-- CREATE THE SALESMAN_MASTER TABLE


CREATE TABLE SALESMAN_MASTER (
SALESMAN_NO INT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
CITY VARCHAR(30),
EMAIL VARCHAR(25) UNIQUE
);

-- INSERT RECORDS INTO THE SALESMAN_MASTER TABLE


INSERT INTO SALESMAN_MASTER (SALESMAN_NO, NAME, CITY, EMAIL) VALUES
(1, 'SALESMAN A', 'MUMBAI', '[email protected]'),
(2, 'SALESMAN B', 'DELHI', '[email protected]'),
(3, 'SALESMAN C', 'MUMBAI', '[email protected]');

-- DISPLAY ALL RECORDS FOR CLIENT_MASTER AND SALESMAN_MASTER


SELECT * FROM CLIENT_MASTER;
SELECT * FROM SALESMAN_MASTER;

-- CONCATENATE THE NAME AND ADDRESS OF ALL THE CLIENTS


SELECT CONCAT(NAME, ' - ', ADDRESS) AS CLIENT_INFO
FROM CLIENT_MASTER;

-- DISPLAY CLIENT INFO LIVING IN MUMBAI


SELECT *
FROM CLIENT_MASTER
WHERE CITY = 'MUMBAI';

-- DISPLAY SALESMAN INFO WHO HAVE GMAIL ACCOUNT


SELECT *
FROM SALESMAN_MASTER
WHERE EMAIL LIKE '%@GMAIL.COM';

-- DESTROY TABLES CLIENT_MASTER AND SALESMAN_MASTER


DROP TABLE CLIENT_MASTER;
DROP TABLE SALESMAN_MASTER;
Q1) CREATE THE FOLLOWING TABLES. HOSPITAL + DOCTOR

CREATE DATABASE HOSPITAL


USE HOSPITAL
-- Create the HOSPITAL table
CREATE TABLE HOSPITAL (
h_id INT PRIMARY KEY,
h_name VARCHAR(30) NOT NULL,
h_address VARCHAR(30) DEFAULT 'Jalgaon',
h_phone VARCHAR(20) UNIQUE
);

-- Insert records into the HOSPITAL table


INSERT INTO HOSPITAL (h_id, h_name, h_address, h_phone) VALUES
(1, 'K.K. Hospital', 'Pune', '1234567890'),
(2, 'ABC Hospital', 'Mumbai', '0987654321'),
(3, 'XYZ Hospital', 'Delhi', '9876543210');

-- Create the DOCTOR table


CREATE TABLE DOCTOR (
d_id INT PRIMARY KEY,
d_name VARCHAR(30),
d_address VARCHAR(30),
d_years_of_exp SMALLINT,
h_id INT,
FOREIGN KEY (h_id) REFERENCES HOSPITAL(h_id)
);

-- Insert records into the DOCTOR table


INSERT INTO DOCTOR (d_id, d_name, d_address, d_years_of_exp, h_id) VALUES
(1, 'Doctor A', 'Pune', 5, 1),
(2, 'Doctor B', 'Mumbai', 10, 2),
(3, 'Doctor C', 'Delhi', 3, 3);

-- Display the information of all the hospitals


SELECT * FROM HOSPITAL;

-- Display the name & address of doctors who lives in Pune


SELECT d_name, d_address FROM DOCTOR WHERE d_address = 'Pune';

-- Find the doctor with maximum experience


SELECT * FROM DOCTOR WHERE d_years_of_exp = (SELECT MAX(d_years_of_exp) FROM DOCTOR);

-- 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;

-- Delete the record of K.K. Hospital


DELETE FROM HOSPITAL WHERE h_name = 'K.K. Hospital';

DROP TABLE DOCTOR,HOSPITAL


Q1) CREATE THE FOLLOWING TABLE AND SOLVE FOLLOWING QUERIES.
ACTOR (NAME, GENDER, BIRTHDATE, FILM_NAME, LANGUAGE, SALARY)

-- Create the ACTOR table


CREATE TABLE ACTOR (
name VARCHAR(30) NOT NULL,
gender VARCHAR(10),
birthdate DATE,
film_name VARCHAR(30) NOT NULL,
language VARCHAR(20),
salary INT CHECK (salary > 0),
CONSTRAINT actor_pk PRIMARY KEY (name, film_name)
);

--Insert records into the ACTOR table


INSERT INTO ACTOR (name, gender, birthdate, film_name, language, salary)
VALUES ('Madhuri', 'Female', '1970-05-15', 'Film A', 'Hindi', 100000),
('Preeti', 'Female', '1980-01-01', 'Film B', 'English', 50000),
('Rani', 'Female', '1978-03-21', 'Film C', 'Hindi', 80000),
('Aishwarya', 'Female', '1973-11-01', 'Film D', 'Hindi', 70000),
('Salman', 'Male', '1965-12-27', 'Film E', 'Hindi', 90000);

-- Update film_name where actress name is 'Madhuri'


UPDATE ACTOR
SET film_name = 'Film X'
WHERE name = 'Madhuri';

-- 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;

-- Display record of actress where name is Preeti, Madhuri, Rani, Aishwarya


SELECT *
FROM ACTOR
WHERE name IN ('Preeti', 'Madhuri', 'Rani', 'Aishwarya');

-- Delete the record where actor's name is Salman


DELETE FROM ACTOR
WHERE name = 'Salman';

-- Find the average salary of the actor


SELECT AVG(salary)
FROM ACTOR;
Q1) CREATE TABLE AS PRODUCT_MASTER WITH FIELDS AS

-- Create the PRODUCT_MASTER table


CREATE TABLE PRODUCT_MASTER (
pno VARCHAR(20),
pname VARCHAR(30),
quantity INT,
cost_price INT,
sell_price INT,
CONSTRAINT pno_not_null CHECK (pno IS NOT NULL)
);

-- Insert records into the PRODUCT_MASTER table


INSERT INTO PRODUCT_MASTER (pno, pname, quantity, cost_price, sell_price)
VALUES ('P001', 'Product A', 100, 500, 1000),
('P002', 'Product B', 200, 1000, 2000),
('P003', 'Product C', 300, 1500, 3000);

-- Apply constraints to pno as primary key using ALTER command


ALTER TABLE PRODUCT_MASTER
ADD CONSTRAINT pno_pk PRIMARY KEY (pno);

-- Show distinct cost_price of all the products


SELECT DISTINCT cost_price
FROM PRODUCT_MASTER;

-- Display the total quantity of all the products


SELECT SUM(quantity)
FROM PRODUCT_MASTER;

-- 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

-- Create the STUDENT table


CREATE TABLE STUDENT (
rollno INT PRIMARY KEY,
name VARCHAR(30),
city VARCHAR(30),
gender CHAR(1) CHECK (gender = 'M' OR gender = 'F'),
class VARCHAR(30),
subject VARCHAR(30)
);

-- Insert records into the STUDENT table


INSERT INTO STUDENT (rollno, name, city, gender, class, subject)
VALUES (1, 'Student A', 'Pune', 'M', 'IMCA', 'Software Engg'),
(2, 'Student B', 'Mumbai', 'F', 'MBA', 'Marketing'),
(3, 'Student C', 'Jalgaon', 'M', 'MCA', 'Database'),
(4, 'Student D', 'Pune', 'F', 'MBA', 'Finance'),
(5, 'Student E', 'Jalgaon', 'M', 'IMCA', 'Networking');

-- Display students information who belongs to Pune city


SELECT *
FROM STUDENT
WHERE city = 'Pune';

-- Update all classes with 'IMCA' & subject with 'Software_Engg'


UPDATE STUDENT
SET class = 'IMCA', subject = 'Software_Engg';

-- List of all students for city as Jalgaon & gender as male


SELECT *
FROM STUDENT
WHERE city = 'Jalgaon' AND gender = 'M';

-- Display count of total number of students


SELECT COUNT(*)
FROM STUDENT;

-- Create a view STUDENT and update the view


CREATE VIEW STUDENT_VIEW AS
SELECT name, city, class
FROM STUDENT
WHERE gender = 'M';
INSERT INTO STUDENT_VIEW (name, city, class)
VALUES ('Student F', 'Mumbai', 'MBA');
UPDATE STUDENT_VIEW
SET class = 'MCA'
WHERE name = 'Student F';
Q1) CREATE TABLES AND INSERT RECORDS AS EMP AND DEPT
-- Create the EMP table
CREATE TABLE EMP (
emp_no INT PRIMARY KEY,
e_name VARCHAR(30),
salary INT,
hire_date DATETIME,
job VARCHAR(20),
dept_no INT,
FOREIGN KEY (dept_no) REFERENCES DEPT(dept_no)
);

-- Insert records into the EMP table


INSERT INTO EMP (emp_no, e_name, salary, hire_date, job, dept_no)
VALUES (1, 'Employee A', 50000, '2022-01-01', 'Manager', 1),
(2, 'Employee B', 40000, '2022-02-01', 'Clerk', 2),
(3, 'Employee C', 60000, '2022-03-01', 'Analyst', 1),
(4, 'Employee D', 70000, '2022-04-01', 'Manager', 2),
(5, 'Employee E', 80000, '2022-05-01', 'Analyst', 1);

-- Create the DEPT table


CREATE TABLE DEPT (
dept_no INT PRIMARY KEY,
d_name VARCHAR(30),
loc VARCHAR(20)
);
-- Insert records into the DEPT table
INSERT INTO DEPT (dept_no, d_name, loc)
VALUES (1, 'Finance', 'Mumbai'),
(2, 'Sales', 'Delhi'),
(3, 'Marketing', 'Bangalore');
-- Write a query to count the number of employees in each dept
SELECT d.d_name, COUNT(e.emp_no) AS num_employees
FROM DEPT d
LEFT JOIN EMP e ON d.dept_no = e.dept_no
GROUP BY d.d_name;

-- Find the max salary


SELECT MAX(salary)
FROM EMP;
-- Find the average salary
SELECT AVG(salary)
FROM EMP;
-- Create a view EMP_VIEW & insert and update view with new values
CREATE VIEW EMP_VIEW AS
SELECT e_name, salary, job
FROM EMP
WHERE dept_no = 1;
INSERT INTO EMP_VIEW (e_name, salary, job)
VALUES ('Employee F', 90000, 'Analyst');
UPDATE EMP_VIEW
SET salary = 100000
WHERE e_name = 'Employee F';
-- Find the employee names who are working in Finance dept
SELECT e_name
FROM EMP
WHERE dept_no = 1;
Q1) CREATE TABLE AS PRODUCT_MASTER WITH FIELDS AS

-- Create the PRODUCT_MASTER table


CREATE TABLE PRODUCT_MASTER (
pno VARCHAR(20),
pname VARCHAR(30),
quantity INT,
cost_price INT,
sell_price INT,
CONSTRAINT pno_not_null CHECK (pno IS NOT NULL)
);

-- Insert records into the PRODUCT_MASTER table


INSERT INTO PRODUCT_MASTER (pno, pname, quantity, cost_price, sell_price)
VALUES ('P001', 'Product A', 100, 500, 1000),
('P002', 'Product B', 200, 1000, 2000),
('P003', 'Product C', 300, 1500, 3000);

-- Apply constraints to pno as primary key using ALTER command


ALTER TABLE PRODUCT_MASTER
ADD CONSTRAINT pno_pk PRIMARY KEY (pno);

-- Show distinct cost_price of all the products


SELECT DISTINCT cost_price
FROM PRODUCT_MASTER;

-- Display the total quantity of all the products


SELECT SUM(quantity)
FROM PRODUCT_MASTER;

-- 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

-- Create the HOSPITAL table


CREATE TABLE HOSPITAL (
H_ID INT PRIMARY KEY,
H_NAME VARCHAR(30) NOT NULL,
H_ADDRESS VARCHAR(30) DEFAULT 'Jalgaon',
H_PHONE VARCHAR(20) UNIQUE
);

-- Insert records into the HOSPITAL table


INSERT INTO HOSPITAL (H_ID, H_NAME, H_ADDRESS, H_PHONE)
VALUES (1, 'K.K. Hospital', 'Pune', '1234567890'),
(2, 'ABC Hospital', 'Mumbai', '0987654321'),
(3, 'XYZ Hospital', 'Delhi', '9876543210');

-- Create the DOCTOR table


CREATE TABLE DOCTOR (
D_ID INT PRIMARY KEY,
D_NAME VARCHAR(30),
D_ADDRESS VARCHAR(30),
D_YEARS_OF_EXP SMALLINT,
H_ID INT,
FOREIGN KEY (H_ID) REFERENCES HOSPITAL(H_ID)
);

-- Insert records into the DOCTOR table


INSERT INTO DOCTOR (D_ID, D_NAME, D_ADDRESS, D_YEARS_OF_EXP, H_ID)
VALUES (1, 'Doctor A', 'Pune', 5, 1),
(2, 'Doctor B', 'Mumbai', 10, 2),
(3, 'Doctor C', 'Delhi', 15, 3);

-- Display the information of all the hospitals


SELECT *
FROM HOSPITAL;

-- Display the name & address of doctors who lives in Pune


SELECT D_NAME, D_ADDRESS
FROM DOCTOR
WHERE D_ADDRESS = 'Pune';

-- Find the doctor with maximum experience


SELECT D_NAME, D_YEARS_OF_EXP
FROM DOCTOR
WHERE D_YEARS_OF_EXP = (SELECT MAX(D_YEARS_OF_EXP) FROM DOCTOR);

-- 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;

-- Delete the record of K.K. Hospital


DELETE FROM HOSPITAL
WHERE H_NAME = 'K.K. Hospital';

Q1) CREATE A TABLE AS CRICKET_TEAMS

-- Create the CRICKET_TEAMS table


CREATE TABLE CRICKET_TEAMS (
TEAM_ID INT PRIMARY KEY,
TEAM_NAME VARCHAR(30),
TEAM_CAPTAIN VARCHAR(30),
TEAM_RANKING SMALLINT
);

-- Insert records into the CRICKET_TEAMS table


INSERT INTO CRICKET_TEAMS (TEAM_ID, TEAM_NAME, TEAM_CAPTAIN,
TEAM_RANKING)
VALUES (1, 'India', 'Virat Kohli', 2),
(2, 'Australia', 'Aaron Finch', 3),
(3, 'England', 'Eoin Morgan', 1),
(4, 'South Africa', 'Quinton de Kock', 5),
(5, 'New Zealand', 'Kane Williamson', 4);

-- Find the names of all captains having "M" as first letter


SELECT TEAM_CAPTAIN
FROM CRICKET_TEAMS
WHERE TEAM_CAPTAIN LIKE 'M%';

-- Find the names of all captains having "O" as second letter


SELECT TEAM_CAPTAIN
FROM CRICKET_TEAMS
WHERE TEAM_CAPTAIN LIKE '_O%';

-- Find all teams whose ranking is between 1 and 5


SELECT *
FROM CRICKET_TEAMS
WHERE TEAM_RANKING BETWEEN 1 AND 5;

-- Find the team name with highest ranking


SELECT TEAM_NAME
FROM CRICKET_TEAMS
WHERE TEAM_RANKING = (SELECT MAX(TEAM_RANKING) FROM
CRICKET_TEAMS);

-- Demonstrate TRUNCATE and DROP TABLE


TRUNCATE TABLE CRICKET_TEAMS; -- Deletes all records from the table
DROP TABLE CRICKET_TEAMS; -- Deletes the entire table
Q1) CREATE THE FOLLOWING TABLE AND SOLVE FOLLOWING QUERIES.
ACTOR (NAME, GENDER, BIRTHDATE, FILM_NAME, LANGUAGE, SALARY)

CREATE TABLE actor (


name VARCHAR(50) NOT NULL,
gender CHAR(1),
birthdate DATE,
film_name VARCHAR(50) NOT NULL,
language VARCHAR(50),
salary DECIMAL(10,2) CHECK (salary > 0),
CONSTRAINT pk_actor PRIMARY KEY (name, film_name)
);

--Insert records into the ACTOR table


INSERT INTO actor (name, gender, birthdate, film_name, language, salary)
VALUES ('Tom Hanks', 'M', '1956-07-09', 'Forrest Gump', 'English', 1000000.00);

--Update FILM_NAME where ACTRESS NAME is 'MADHURI'


UPDATE ACTOR SET FILM_NAME = 'Hum Aapke Hain Koun'WHERE NAME = 'Madhuri';

--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;

--Display record of ACTRESS where NAME is PREETI, MADHURI, RANI, AISHWARYA


SELECT *
FROM ACTOR
WHERE NAME IN ('Preeti', 'Madhuri', 'Rani', 'Aishwarya');

--Delete the record where ACTOR'S NAME is SALMAN


Delete the record where ACTOR'S NAME is SALMAN
Q1) CREATE TABLES AS PRODUCT AND ORDER TABLE

CREATE TABLE PRODUCT (


P_NO VARCHAR(20) PRIMARY KEY,
P_NAME VARCHAR(30),
RATE INT
);

CREATE TABLE ORDER_TABLE (


O_NO INT PRIMARY KEY,
P_NO VARCHAR(20) REFERENCES PRODUCT(P_NO),
QTY_ORDERED INT,
O_DATE DATETIME
);

--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;

INSERT INTO ORDER_VIEW (O_NO, P_NAME, QTY_ORDERED, O_DATE)


VALUES (1, 'Product A', 10, '2023-04-11');

UPDATE ORDER_VIEW
SET QTY_ORDERED = 20
WHERE O_NO = 1;
Q1) CREATE A TABLE CLIENT_MASTER AND SALESMAN_MASTER

CREATE TABLE CLIENT_MASTER (


CLIENT_NO INT PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
CITY VARCHAR(30),
ADDRESS VARCHAR(20)
);

CREATE TABLE SALESMAN_MASTER (


SALESMAN_NO INT PRIMARY KEY,
NAME VARCHAR(30),
CITY VARCHAR(30),
EMAIL VARCHAR(25) UNIQUE
);

--To display all records for client and salesman,


SELECT * FROM CLIENT_MASTER;
SELECT * FROM SALESMAN_MASTER;

--To concatenate the name and address of all the clients


SELECT CONCAT(NAME, ' ', ADDRESS) AS CLIENT_INFO
FROM CLIENT_MASTER;

--To display client info living in Mumbai,


SELECT * FROM CLIENT_MASTER
WHERE CITY = 'Mumbai';

--To display salesman info who have Gmail account


SELECT * FROM SALESMAN_MASTER
WHERE EMAIL LIKE '%@gmail.com';

--To destroy tables CLIENT_MASTER and SALESMAN_MASTER,


DROP TABLE CLIENT_MASTER;
DROP TABLE SALESMAN_MASTER;

You might also like