1.
Create the Supplier table as shown below:
-- Creating Database
create database CyberSecurity;
-- Using Database
use CyberSecurity;
-- 1. Creating the Supplier table:
CREATE TABLE Supplier (
Sup_No VARCHAR(20),,
Sup_Name VARCHAR(50),
Item_Supplied VARCHAR(30),
Item_Price INT,
City VARCHAR(30)
);
desc Supplier;
Output:
-- Inserting into table
INSERT INTO Supplier VALUES
('S1', 'Suresh', 'Keyboard', 400, 'Hyderabad'),
('S2', 'Kiran', 'Processor', 8000, 'Delhi'),
('S3', 'Mohan', 'Mouse', 350, 'Delhi'),
('S4', 'Ramesh', 'Processor', 9000, 'Bangalore'),
('S5', 'Manish', 'Printer', 6000, 'Mumbai'),
('S6', 'Srikanth', 'Processor', 8500, 'Chennai');
select * from Supplier;
Output:
-- a) Write a SQL query to display Supplier numbers and Supplier names whose name
starts with ‘R’
SELECT Sup_No, Sup_Name
FROM Supplier
WHERE Sup_Name LIKE 'R%';
Output:
-- What does 'R%' mean?
-- % is a wildcard in SQL — it means “anything after”
-- 'R%' means:
-- The name starts with 'R'
-- Followed by zero or more characters
-- Examples that match: Ravi, Ramesh, Raj, Ritu
-- WHERE clause" is used to filter rows based on a condition
-- b) Write a SQL query to display the names of suppliers who supply Processors and whose
city is Delhi
SELECT Sup_Name
FROM Supplier
WHERE Item_Supplied = 'Processor' AND City = 'Delhi';
Output:
-- c) Write a SQL query to display the names of suppliers who supply the same items as
supplied by Ramesh
SELECT Sup_Name
FROM Supplier
WHERE Item_Supplied = (
SELECT Item_Supplied
FROM Supplier
WHERE Sup_Name = 'Ramesh'
);
Output:
SET SQL_SAFE_UPDATES = 0;
SET SQL_SAFE_UPDATES = 0; Disable protection temporarily
SET SQL_SAFE_UPDATES = 1; Enable protection again (recommended!)
-- d) Write sql query to increase the price of Keyboard by 200.
UPDATE Supplier
SET Item_Price = Item_Price + 200
WHERE Item_Supplied = 'Keyboard';
select * from supplier;
Output:
-- e) Write sql query to display supplier numbers, Supplier names and
item price for suppliers in delhi in the ascending order of item price
SELECT Sup_No, Sup_Name, Item_Price
FROM Supplier
WHERE City = 'Delhi'
ORDER BY Item_Price ASC;
Output:
-- f) Write sql query to add a new column called CONTACTNO.
ALTER TABLE Supplier
ADD CONTACTNO VARCHAR(15);
select * from supplier;
Output:
-- g) Write sql query to delete the record whose item price is the lowest of all the items
supplied.
DELETE FROM Supplier
WHERE Item_Price = (
SELECT MIN(Item_Price) FROM Supplier
);
Output:
-- h) Create a view on the table which displays only supplier numbers and supplier names.
CREATE VIEW SupplierView AS
SELECT Sup_No, Sup_Name
FROM Supplier;
Output:
-- i) Write sql query to display the records in the descending order of item price for each
item supplied.
SELECT * FROM Supplier
ORDER BY Item_Supplied, Item_Price DESC;
Output:
-- j) Write sql query to display the records of suppliers who supply items other than
Processor or Keyboard.
SELECT * FROM Supplier
WHERE Item_Supplied NOT IN ('Processor', 'Keyboard');
Output:
DBMS Lab - Task 2:
Below are the details of Employees working for a software Company. Create the table called
EmpDetails with the below mentioned details.
a. Write sql query to display all the employees whose designation is Programmer.
Create Table:
CREATE TABLE EmpDetails (
Eid VARCHAR(10) PRIMARY KEY,
Ename VARCHAR(50),
DOB DATE,
Designation VARCHAR(30),
Salary INT,
DOJ DATE);
Desc EmpDetails;
Output:
Insert Data:
INSERT INTO EmpDetails VALUES
('E101', 'Suma', '1989-12-29', 'Designer', 20000, '2010-04-01'),
('E102', 'Amit', '1995-01-10', 'Programmer', 25000, '2018-02-18'),
('E103', 'Payal', '1985-08-15', 'Tester', 35000, '2011-06-13'),
('E104', 'Kiran', '1990-04-20', 'Programmer', 40000, '2014-03-07'),
('E105', 'Meenal', '1983-05-29', 'DBA', 50000, '2011-12-09'),
('E106', 'Sheila', '1970-05-01', 'Analyst', 60000, '2018-09-25'),
('E107', 'Swamy', '1985-01-13', 'Programmer', 45000, '2016-02-14'),
('E108', 'Sushma', '1976-12-22', 'DBA', 45000, '2012-01-31');
SELECT * FROM EmpDetails;
Output:
b) Write a SQL query to display employees who have joined after 2014.
SELECT * FROM EmpDetails
WHERE DOJ > '2014-12-31';
Output:
c) Write a SQL query to display all the employees whose name ends with ‘a’.
SELECT * FROM EmpDetails
WHERE Ename LIKE '%a';
Output:
d) Write a SQL query to display the total salary of all the employees whose designation is
programmer.
SELECT SUM(Salary) AS Total_Programmer_Salary
FROM EmpDetails
WHERE Designation = 'Programmer';
Output:
e) Write a SQL query to display all the employee names in upper case.
SELECT UPPER(Ename) AS Upper_Name
FROM EmpDetails;
Output:
f) Write a SQL query to display the details of the employee with highest experience.
SELECT * FROM EmpDetails
ORDER BY DOJ ASC
LIMIT 1;
Output:
g) Write a SQL query to display the details of the employees whose name contains ‘ee’.
SELECT * FROM EmpDetails
WHERE Ename LIKE '%ee%';
Output:
h) Write a SQL query to increase the salaries of employees by 5000 whose designation is
DBA.
UPDATE EmpDetails
SET Salary = Salary + 5000
WHERE Designation = 'DBA';
Output:
i) Write sql query to display the employees whose salary is more than the average salary of
all the employees.
SELECT * FROM EmpDetails
WHERE Salary > (
SELECT AVG(Salary) FROM EmpDetails
);
Output:
j) Write sql query to display the record in the following format: xxxxxxxxx is working as
xxxxxxxxxxxxxx with a Salary of Rs.xxxxxxxx eg: Suma is working as Designer with a
Salary of Rs. 20000
SELECT CONCAT(Ename, ' is working as ', Designation, ' with a Salary of Rs. ', Salary) AS
Description
FROM EmpDetails;
Output: