0% found this document useful (0 votes)
23 views31 pages

Dbms Assignment

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
23 views31 pages

Dbms Assignment

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

Department of Mathematics & Scientific Computing

LAB MANUAL-2024

ROLL NO. – 23MMA019


NAME – VIANY KUMAR
BRANCH – M.Sc. (M&C)
ASSIGNMENT – 1

create database company;

use company;

Q1. Create the employee table


create table employee (

emp_no int ,

e_name varchar(60),

e_address varchar(100),

e_ph_no varchar(20)

dept_no int,

dept_name varchar(100),

job_id varchar(10),

salary float

);

Q2. Add a new column: HIREDATE


alter table employee

add hiredate date;

Q3. Change the datatype of JOB_ID from char to varchar


alter table employee

modify job_id varchar(20);

Q4. Change the name of column Emp_no to E_no


alter table employee

change emp_no e_no int;

Q5. Modify the column width of the job field (JOB_ID)


alter table employee

modify job_id varchar(50);

drop table employee;


Q1. Create the EMPLOYEE table
create table employee (

emp_no int ,

e_name varchar(100),

e_address varchar(100),

e_ph_no varchar(20),

dept_no int,

dept_name varchar(100),

job_id varchar(20),

salary float

);

Q2. Insert at least 5 rows into the EMPLOYEE table


insert into employee (emp_no, e_name, e_address, e_ph_no, dept_no, dept_name, job_id, salary)

values

(1, 'John', 'New York', '1234567890', 10, 'SALES', 'MGR', 5000),

(2, 'James', 'Los Angeles', '9876543210', 20, 'IT', 'ANALYST', 4000),

(3, 'Sara', 'Chicago', '4567891230', 30, 'HR', 'CLERK', 3000),

(4, 'Mike', 'Dallas', '1478523690', 10, 'SALES', 'SALESPERSON', 3500),

(5, 'Anna', 'Miami', '2589631470', 40, 'MECH', 'ENGINEER', 6000);

Q3. Display all the information of the EMPLOYEE table


select * from employee;

Q4. Display the record of each employee who works in department D10
select * from employee

where dept_no = 10;


Q5. Update the city of Emp_no-12 (assuming emp_no = 12)
update employee set e_address = 'Nagpur'

where emp_no = 12;

Q6. Display the details of Employee who works in department MECH


select * from employee

where dept_name = 'MECH';

Q7. Delete the email_id of employee James (assuming email is in e_ph_no)


update employee set e_ph_no = null

where e_name = 'James';

Q8. Display the complete record of employees working in the SALES Department
select * from employee

where dept_name = 'SALES';

drop table employee;

Q1. Create the employee table


create table employee (

emp_no int ,

e_name varchar(100),

e_address varchar(100),

e_ph_no varchar(20),

dept_no int,

dept_name varchar(100),

job_id varchar(10),

designation varchar(50),

salary float,

hiredate date
);

Q2. Insert data into the employee table


insert into employee (emp_no, e_name, e_address, e_ph_no, dept_no, dept_name, job_id,
designation, salary, hiredate)

values

(1, 'John', 'New York', '1234567890', 10, 'SALES', 'MGR', 'MANAGER', 5000, '1981-05-01'),

(2, 'James', 'Los Angeles', '9876543210', 20, 'IT', 'ANALYST', 'IT PROFF', 4000, '1981-12-03'),

(3, 'Sara', 'Chicago', '4567891230', 30, 'HR', 'CLERK', 'HR ASSISTANT', 3000, '1980-01-19'),

(4, 'Mike', 'Dallas', '1478523690', 40, 'MECH', 'ENGINEER', 'ENGINEER', 6000, '1981-12-17'),

(5, 'Sam', 'Miami', '2589631470', 10, 'SALES', 'SALESREP', 'SALES MANAGER', 3500, '2022-07-10');

Q3. List the E_no, E_name, and Salary of all employees working as MANAGER
select emp_no, e_name, salary from employee

where designation = 'MANAGER';

Q4. Display all the details of the employee whose salary is more than the salary of any IT
PROFF
select * from employee

where salary > (select max(salary) from employee where designation = 'IT PROFF');

Q5. List the employees in ascending order of Designations who joined after 1981
select * from employee

where hiredate > '1981-12-31' order by designation;

Q6. List the employees along with their Experience and Daily Salary
select e_name, hiredate, salary, floor(datediff(curdate(), hiredate) / 365) as experience,

(salary / 30) as daily_salary

from employee;
Q7. List the employees who are either 'CLERK' or 'ANALYST'
select * from employee

where job_id in ('CLERK', 'ANALYST');

Q8. List the employees who joined on specific dates


select * from employee

where hiredate in ('1981-05-01', '1981-12-03', '1981-12-17', '1980-01-19');

Q9. List the employees who are working for Dept_no 10 or 20


select * from employee

where dept_no in (10, 20);

Q10. List the E_names that start with 'S'


select e_name from employee

where e_name like 'S%';

Q11. Display the name and first five characters of names starting with 'H'
select e_name, left(e_name, 5) as first_five from employee

where e_name like 'H%';

Q12. List all employees except 'PRESIDENT' & 'MGR' in ascending order of Salaries
select * from employee

where job_id not in ('PRESIDENT', 'MGR')

order by salary;

ASSIGNMENT – 2
create database data;

use data;

CREATE TABLE hotel (

hotel_no CHAR(4) NOT NULL,

name VARCHAR(20) NOT NULL,

address VARCHAR(50) NOT NULL);

CREATE TABLE room (

room_no VARCHAR(4) NOT NULL,

hotel_no CHAR(4) NOT NULL,

type CHAR(1) NOT NULL,

price DECIMAL(5,2) NOT NULL

);

CREATE TABLE booking (


hotel_no CHAR(4) NOT NULL,

guest_no CHAR(4) NOT NULL,

date_from DATETIME NOT NULL,

date_to DATETIME NULL,

room_no CHAR(4) NOT NULL);

CREATE TABLE guest (

guest_no CHAR(4) NOT NULL,

name VARCHAR(20) NOT NULL,

address VARCHAR(50) NOT NULL);

INSERT INTO hotel (hotel_no, name, address)

VALUES

('H111', 'Grosvenor Hotel', 'London'),

('H112', 'riyaz hotel', 'New York'),

('H113', 'ridhi hotel', 'paris');

INSERT INTO room (room_no, hotel_no, type, price)

VALUES

('1', 'H111', 'S', 72.00),

('2', 'H112', 'D', 120.00),

('3', 'H111', 'S', 150.00);

INSERT INTO guest (guest_no, name, address)

VALUES

('G111', 'John smith', 'London'),

('G112', 'jimmy', 'New York'),

('G113', 'downy', 'Paris'),

('G114', 'danny', 'Tokyo');


INSERT INTO booking (hotel_no, guest_no, date_from, date_to, room_no)

VALUES

('H111', 'G111', '2024-01-01', '2024-01-03', '1'),

('H111', 'G112', '2024-02-01', '2024-02-05', '1'),

('H112', 'G113', '2024-03-10', '2024-03-12', '2'),

('H113', 'G114', '2024-04-15', '2024-04-20', '3');

Simple Queries
--QUERY--
SELECT * FROM HOTEL;

--QUERY--
SELECT * FROM HOTEL

WHERE ADDRESS ='LONDON';

--QUERY--
SELECT NAME,ADDRESS FROM GUEST

WHERE ADDRESS='LONDON' order by NAME;

--QUERY--
select * from room

where price <400.00 order by price asc;


--QUERY--
SELECT * FROM BOOKING

WHERE date_to = 2024-04-20;

Aggregate function
--QUERY--
SELECT COUNT(*) AS TOTAL_HOTELS FROM HOTEL;

--QUERY--
SELECT AVG(PRICE) AS AVG_PRICE FROM ROOM;

--QUERY--
SELECT SUM(PRICE) AS TOTAL_REVENUE FROM ROOM WHERE TYPE='S';

--QUERY--
SELECT COUNT(*) FROM BOOKING

WHERE MONTH(DATE_FROM)=3;
Subqueries and joins

--QUERY--
SELECT price, type FROM room

WHERE hotel_no = (SELECT hotel_no FROM hotel

WHERE name = 'Grosvenor Hotel');

--QUERY--
SELECT guest.name FROM guest INNER JOIN booking ON guest.guest_no = booking.guest_no

WHERE booking.hotel_no = (SELECT hotel_no FROM hotel WHERE name = 'Grosvenor Hotel')

AND CURDATE() BETWEEN booking.date_from AND booking.date_to;

--QUERY--
SELECT room.room_no, room.type, room.price, guest.name FROM room

LEFT JOIN booking ON room.room_no = booking.room_no

LEFT JOIN guest ON booking.guest_no = guest.guest_no

WHERE room.hotel_no = (SELECT hotel_no FROM hotel WHERE name = 'Grosvenor Hotel');

--QUERY--
SELECT SUM(room.price) AS total_income FROM booking
INNER JOIN room ON booking.room_no = room.room_no

WHERE booking.hotel_no = (SELECT hotel_no FROM hotel

WHERE name = 'Grosvenor Hotel')

AND CURDATE() BETWEEN booking.date_from AND booking.date_to;

--QUERY--
SELECT room.room_no FROM room LEFT JOIN booking ON room.room_no = booking.room_no

WHERE room.hotel_no = (SELECT hotel_no FROM hotel WHERE name = 'Grosvenor Hotel')

AND (booking.room_no IS NULL OR CURDATE() NOT BETWEEN booking.date_from AND


booking.date_to);

--QUERY--
SELECT SUM(room.price) AS lost_income FROM room

LEFT JOIN booking ON room.room_no = booking.room_no

WHERE room.hotel_no = (SELECT hotel_no FROM hotel WHERE name = 'Grosvenor Hotel')

AND (booking.room_no IS NULL OR CURDATE() NOT BETWEEN booking.date_from

AND booking.date_to);

Grouping

--QUERY--
SELECT hotel_no, COUNT(*) AS number_of_rooms FROM room GROUP BY hotel_no;

--QUERY--
SELECT room.hotel_no, COUNT(*) AS number_of_rooms FROM room

INNER JOIN hotel ON room.hotel_no = hotel.hotel_no

WHERE hotel.address = 'London'

GROUP BY room.hotel_no;

ASSIGNMENT – 3
create database company;

use company;

CREATE TABLE employees (

emp_id INT PRIMARY KEY,

first_name VARCHAR(50),

last_name VARCHAR(50),

email VARCHAR(100),

department VARCHAR(50),

salary float,

city VARCHAR(100)

);

insert into employees values

(101,"alak", "panday","alak707@gmail.com","ma",90000,"bikaner"),
(102,"sagar", "kasotiya","sager707@gmail.com","ph",70000,"kuchaman city"),

(103,"rinku", "kumar","rinku707@gmail.com","ch",80000,"kanpur"),

(104,"virat", "saini","virat707@gmail.com","ph",60000,"delhi"),

(105,"rahul", "singh","rahul707@gmail.com","ma",50000,"mumbai"),

(106,"viru", "saini","viru707@gmail.com","ph",70000,"delhi"),

(107,"ram", "singh","ram707@gmail.com","ma",50000,"mumbai");

--QUERY--
select city from employees where left(city,0) not in('a','e','i','o','u')

and right(city,1) not in('a','e','i','o','u');

--QUERY--
CREATE TABLE new_employees AS SELECT * FROM employees;

--QUERY--
select * from new_employees;

--QUERY--
select department, max(salary) as highest_salary from employees

group by department;
--QUERY--
select email, count(*) from employees

group by email having count(*)>1;

--QUERY--
SELECT UPPER(first_name) AS uppercase_name from employees;

--QUERY--
select * from employees order by emp_id asc limit 3;

--QUERY--
select concat(first_name,',',last_name) as full_name from employees;
--QUERY--
SELECT department, SUM(salary) AS total_salary

FROM employees GROUP BY department;

--QUERY--
SELECT * FROM employees e1 JOIN new_employees e2 ON e1.emp_id = e2.emp_id;

--QUERY--
SELECT * FROM employees WHERE city = 'Delhi';

--QUERY--
SELECT first_name FROM employees;
--QUERY--
SELECT * FROM employees

WHERE email IN (SELECT email FROM employees GROUP BY email HAVING COUNT(email) > 1);

--QUERY--
SELECT COUNT(*) FROM employees

WHERE department = 'ma';

--QUERY--
SELECT * FROM employees

WHERE first_name LIKE '__r%';

--QUERY--
SELECT * FROM employees

WHERE salary BETWEEN 50000 AND 70000;


ASSIGNMENT – 4
Table salesman:

create table salesman (

salesman_id int not null,

name varchar(30) ,

city varchar(20),

commision float);

insert into salesman values

(5001 ,"james Hoog","new york",0.15),

(5002,"nail Knite","paris",0.13),

(5005,"pit Alex","london",0.11),

(5006,"Mc Lyon ","paris",0.14),

(5003,"Lauson Hen",null,0.12),

(5007,"Paul Adam","rome",0.13);

Table coustomer:

create table customerr(

customer_id int null,

customer_name varchar(30),

city varchar(20),
grade int ,

salesman_id int );

insert into customerr values

(3002,"nick Rimando","new york",100,5001),

(3005,"Graham zusi","california",200,5002),

(3001,"Brad Guzan","london ",null,null),

(3, 'Vijay Patel', 'Ahmedabad', 2, 103),

(3004,"fabian john","paris",300,5006),

(3007,"Brad Davis","New york",200,5001),

(3009,"Geoff Camero","Berlin",100,null),

(3008,"julian green","london",300,5002),

(3003,"jozy altidor","moncow",200,5007);

Table orders:

create table orders( order_no int not null,

purch_amt float,

order_date date,

customer_id int,

salesman_id int);

insert into orders values

(7001, 150.5, '2016-10-05', 3005, 5002),

(7009, 270.65, '2016-09-10', 3001, NULL),

(7002, 65.26, '2016-10-05', 3002, 5001),

(7004, 110.5, '2016-08-17', 3009, NULL),

(7007, 948.5, '2016-09-10', 3005, 5002),

(7005, 2400.6, '2016-07-27', 3007, 5001),

(7008, 5760, '2016-09-10', 3002, 5001),

(7010, 1983.43, '2016-10-10', 3004, 5006),

(7003, 2480.4, '2016-10-10', 3009, NULL),


(7012, 250.45, '2016-06-27', 3008, 5002),

(7011, 75.29, '2016-08-17',3003,5007)

Table nobel_win:

CREATE TABLE nobel_win (

year INT,

subject VARCHAR(50),

winner VARCHAR(100),

country VARCHAR(50),

category VARCHAR(50)

);

INSERT INTO nobel_win (year, subject, winner, country, category) VALUES

(1970, 'Physics', 'Hannes Alfven', 'Sweden', 'Scientist'),

(1970, 'Physics', 'Louis Neel', 'France', 'Scientist'),

(1971, 'Economics', 'Simon Kuznets', 'Russia', 'Economist'),

(1970, 'Literature', 'Aleksandr Solzhenitsyn', 'Russia', 'Linguist'),

(1971, 'Literature', 'Pablo Neruda', 'Chile', 'Linguist'),

(1987, 'Chemistry', 'Donald J. Cram', 'USA', 'Scientist');

Table itm_mast:

CREATE TABLE item_mast (

pro_name VARCHAR(50),

pro_price DECIMAL(10, 2)

);

INSERT INTO item_mast (pro_name, pro_price) VALUES

('ZIP drive', 250.00),

('Mouse', 250.00),

('Keyboard', 300.00);

--QUERY--
(Display name and commission of all the salesmen)
SELECT name, commision

FROM salesman;

--QUERY--
(Retrieve salesman id of all salesmen from the orders table without any repeats)
SELECT DISTINCT salesman_id
FROM orders;

--QUERY--
(Display names and city of salesman, who belongs to the city of Paris)
SELECT name, city
FROM salesman
WHERE city = 'Paris';

--QUERY—
(Display all the information for those customers with a grade of 200)
SELECT *FROM customerr
WHERE grade = 200;

--QUERY—
(Display the order number, order date, and the purchase amount for orders delivered by
the salesman with ID 5001)
SELECT order_no, order_date, purch_amt
FROM orders
WHERE salesman_id = 5001;

--QUERY—
(Show the winner of the 1971 prize for Literature)
SELECT winner FROM nobel_win
WHERE year = 1971
AND subject = 'Literature';

--QUERY—
Show all the details of the winners with first name Louis.
SELECT * FROM nobel_win
WHERE winner LIKE 'Louis%';

--QUERY—
Show all the winners in Physics for 1970 together with the winner of Economics for 1971.
SELECT * FROM nobel_win
WHERE (subject = 'Physics' AND year = 1970)
UNION
SELECT * FROM nobel_win
WHERE (subject = 'Economics' AND year = 1971);

--QUERY—
Show all the winners of Nobel prize in the year 1970 except the subject Physiology and
Economics.
SELECT * FROM nobel_win
WHERE year = 1970
AND subject NOT IN ('Physiology', 'Economics');

--QUERY—
Find all the details of the Nobel winners for the subject not starting with the letter 'P' and
arranged as the most recent comes first, then by name in order.
SELECT * FROM nobel_win
WHERE subject NOT LIKE 'P%'
ORDER BY year DESC, winner;

--QUERY—
Find the name and price of the cheapest item(s) from item_mast.
SELECT pro_name, pro_price FROM item_mast
WHERE pro_price = (SELECT MIN(pro_price) FROM item_mast);

--QUERY—
Display all the customers, who either belong to New York or have not had a grade above
100.
SELECT * FROM customerr
WHERE city = 'New York' OR grade <= 100;

--QUERY—
Find salesmen with a commission within a range of 0.12 and 0.14.
SELECT salesman_id, name, city, commission FROM salesman
WHERE commission BETWEEN 0.12 AND 0.14;
--QUERY—
Find all customers whose names end with the letter 'n'.
SELECT * FROM customerr
WHERE customer_name LIKE '%n';

--QUERY—
Find salesmen whose name contains 'N' as the first character and 'l' as the fourth
character.
SELECT * FROM salesman
WHERE name LIKE 'N__l%';

--QUERY—
Find customers who do not have any grade assigned (grade is NULL).
SELECT *
FROM customer
WHERE grade IS NULL;

--QUERY—
Find the total purchase amount of all orders.
SELECT SUM(purch_amt)
FROM orders;

--QUERY—
Find the number of salesmen currently listing for all of their customers.
SELECT COUNT(DISTINCT salesman_id)
FROM orders;

--QUERY—
Find the highest grade for each of the cities of the customers.
SELECT city, MAX(grade)
FROM customer
GROUP BY city;

--QUERY—
Find the highest purchase amount ordered by each customer.
SELECT customer_id, MAX(purch_amt)
FROM orders
GROUP BY customer_id;
--QUERY—
Find the highest purchase amount ordered by each customer on a particular date.
SELECT customer_id, ord_date, MAX(purch_amt) FROM orders
GROUP BY customer_id, ord_date;

--QUERY—
Find the highest purchase amount ordered by each customer on a particular date.
SELECT customer_id, ord_date, MAX(purch_amt) FROM orders
GROUP BY customer_id, ord_date;
--QUERY—
Count all orders for a date SEPTEMBER 10th, 2012.
SELECT COUNT(*) FROM orders
WHERE ord_date = '2012-09-10';

Multiple Tables Joins Nested Queries

--QUERY—
Find the name and city of those customers and salesmen who live in the same city.
SELECT C.cust_name, S.name, S.city
FROM salesman AS S, customer AS C
WHERE S.city = C.city;

--QUERY—
Find the names of all customers along with the salesmen who work for them.
SELECT customerr.cust_name, salesman.name
FROM customerr, salesman
WHERE salesman.salesman_id = customerr.salesman_id;
--QUERY—
Display all those orders by the customers not located in the same cities where their
salesmen live.
SELECT ord_no, cust_name, orders.customer_id, orders.salesman_id
FROM salesman, customer, orders
WHERE customer.city <> salesman.city
AND orders.customer_id = customer.customer_id
AND orders.salesman_id = salesman.salesman_id;

--QUERY—
Display all the orders issued by the salesman 'Paul Adam' from the orders table.
SELECT * FROM orders
WHERE salesman_id = (SELECT salesman_id FROM salesman WHERE name = 'Paul Adam');

--QUERY—
Display all the orders which values are greater than the average order value for 10th
October 2012.
SELECT * FROM orders
WHERE purch_amt > (SELECT AVG(purch_amt) FROM orders
WHERE ord_date = '2012-10-10');

--QUERY—
Find all orders attributed to salesmen in Paris.
SELECT * FROM orders
WHERE salesman_id IN (SELECT salesman_id FROM salesman WHERE city ='Paris');

--QUERY—
Find the name and ids of all salesmen who had more than one customer.
SELECT c.salesman_id, s.name
FROM salesman AS s, customer AS c
WHERE s.salesman_id = c.salesman_id
GROUP BY c.salesman_id, s.name
HAVING COUNT(c.salesman_id) > 1;

--QUERY—
Display all the orders that had amounts that were greater than at least one of the orders
from September 10th 2012.
SELECT * FROM Orders
WHERE purch_amt > ANY (SELECT purch_amt
FROM orders WHERE ord_date = '2012-09-10');

You might also like