Lab Project of SQL
Q-1. Write an SQL query to fetch "FIRST_NAME" from Worker table using
the alias name as <WORKER_NAME>.
Query:- Select FIRST_NAME as WORKER_NAME from Worker;
Output:-
Q-2. Write an SQL query to fetch "FIRST_NAME" from Worker table in
upper case.
Query:-Select upper (FIRST NAME) from Workers;
Output:-
Q-3. Write an SQL query to fetch unique values of DEPARTMENT from
Worker table.
Query:-Select distinct DEPARTMENT from Worker;
Output:-
Q-4. Write an SQL query to print the first three characters of FIRST_NAME
from Worker table.
Query:-Select substring (FIRST NAME,1,3) from Workers;
Output:-
Q-5. Write an SQL query to find the position of the alphabet ('a') in the
first name column 'Amitabh' from Worker table.
Query:-Select INSTR (FIRST NAME, BINARY 'a') from Worker where
FIRST_NAME= "SATISH';
Output:-
Q-6. Write an SQL query to print the FIRST_NAME from Worker table after
removing white spaces from the right side.
Query:-Select RTRIM (FIRST NAME) from Worker;
Output:-
Q-7. Write an SQL query to print the DEPARTMENT from Worker table after
removing white spaces from the left side.
Query:-Select LTRIM(DEPARTMENT) from Workers;
Output:-
Q-8. Write an SQL query that fetches the unique values of DEPARTMENT
from Worker table and prints its length.
Query:-Select distinct length (DEPARTMENT) from Worker;
Output:-
Q-9. Write an SQL query to print the FIRST_NAME from Worker table after
replacing 'a' with 'A'.
Query:-Select REPLACE (FIRST NAME, 'A', 'a') from Worker;
Output:-
Q-10. Write an SQL query to print the FIRST_NAME and LAST_NAME from
Worker table into a single column COMPLETE_NAME. A space char should
separate them.
Query:-Select CONCAT (FIRST_NAME, ‘ ‘, LAST_NAME) AS
‘COMPLETE_NAME’ FROM WORKER;
Output:-
Q-11. Write an SQL query to print all Worker details from the Worker table
order by FIRST_NAME Ascending.
Query:-Select * from Worker order by FIRST_NAME asc;
Output:-
Q-12. Write an SQL query to print all Worker details from the Worker table
order by FIRST_NAME Ascending and DEPARTMENT Descending.
Query:-Select * from Worker order by FIRST NAME asc, DEPARTMENT
desc;
Output:-
Q-13. Write an SQL query to print details for Workers with the first name
as "Vipul" and "Satish" from Worker table.
Query:-Select * from Worker where FIRST_NAME in (‘SATISH’,’PUNIT’);
Output:-
Q-14. Write an SQL query to print details of workers excluding first names,
"Vipul" and "Satish" from Worker table.
Query:-Select * from Worker where FIRST NAME not to ('Vipul, Satish');
Output:-
Q-15. Write an SQL query to print details of Workers with DEPARTMENT
name as "Admin".
Query:-Select * from Worker where DEPARTMENT like 'Admins';
Output:-
Q-16. Write an SQL query to print details of the Workers whose FIRST
NAME contains 'a'.
Query:-Select * from Worker where FIRST_NAME like ‘%a%’;
Output:-
Q-17. Write an SQL query to print details of the Workers whose
FIRST_NAME ends with 'a'.
Query:-Select * from Worker where FIRST_NAME like '%a';
Output:-
Q-18. Write an SQL query to print details of the Workers whose
FIRST_NAME ends with 'h' and contains six alphabets.
Query:-Select * from Worker where FIRST NAME like ‘h’;
Output:-
Q-19. Write an SQL query to print details of the Workers whose SALARY
lies between 50000 and 100000.
Query:-Select * from Worker where SALARY between 50000 and 100000;
Output:-
Q-20. Write an SQL query to print details of the Workers who have Joined
in Feb'2026.
Query:-Select * from Worker where year (JOINING_DATE) = 2026 and
month (JOINING_DATE) = 2;
Output:-
Q-21. Write an SQL query to fetch the count of employees working in the
department 'Admin'.
Query:-SELECT COUNT (*) FROM worker WHERE DEPARTMENT = ‘Admin’;
Output:-
Q-22. Write an SQL query to fetch worker names with salaries >= 50000
and <= 100000.
Query:-SELECT CONCAT (FIRST_NAME, ‘ ‘ , LAST_NAME) As Worker_Name,
Salary FROM worker WHERE WORKER_ID IN
(SELECT WORKER_ID FROM worker WHERE Salary BETWEEN 50000 AND
100000);
Output:-
Q-23. Write an SQL query to fetch the no. of workers for each department
in the descending order.
Query:-SELECT DEPARTMENT, count (WORKER ID) No_Of_Workers FROM
worker
GROUP BY DEPARTMENT
ORDER BY No_Of_Workers DESC;
Output:-
Q-24. Write an SQL query to print details of the Workers who are also
Managers.
Query:-SELECT DISTINCT W.FIRST_NAME, T.WORKER_TITLE
FROM Worker W
INNER JOIN Title T
ON W.WORKER_ID = T.WORKER_REF_ID AND T.WORKER TITLE in
('Manager');
Output:-
Q-25. Write an SQL query to fetch duplicate records having matching data
in some fields of a table.
Query:-SELECT WORKER_TITLE, AFFECTED_FROM, COUNT(*)
FROM Title
GROUP BY WORKER_TITLE, AFFECTED_FROM
HAVING COUNT (*) > 1;
Output:-
Q-26. Write an SQL query to show only odd rows from a table.
Query:-SELECT * FROM Worker WHERE MOD (WORKER ID, 2) <> 0;
Output:-
Q-27. Write an SQL query to show only even rows from a table.
Query:-SELECT * FROM Worker WHERE MOD (WORKER ID, 2) = 0;
Output:-
Q-28. Write an SQL query to clone a new table from another table.
Query:-SELECT * FROM WorkerClone;
Output:-
Q-29. Write an SQL query to fetch intersecting records of two tables.
Query:-(SELECT * FROM Worker) INTERSECT (SELECT * FROM
WorkerClone);
Output:-
Q-30. Write an SQL query to show records from one table that another
table does not have.
Query:-SELECT * FROM Worker not in SELECT * FROM Title;
Output:-
Q-31. Write an SQL query to show the current date and time.
Query:-SELECT CURDATE();
Output:-
Query:-SELECT NOW();
Output:-
Q-32. Write an SQL query to show the top n (say 8) records of a table.
Query:-SELECT * FROM Worker ORDER BY Salary DESC LIMIT 8;
Output:-
Q-33. Write an SQL query to determine the nth (say n=5) highest salary
from a table. Ans. The following MySQL query returns the nth highest
salary:
Query:- select distinct salary from worker order by salary desc limit 1
offset 5;
Output:-
The following SQL Server query returns the nth highest salary:
Query:- set @n = 5;
set @offset = @n-1;
prepare stmt from 'select distinct salary from worker order by salary desc
limit 1 offset?';
execute stmt using @offset;
Output:-
Q-34. Write an SQL query to determine the 5th highest salary without
using TOP or limit method.
Query:-SELECT Salary FROM Worker W1 WHERE 4 = (SELECT
COUNT( DISTINCT( W2.Salary ) ) FROM Worker W2 WHERE W2.Salary >=
W1.Salary );
Output:-
Use the following generic method to find nth highest salary without using
TOP or limit.
Query:- Set @n=5;
SELECT Salary FROM Worker W1 WHERE @n-1 = (SELECT
COUNT( DISTINCT( W2.Salary ) ) FROM Worker W2 WHERE W2.Salary >=
W1.Salary );
Output:-
Q-35. Write an SQL query to fetch the list of employees with the same
salary.
Query:-Select distinct W.WORKER_ID, W.FIRST_NAME, W.Salary from
Worker W, Worker W1 where W.Salary = W1.Salary and W.WORKER_ID !=
W1.WORKER_ID;
Output:-
Q-36. Write an SQL query to show the second highest salary from a table.
Query:-Select max(Salary) from Worker where Salary not in (Select
max(Salary) from Worker);
Output:-
Q-37. Write an SQL query to show one row twice in results from a table.
Query:- select FIRST_NAME, DEPARTMENT from worker W where
W.DEPARTMENT='HR' union all select FIRST_NAME, DEPARTMENT from
Worker W1 where W1.DEPARTMENT='HR';
Output:-
Q-38. Write an SQL query to fetch intersecting records of two tables.
Ans. The required query is:
Query:- (SELECT * FROM Worker) INTERSECT (SELECT * FROM
WorkerClone);
Output:-
Q-39. Write an SQL query to fetch the first 50% records from a table.
Query:-SELECT * FROM WORKER WHERE WORKER_ID <= (SELECT
count(WORKER_ID)/2 from Worker);
Output:-
Q-40. Write an SQL query to fetch the departments that have less than
five people in it. Ans. The required query is:
Query:- SELECT DEPARTMENT, COUNT(WORKER_ID) as 'Number of
Workers' FROM Worker GROUP BY DEPARTMENT HAVING
COUNT(WORKER_ID) < 5;
Output:-
Q-41. Write an SQL query to show all departments along with the number
of people in there.
Query:-SELECT DEPARTMENT, COUNT(DEPARTMENT) as 'Number of
Workers' FROM Worker GROUP BY DEPARTMENT;
Output:-
Q-42. Write an SQL query to show the last record from a table.
Query:-Select * from Worker where WORKER_ID = (SELECT
max(WORKER_ID) from Worker);
Output:-
Q-43. Write an SQL query to fetch the first row of a table.
Query:-Select * from Worker where WORKER_ID = (SELECT
min(WORKER_ID) from Worker);
Output:-
Q-44. Write an SQL query to fetch the last five records from a table.
Query:-SELECT * FROM Worker WHERE WORKER_ID >=5 UNION SELECT *
FROM (SELECT * FROM Worker W order by W.WORKER_ID DESC) AS W1
WHERE W1.WORKER_ID >=5;
Output:-
Q-45. Write an SQL query to print the name of employees having the
highest salary in each department.
Query:-SELECT t.DEPARTMENT, t.FIRST_NAME, t.Salary from(SELECT
max(Salary) as TotalSalary,DEPARTMENT from Worker group by
DEPARTMENT) as TempNew Inner Join Worker t on
TempNew.DEPARTMENT=t.DEPARTMENT and
TempNew.TotalSalary=t.Salary;
Output:-
Q-46. Write an SQL query to fetch three max salaries from a table.
Query:-SELECT distinct Salary from worker a WHERE 3 >= (SELECT
count(distinct Salary) from worker b WHERE a.Salary <= b.Salary) order
by a.Salary desc;
Output:-
Q-47. Write an SQL query to fetch three min salaries from a table.
Query:-SELECT distinct Salary from worker a WHERE 3 >= (SELECT
count(distinct Salary) from worker b WHERE a.Salary >= b.Salary) order
by a.Salary desc;
Output:-
Q-48. Write an SQL query to fetch nth max salaries from a table.
Query:- set @n=10;
SELECT distinct Salary from worker a WHERE @n >= (SELECT
count(distinct Salary) from worker b WHERE a.Salary <= b.Salary) order
by a.Salary desc;
Output:-
Q-49. Write an SQL query to fetch departments along with the total
salaries paid for each of them.
Query:-SELECT DEPARTMENT, sum(Salary) from worker group by
DEPARTMENT;
Output:-
Q-50. Write an SQL query to fetch the names of workers who earn the
highest salary. Query:-SELECT FIRST_NAME, SALARY from Worker WHERE
SALARY=(SELECT max(SALARY) from Worker);
Output:-