Extrea Queries For Practice
Extrea Queries For Practice
create table worker(worker_id number not null primary key ,first_name varchar(30),
last_name varchar(30),salary number, joining_date date, department varchar(10));
--2)Bonus table
--3)Title table
--1)Write an SQL query to fetch �FIRST_NAME� from Worker table using the alias name
as <WORKER_NAME>.
--2)Write an SQL query to fetch �FIRST_NAME� from Worker table in upper case.
--3)Write an SQL query to fetch unique values of DEPARTMENT from Worker table.
select distinct department from worker;
--4)Write an SQL query to print the first three characters of FIRST_NAME from
Worker table.
--5)Write an SQL query to find the position of the alphabet (�a�) in the first name
column �Amitabh� from Worker table.
--6)Write an SQL query to print the FIRST_NAME from Worker table after removing
white spaces from the right side.
--7)Write an SQL query to print the DEPARTMENT from Worker table after removing
white spaces from the left side.
--8)Write an SQL query that fetches the unique values of DEPARTMENT from Worker
table and prints its length.
----------------------or-----------------------
--9)Write an SQL query to print the FIRST_NAME from Worker table after replacing
�a� with �A�.
--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.
------------------OR-------------------------
--12)Write an SQL query to print all Worker details from the Worker table order by
FIRST_NAME Ascending and DEPARTMENT Descending.
--13) Write an SQL query to print details for Workers with the first name as
�Vipul� and �Satish� from Worker table.
--14)Write an SQL query to print details of workers excluding first names, �Vipul�
and �Satish� from Worker table.
--16)Write an SQL query to print details of the Workers whose FIRST_NAME contains
�a�.
--17)Write an SQL query to print details of the Workers whose FIRST_NAME ends with
�a�.
--18)Write an SQL query to print details of the Workers whose FIRST_NAME ends with
�h� and contains six alphabets.
--19)Write an SQL query to print details of the Workers whose SALARY lies between
100000 and 500000.
--20)Write an SQL query to print details of the Workers who have joined in
Feb�2021.
-----------------------------Or-----------------------
Select * from Worker where to_char(JOINING_DATE,'mon-yy') = 'feb-21';
--21)Write an SQL query to fetch the count of employees working in the department
�Admin�.
--22)Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.
--23)Write an SQL query to fetch the no. of workers for each department in the
descending order.
--24)Write an SQL query to print details of the Workers who are also Managers.
--25) Write an SQL query to fetch duplicate records having matching data in some
fields of a table.
create table
workerclone(WORKER_ID,FIRST_NAME,LAST_NAME,SALARY,JOINING_DATE,DEPARTMENT) as
(select WORKER_ID,FIRST_NAME,LAST_NAME,SALARY,JOINING_DATE,DEPARTMENT from
worker);
---------------------
--29)Write an SQL query to fetch intersecting records of two tables.
--30)Write an SQL query to show records from one table that another table does not
have.
--32)Write an SQL query to show the top n (say 10) records of a table.
--33)Write an SQL query to determine the nth (say n=5) highest salary from a table.
select * from(
select FIRST_NAME, salary, dense_rank()
over(order by salary desc)r from worker)
where r=&n;
--34) Write an SQL query to determine the 5th highest salary without using TOP or
limit method.
---------------------------------OR------------------------
SELECT Salary
FROM Worker W1
WHERE 4 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
--35) Write an SQL query to fetch the list of employees with the same salary.
--37. Write an SQL query to show one row twice in results from a table.
--39). Write an SQL query to fetch the first 50% records from a table.
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
--40)Write an SQL query to fetch the departments that have less than five people in
it.
--41. Write an SQL query to show all departments along with the number of people in
there.
--42) Write an SQL query to show the last record from a table.
--44)Write an SQL query to fetch the last five records from a table.
select * from (select * from worker order by worker_id desc) where rownum<=4;
--45) Write an SQL query to print the name of employees having the highest salary
in each department
--46) Write an SQL query to fetch three max salaries from a table.
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;
--48) Write an SQL query to fetch nth max salaries from a table.
--not working
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;
--anater solution--working
select * from(
select FIRST_NAME, salary, dense_rank()
over(order by salary desc)r from worker)
where r=&n;
--49. Write an SQL query to fetch departments along with the total salaries paid
for each of them.
SELECT DEPARTMENT, sum(Salary) from worker group by DEPARTMENT;
--50) Write an SQL query to fetch the names of workers who earn the highest salary.
-- functions in
rdbms*************************************************************************88