DBMS LAB Assignment 1new
DBMS LAB Assignment 1new
INFORMATION TECHNOLOGY OF
Submitted By: Nilesh kumar kamal
Roll no-2021UG1060
Branch: CSE
Group: A
Semester: V
Course: DBMS
Course Code: CS 3001
Submitted To: Dr. Nidhi Kushwaha
LAB ASSIGNMENT 1:
Creating table Employee:
CREATE TABLE Employee (
EMPLOYEE_ID NUMBER,
FIRST_NAME CHAR(50),
LAST_NAME CHAR(50),
SALARY NUMBER,
JOINING_DATE CHAR(50),
DEPARTMENT CHAR(50)
);
insert into Employee values (001, 'Monika', 'Arora', 100000, '2014-02-20 09:00:00','HR');
insert into Employee values (002, 'Niharika', 'Verma', 80000, '2014-06-11 09:00:00','Admin');
insert into Employee values (003, 'Vishal', 'Singhal', 300000, '2014-02-20 09:00:00', 'HR');
insert into Employee values (004, 'Amitabh', 'Singh', 500000, '2014-02-20 09:00:00', 'Admin');
insert into Employee values (005, 'Vivek', 'Bhati', 500000, '2014-06-11 09:00:00', 'Admin');
insert into Employee values (006, 'Vipul', 'Diwan', 200000, '2014-06-11 09:00:00', 'Account');
insert into Employee values (007, 'Satish', 'Kumar', 75000, '2014-01-20 09:00:00', 'Account');
insert into Employee values (008, 'Geetika', 'Chauhan', 90000, '2014-04-11 09:00:00', 'Admin');
For the tables created above, write the SQL queries for the
following:
• Write an SQL query to determine the nth (say n=5) highest salary from a table.
SELECT DISTINCT SALARY
FROM Employee E1
WHERE 5 = (
SELECT COUNT(DISTINCT SALARY)
FROM Employee E2
WHERE E1.SALARY <= E2.SALARY
);
• Write an SQL query to determine the 5th highest salary without using the TOP or
limit method.
SELECT DISTINCT SALARY
FROM Employee E1
WHERE 5 = (
SELECT COUNT(DISTINCT SALARY)
FROM Employee E2
WHERE E1.SALARY <= E2.SALARY
);
• Write an SQL query to fetch the list of employees with the same salary.
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY
FROM Employee
WHERE SALARY IN (SELECT SALARY FROM Employee
GROUP BY SALARY HAVING COUNT(*) > 1);
• Write an SQL query to show one row twice in the results from a table.
with sort_tab as(
SELECT * FROM Employee
UNION ALL SELECT *
FROM Employee)
SELECT * FROM sort_tab
WHERE First_Name = 'Vishal'
• Write an SQL query to fetch the departments that have less than five people in
them.
SELECT DEPARTMENT
FROM Employee
GROUP BY DEPARTMENT
HAVING COUNT(*) < 5;
• Write an SQL query to fetch the last five records from a table.
SELECT * FROM (
SELECT * FROM Employee
order by Employee_ID desc)
fetch WHERE ROWNUM<=5;
• Write an SQL query to print the name of employees having the highest salary in
each department.
SELECT E.First_Name,
E.Department FROM Employee
E WHERE E.Salary in (
SELECT max(Salary)
FROM Employee GROUP
BY Department);
• Write an SQL query to fetch departments along with the total salaries paid for each
of them.
SELECT DEPARTMENT, SUM(SALARY) AS TotalSalary
FROM Employee
GROUP BY DEPARTMENT;
• Write an SQL query to fetch the names of employees who earn the highest salary.
SELECT FIRST_NAME, LAST_NAME
FROM Employee
WHERE SALARY = (SELECT MAX(SALARY) FROM Employee);
• Write an SQL query to show the top n (say 10) records of a table.
select * from
employee where
ROWNUM<=10;
• Write an SQL query to fetch “FIRST_NAME” from the EMPLOYEE table using
the alias name.
SELECT FIRST_NAME AS EMPLOYEE_NAME FROM Employee;
• Write an SQL query to fetch “FIRST_NAME” from the Employee table in upper
case.
SELECT UPPER(FIRST_NAME) AS UPPER_CASE_FIRST_NAME
FROM Employee;
• Write an SQL query to fetch unique values of DEPARTMENT from the Employee
table.
SELECT DISTINCT DEPARTMENT
FROM Employee;
• Write an SQL query to print the first three characters of FIRST_NAME from the
Employee table.
SELECT SUBSTR(FIRST_NAME, 1, 3) AS FIRST_THREE_CHARACTERS
FROM Employee;
• Write an SQL query to find the position of the alphabet (‘a’) in the first name
column ‘Amitabh’ from the Employee table.
SELECT INSTR(FIRST_NAME, 'a') AS
POSITION_OF_A FROM Employee
WHERE FIRST_NAME = 'Amitabh';
• Write an SQL query that fetches the unique values of DEPARTMENT from the
Employee table and prints its length.
SELECT DEPARTMENT, LENGTHB(
TRIM(DEPARTMENT)) AS DEPARTMENT_LENGTH
FROM Employee
• Write an SQL query to print the FIRST_NAME from the Employee table after
replacing ‘a’ with ‘A’.
SELECT REPLACE(FIRST_NAME, 'a', 'A') AS UPDATED_FIRST_NAME
FROM Employee;
• Write an SQL query to print the FIRST_NAME and LAST_NAME from the
Employee table into a single column COMPLETE_NAME. A space char should
separate them.
SELECT FIRST_NAME || ' ' || LAST_NAME
AS COMPLETE_NAME
FROM Employee;
• Write an SQL query to print all Employee details from the Employee table order by
FIRST_NAME Ascending and DEPARTMENT Descending.
SELECT *
FROM Employee
ORDER BY FIRST_NAME ASC, DEPARTMENT DESC;
• Write an SQL query to print details of employee excluding first names, “Vipul” and
“Satish” from the Employee table.
SELECT *
FROM Employee
WHERE FIRST_NAME NOT IN ('Vipul', 'Satish');
• Write an SQL query to print details of the Employees whose SALARY lies between
100000 and 500000.
SELECT *
FROM Employee
WHERE SALARY BETWEEN 100000 AND 500000;
• Write an SQL query to print details of the Employees who joined in Feb’2014.
SELECT *
FROM Employee
WHERE JOINING_DATE BETWEEN ('2014-02-01') AND ('2014-02-28');
• Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM Employee
WHERE SALARY BETWEEN 50000 AND 100000;
• Write an SQL query to print details of the Employees who are also Managers.
SELECT e.*
FROM Employee e
JOIN Category c ON e.EMPLOYEE_ID = c.EMPLOYEE_REF_ID
WHERE c.EMPLOYEE_TITLE = 'Manager';
• Write an SQL query to show records from one table that another table does not
have.
SELECT e.*
FROM Employee e
LEFT JOIN Profit p ON e.EMPLOYEE_ID = p.EMPLOYEE_REF_ID
WHERE p.EMPLOYEE_REF_ID IS NULL;
• Write an SQL query to fetch the count of employees working in the department
‘Admin’
SELECT COUNT(*)
FROM Employee
WHERE DEPARTMENT = 'Admin';