Dbms Assignment
Dbms Assignment
LAB MANUAL-2024
use company;
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
);
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
);
values
Q4. Display the record of each employee who works in department D10
select * from employee
Q8. Display the complete record of employees working in the SALES Department
select * from 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
);
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
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
Q6. List the employees along with their Experience and Daily Salary
select e_name, hiredate, salary, floor(datediff(curdate(), hiredate) / 365) as experience,
from employee;
Q7. List the employees who are either 'CLERK' or 'ANALYST'
select * from employee
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
Q12. List all employees except 'PRESIDENT' & 'MGR' in ascending order of Salaries
select * from employee
order by salary;
ASSIGNMENT – 2
create database data;
use data;
);
VALUES
VALUES
VALUES
VALUES
Simple Queries
--QUERY--
SELECT * FROM HOTEL;
--QUERY--
SELECT * FROM HOTEL
--QUERY--
SELECT NAME,ADDRESS FROM GUEST
--QUERY--
select * from room
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
--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')
--QUERY--
SELECT room.room_no, room.type, room.price, guest.name FROM room
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
--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')
--QUERY--
SELECT SUM(room.price) AS lost_income FROM room
WHERE room.hotel_no = (SELECT hotel_no FROM hotel WHERE name = 'Grosvenor Hotel')
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
GROUP BY room.hotel_no;
ASSIGNMENT – 3
create database company;
use company;
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
department VARCHAR(50),
salary float,
city VARCHAR(100)
);
(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')
--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
--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
--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
--QUERY--
SELECT * FROM employees
--QUERY--
SELECT * FROM employees
name varchar(30) ,
city varchar(20),
commision float);
(5002,"nail Knite","paris",0.13),
(5005,"pit Alex","london",0.11),
(5003,"Lauson Hen",null,0.12),
(5007,"Paul Adam","rome",0.13);
Table coustomer:
customer_name varchar(30),
city varchar(20),
grade int ,
salesman_id int );
(3005,"Graham zusi","california",200,5002),
(3004,"fabian john","paris",300,5006),
(3009,"Geoff Camero","Berlin",100,null),
(3008,"julian green","london",300,5002),
(3003,"jozy altidor","moncow",200,5007);
Table orders:
purch_amt float,
order_date date,
customer_id int,
salesman_id int);
Table nobel_win:
year INT,
subject VARCHAR(50),
winner VARCHAR(100),
country VARCHAR(50),
category VARCHAR(50)
);
Table itm_mast:
pro_name VARCHAR(50),
pro_price DECIMAL(10, 2)
);
('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';
--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');