Dbms Assignment
Dbms Assignment
Dbms Assignment
INFORMATION TECHNOLOGY
LAB ASSIGNMENT 1:
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 the second-highest salary from a table.
SELECT MAX(SALARY) AS SecondHighestSalary
FROM Employee
WHERE SALARY < (
SELECT MAX(SALARY)
FROM Employee);
• 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 intersecting records of two tables.
SELECT Employee_ID,
First_Name,
Last_name
Department FROM Employee
intersect select Employee_ID,
First_Name, Last_name
FROM Employee;
• Write an SQL query to fetch the first 50% of records from a table.
SELECT *
FROM Employee
where Employee_ID <= (
SELECT COUNT(Employee_ID)/2
FROM Employee);
• 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 show the last record from a table.
SELECT * FROM (
SELECT * FROM
Employee ORDER BY
Employee_ID DESC)
fetch WHERE ROWNUM=1;
• 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 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 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 Employee with DEPARTMENT name as
“Admin”.
SELECT *
FROM Employee
WHERE DEPARTMENT = 'Admin';
• 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';
INDIAN INSTITUTE OF
INFORMATION TECHNOLOGY
• First reserves and boat then the result should be join with sailors Use the
following concept SELECT * FROM Table1 JOIN Table2 ON
Table1.fielda=Table2.fielda JOIN Table3 ON Table2.fieldb =
Table3.fieldb
SELECT *
FROM Reserves
JOIN Boat ON Reserves.BID = Boat.BID
JOIN Sailors ON Sailors.SID = Reserves.SID;
• Select BNAME which is assigned to at least two BID.
SELECT BNAME
FROM Boat
GROUP BY BNAME
HAVING COUNT(BID) >= 2;
• Apply SQL RIGHT JOIN two tables, the RESERVES table and the BOAT table.
SELECT *
FROM Reserves
RIGHT JOIN Boat ON Reserves.BID = Boat.BID;
• Apply SQL LEFT JOIN two tables, the SAILORS and the RESERVES table.
SELECT *
FROM Sailors
LEFT JOIN Reserves ON Sailors.SID = Reserves.SID;
• Get the complete record (SNAME, DAY) from both tables([SAILORS],
[RESERVERS]), if no match is found in any table, then show NULL.
SELECT SNAME, DAY
FROM Sailors
FULL OUTER JOIN Reserves ON Sailors.SID = Reserves.SID;
• Write a query to find out the SNAME whose BID is 101, and display it.
SELECT SNAME FROM Sailors
JOIN Reserves ON Sailors.SID = Reserves.SID
WHERE Reserves.BID = 101;
• Write down the query to fetch BID which is assigned to the Sailors whose age is
less than 5 years.
SELECT BID
FROM Sailors
JOIN Reserves ON Sailors.SID = Reserves.SID
WHERE Sailors.AGE < 5;
• Apply SQL FULL JOIN on two tables, the RESERVES table and the BOAT
table.
SELECT *
FROM Reserves
FULL OUTER JOIN Boat ON Reserves.BID = Boat.BID;
• Write down the query to fetch SID & COLOR who have the same color boat.
SELECT Sailors.SID, Boat.COLOR
FROM Sailors
JOIN Reserves ON Sailors.SID = Reserves.SID
JOIN Boat ON Reserves.BID = Boat.BID
GROUP BY Sailors.SID, Boat.COLOR
HAVING COUNT(DISTINCT Boat.BID) > 1;
• Find the name and age of sailor who is having red and blue color boat.
SELECT SNAME, AGE
FROM Sailors
JOIN Reserves ON Sailors.SID = Reserves.SID
JOIN Boat ON Reserves.BID = Boat.BID
WHERE Boat.COLOR IN ('Red', 'Blue')
GROUP BY SNAME, AGE
HAVING COUNT(DISTINCT Boat.COLOR) = 2;
• Find the name of boat whose rating is greater than 40.
SELECT BName
FROM Boat
WHERE BID IN (
SELECT BID
FROM Sailors
WHERE RATING > 40
);
• Perform the natural join between sailors and reserves
SELECT *
FROM Sailors
NATURAL JOIN Reserves;
• ON DELETE RESTRICT
CREATE TABLE ParentTable (
attr3 INT PRIMARY KEY
);
This option restricts the deletion of a row in ParentTable if there are any matching
rows in ChildTable.
• ON DELETE NO ACTION
CREATE TABLE ParentTable (
attr3 INT PRIMARY KEY
);
CREATE TABLE ChildTable (
attr1 INT,
attr2_id INT,
FOREIGN KEY (attr1) REFERENCES ParentTable(attr3) ON DELETE NO
ACTION);
This is the default option. It specifies that no action should be taken if a row in
ParentTable is deleted, and it is mainly used for checking referential integrity
without any automatic action.
INDIAN INSTITUTE OF
INFORMATION TECHNOLOGY
• Write a code in PL/SQL to create a trigger that checks for different values in a
specific column and raises an exception if found.
CREATE OR REPLACE TRIGGER check_last_name
BEFORE INSERT OR UPDATE ON Employee
FOR EACH ROW
BEGIN
-- Check for specific values in the 'last_name' column
IF :NEW.last_name= 'INVALID' THEN
-- Raise an exception if 'INVALID' values are found
RAISE_APPLICATION_ERROR(-20001, 'Invalid value in the last_name
column: ' || :NEW.last_name);
END IF;
END;
• Write a code in PL/SQL to create a trigger that checks for duplicate values in a
specific column and raises an exception if found.
CREATE OR REPLACE TRIGGER prevent_duplicates
BEFORE INSERT ON Employee
FOR EACH ROW
DECLARE
v_count NUMBER;
BEGIN
-- Check if the new first_name already exists
SELECT COUNT(*) INTO v_count FROM Employee WHERE first_name =
:NEW.first_name;
-- If duplicate value found, raise an error
IF v_count> 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'first name already exists.');
END IF;
END;
/
• Create a trigger that checks if "value" is greater than 1000 for any new row
inserted to "Test" table, then insert a row to Audit table?
CREATE TABLE Test
(
test_id INTEGER,
value NUMBER,
test_date DATE
);
• Write a PL/SQL function to return the maximum bit length of the Last_Names in
the Employees table.
DECLARE
v_bit_length NUMBER;
v_max_bit_length NUMBER := 0;
BEGIN
FOR emp IN (SELECT Last_Name FROM Employee) LOOP
v_bit_length := LENGTHB(trim(emp.Last_Name)) * 8;
IF v_bit_length > v_max_bit_length THEN
v_max_bit_length := v_bit_length;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Maximum Bit Length: ' || v_max_bit_length);
END;
/
• Write a PL/SQL block to calculate the bit length of the employee's first name in
the employees table for all records.
DECLARE
v_bit_length NUMBER;
BEGIN
FOR emp IN (SELECT Last_Name FROM Employee) LOOP
v_bit_length := LENGTHB(trim(emp.Last_Name)) * 8;
DBMS_OUTPUT.PUT_LINE('Bit length of ' || emp.Last_Name || ': ' ||
v_bit_length);
END LOOP;
END;
/
• Write a PL/SQL block to display the character representation of each ASCII
value in the range of 65 to 90.
DECLARE
v_ascii_value NUMBER;
v_character CHAR(1);
BEGIN
FOR v_ascii_value IN 65..90 LOOP
-- Convert ASCII value to character
v_character := CHR(v_ascii_value);
• Write a PL/SQL block that replaces all occurrences of the substring 'IT_PROG'
with ‘IT PROGRAMMER' in the job titles of employees in the employees table.
Display the updated job titles.
DECLARE
v_job_title Employee.Job_Id%TYPE;
BEGIN
FOR emp IN (SELECT Job_Id FROM Employee) LOOP
-- Check if the current job_title is 'IT_PROG'
IF emp.Job_Id = 'IT_PROG' THEN
-- Update job_title to 'IT_PROGRAMMER'
v_job_title := 'IT_PROGRAMMER';
ELSE
-- Keep the existing job_title if not 'IT_PROG'
v_job_title := emp.Job_Id;
END IF;
BEGIN
UPDATE emp_temp
SET email = 'not available'
WHERE first_name LIKE 'B%';
• Write a program to find the minimum of two values. Here, the procedure takes
two numbers using the IN mode and returns their minimum using the OUT
parameters.
-- Create a PL/SQL procedure to find the minimum of two values
CREATE OR REPLACE PROCEDURE find_minimum(
p_number1 IN NUMBER,
p_number2 IN NUMBER,
p_minimum OUT NUMBER
) AS
BEGIN
-- Check for the minimum of the two values
IF p_number1 < p_number2 THEN
p_minimum := p_number1;
ELSE
p_minimum := p_number2;
END IF;
END;
/
-- Call the procedure to compute the square (passing and returning the same
parameter)
compute_square(v_number);
-- Display the squared result
DBMS_OUTPUT.PUT_LINE('Squared value: ' || v_number);
END;
/
BEGIN
-- Update the salary for each customer
UPDATE Customer
SET SALARY = SALARY + 1000;
BEGIN
-- Open the cursor
OPEN customer_cursor;