0% found this document useful (0 votes)
20 views9 pages

Final Mock Ques 51 Ans

The document contains a series of SQL queries addressing various database operations, including updating employee salaries, finding average salaries, deleting duplicates, and retrieving specific employee details. It covers a wide range of SQL functionalities such as joins, aggregations, subqueries, and conditional statements. The queries are structured to demonstrate practical applications of SQL in managing employee and training data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Final Mock Ques 51 Ans

The document contains a series of SQL queries addressing various database operations, including updating employee salaries, finding average salaries, deleting duplicates, and retrieving specific employee details. It covers a wide range of SQL functionalities such as joins, aggregations, subqueries, and conditional statements. The queries are structured to demonstrate practical applications of SQL in managing employee and training data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1)Update the salary of employees to be 10% higher if they have been with the

company for more than 5 years.


--> update emp set sal=sal+sal*10/100 where
round(months_between(current_Date,hiredate)/12) >5;

2)Update NULL values in a certain column with the average value for that column.
-->UPDATE your_table
SET your_column = (SELECT AVG(your_column) FROM your_table)
WHERE your_column IS NULL;

3)Find the average salary, excluding NULL values, for each department.
-->SELECT department, AVG(salary) AS average_salary
FROM employees
WHERE salary IS NOT NULL
GROUP BY department;

4)Create a query to find the departments where the average salary is greater than
the company's average salary.
--> select dname
from emp,dept
where dept.deptno=emp.deptno
group by dname
having avg(sal) >(select avg(sal) from emp);

5)display deptname,no. of employees with max salary of each dept if at least


2 managers working in each department.
-->select dname,count(*),max(sal)
from emp
where job='MANAGER'
group by deptno
having count(*)>=2;

6)delete duplicates from employee table.


-->delete from emp where rowid not in (Select max(rowid) from emp
group by empno,ename,sal,comm,deptno);

7)Display employee name who dont have manager without using is null.
-->select e.ename from emp e
where e.mgr not in (Select m.empno from emp m where e.mgr=m.empno);

or

select ename from emp


where nvl(mgr,0)=0;

8)find the employees who were hired before their managers.


--> select e.ename
from emp e,emp m
where e.mgr=m.empno and
e.hiredate<m.hiredate;

9)calculate the avg(sal) for employees in each department excluding the highest and
lowest salary of each dept .
--> select avg(Sal),deptno
from emp
where (sal,deptno) not in (select max(Sal),deptno from emp group by deptno)
and (sal,deptno) not in (select min(sal),deptno from emp group by deptno)
group by deptno
10)waqtd details of employee who is earning 2nd highest salary.
--> select * from emp
where sal =(select max(Sal) from emp
where sal<(Select max(Sal) from emp));

or

--> select * from emp e1


where 2=(select count(distinct e2.sal) from emp e2 where e1.sal<=e2.sal);

11)Write a SQL statement to update the salary of employees


in the "10" department to increase it by 10%,
but only for those who have been with the company for more than 5 years. (repeated)
--> update emp set sal=sal+10/100 where
round(months_between(current_Date,hiredate)/12) >5;

12)display duplicate records from the employee table.


--> select * from emp
where rowid not in (Select max(rowid) from emp
group by empno,ename,sal,job,deptno);

13)WAQTD employee name and department name ,where more number of employees are
working based on each dept.
-->select ename,dname
from emp,dept
where emp.deptno=dept.deptno
and deptno in (Select deptno from emp group by deptno having count(*)=
(select max(count(*)) from emp group by deptno));

14)WA syntax to retreive the data present in first table but not in second table.
--> select * from table-1
minus
select * from table-2;

15)WAQTD top 10 highest salraies from the employee table.


--> select e1.sal
from (select e.*,rownum r
from (select distinct sal
from emp
order by sal desc)e)e1
where r<=10;

16)SELECT * FROM users;

user_id username
1 John Doe

2 Jane Don

3 Alice Jones

4 Lisa Romero

SELECT * FROM training_details;

user_training_id user_id training_id training_date


1 1 1 "2015-08-02"
2 2 1 "2015-08-03"
3 3 2 "2015-08-02"
4 4 2 "2015-08-04"
5 2 2 "2015-08-03"
6 1 1 "2015-08-02"
7 3 2 "2015-08-04"
8 4 3 "2015-08-03"
9 1 4 "2015-08-03"
10 3 1 "2015-08-02"
11 4 2 "2015-08-04"
12 3 2 "2015-08-02"
13 1 1 "2015-08-02"
14 4 3 "2015-08-03"

Write a query to to get the list of users who took the a training lesson more than
once in the same day,
grouped by user and training lesson, each ordered from the most recent lesson date
to oldest date.

--> SELECT
u.user_id,
username,
training_id,
training_date,
count( user_training_id ) AS count
FROM users u JOIN training_details t
ON t.user_id = u.user_id
GROUP BY u.user_id,
username,
training_id,
training_date
HAVING count( user_training_id ) > 1
ORDER BY training_date DESC;

17)Given a table dbo.users where the column user_id is a unique numeric identifier,

how can you efficiently select the first 100 odd user_id values from the table?
(Assume the table contains well over 100 records with odd user_id values.)

SELECT user_id
FROM (
SELECT user_id
FROM dbo.users
WHERE MOD(user_id, 2) <> 0 -- Filter for odd user_id values
ORDER BY user_id -- Order by user_id to ensure consistent results
)
WHERE ROWNUM <= 100; -- Select the first 100 rows

18)What will be the output of the below query, given an Employee table having 10
records?
-->TRUNCATE TABLE Employees
ROLLBACK
SELECT * FROM Employees;

output :--> no rows selected ,bcz truncate cannot restore the records and once we
use truncate its permanent
change to the database. (tcl commands are not used for ddl )

19)What will be the output of the below query, given an Employee table having 10
records?
--> delete from employees;
alter table employees add email varchar2(10);
select * from employees;
-->(how many records are there in employee table now?)

output:-> no rows selected .


-->beacuse after dml changes if we do any other ddl changes ,the dml operations
become permanent.

20)WAQTD 10 latest hired employee details .

-->select e1.sal
from (select e.*,rownum r
from (select hiredate
from emp
order by hiredate desc)e)e1
where r<=10;

21)Write a query to insert/update Col2’s values to look exactly opposite to Col1’s


values.

Col1 Col2
1 0
0 1
0 1
0 1
1 0
0 1
1 0
1 0

--> UPDATE your_table_name


SET Col2 = CASE WHEN Col1 = 1 THEN 0 ELSE 1 END;

or

-->UPDATE your_table_name
SET Col2 = 1 - Col1;

22)Given the table mass_table:

weight
5.67
34.567
365.253
34
Write a query that produces the output:

weight kg gms
5.67 5 67
34.567 34 567
365.253 365 253
34 34 0

--> select weight, trunc(weight) as kg, nvl(substr(weight - trunc(weight), 2), 0)


as gms
from mass_table;
23)How do you copy data from one table to another table?

-->INSERT INTO table-name (column1, column2, ...)


SELECT column1, column2, ...
FROM source_table;

24)delete duplicate records with one field


and
delete duplicate records with more than one field

-->DELETE FROM your_table


WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM your_table
GROUP BY field_name
);

--> DELETE FROM your_table


WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM your_table
GROUP BY field1, field2, ..., fieldN
);

25) Write an SQL query to fetch all the Employees who are also managers from the
EmployeeDetails table.
--> select distinct m.ename
from emp e,emp m
where e.mgr=m.empno ;
or
select distinct e.ename
from emp e,emp m
where e.empno=m.mgr;

26)WAQTD alternate records from the employee table.


--> select * from (Select emp.*,rownum from emp )
where mod(r,2)=0;

or

-->select * from (Select emp.*,rownum from emp )


where mod(r,2)=1;

27)Write a query to find employees who earn more than their managers.
--> select e.ename
from emp e,emp m
where e.mgr=m.empno and e.sal>m.sal;

28)Consider a BookCheckout table with columns – CheckoutID, MemberID, BookID,


CheckoutDate, ReturnDate.
Write an SQL query to find the number of books checked out by each member.
-->SELECT MemberID, COUNT(*) AS NumberOfBooksCheckedOut
FROM BookCheckout
GROUP BY MemberID;

29)Consider a StudentGrades table with columns – StudentID, CourseID, Grade.


Write a query to find students who have scored an ‘A’ in more than three courses.
Here we will write an SQL query that filters students who have received an ‘A’
grade
and groups them by StudentID, counting the number of ‘A’ grades per student.

--> SELECT StudentID FROM StudentGrades


WHERE Grade = 'A'
GROUP BY StudentID
HAVING COUNT(*) > 3;

30)How would you retrieve employee information when the employee has not been
assigned to a department?
--> select ename from emp
where deptno not in (Select deptno from dept
where emp.deptno=dept.deptno);

31)Show the number of employees who joined in the past three years.
--> SELECT COUNT(*) AS nu
FROM emp
WHERE hiredate >= ADD_MONTHS(TRUNC(SYSDATE), -36)

32)Find the employee who has been with the company the longest: (display complete
detials )
-->SELECT *
FROM (
SELECT *
FROM emp
ORDER BY hiredate
)
WHERE ROWNUM = 1;

33)Calculate employee tenure in years:


--> select ename,hiredate,round(months_between(current_date,hiredate)/12)
from emp;

34)display current age ,with the day if born.(wednesday/thursday/...)


--> select to_char(to_Date('10-JUN-1994'),'day') from dual;

35)In a Table having duplicate email id,show only the records having duplicate
email.
-->SELECT *
FROM your_table
WHERE email IN (
SELECT email
FROM your_table
GROUP BY email
HAVING COUNT(*) > 1
);

36)WAQT fetch top 5 employees based on empno.


-->SELECT *
FROM (SELECT *
FROM emp
ORDER BY empno)
WHERE ROWNUM <= 5;

37)explain the execution procces of the query (Select to order by including


distinct in select)
--> from
where
group by
having
select distinct
order by

38)Find the employee details, who is earning lowest salary.


--> select * from emp
where sal=(Select min(Sal) from emp);

39)WAQTD dname and ename of employees who are earning highest sal in each dept.
--> select dname,ename
from emp ,dept
where emp.deptno=dept.deptno
and (sal,deptno) in (Select max(Sal),deptno from emp group by deptno);

40)WAQ to delete delete 10th record from the employee table.?


--> delete from emp e1
where 10 =(Select count(e2.rowid) from emp e2
where e1.rowid>e2.rowid);

41)WAQT to fetch first half of the records from employee table.


--> select *
from emp
where rownum<=(select round(count(*)/2) from emp);

42)WAQT to display employee name and their manager name and location of the
manager.
--> select e.ename,m.ename,d1.loc
from emp e,emp m ,dept d
where e.mgr=m.empno and m.deptno=d.deptno;

43)WAQT to employee name and department name ,also employee name who dont work in
any department.
--> select e.ename,d.dname
from emp e,dept d
where e.deptno=d.deptno(+);

44)WAQTD average salary,maximum salary and minimum salary of employee ,


if at least 3 employees working in each dept as CLERK.
--> select avg(Sal),max(sal),min(sal)
from emp
where job='CLERK'
group by deptno
having count(*)>=3;

45)create syntax of view and materialized view.


-->create materialized view mv-name as select stmnt;

46)WAQTD employee name and sal along with manager name and his salary,
also check employee salary is more than his manager salary, and employee
works in new york.
--> select e.ename,e.sal,m.ename,m.sal
from emp e,emp m ,dept d
where e.mgr=m.empno and e.sal>m.sal and e.deptno=d.deptno and d.loc='NEW YORK';

47)Display employee details ,who earns same salary.


--> select * from emp
where sal in (Select sal from emp group by sal having count(*)>1);

48)How would you view the month-wise maximum salary for a specific department?
--> SELECT
TO_CHAR(hiredate, 'MM') AS month,
MAX(sal) AS max_salary
FROM
emp
WHERE
deptno=20
GROUP BY
TO_CHAR(hiredate, 'MM')
ORDER BY
TO_CHAR(hiredate, 'MM')

49)How would you find the previous month’s last day with a query?
--> select last_day(add_months(current_Date,-1)) from emp;

50)How do you read the last five records from a database using a SQL query?
--> select *
from (select emp.*
from emp
order by rownum desc)
where rownum<=10;

or

select *
from (select emp.* ,rownum r
from emp
order by r desc)
where rownum<=10;

51)How do you view only common records from two different tables?
--> select * from table-1
intersect
select * from table-2;

You might also like