Final Mock Ques 51 Ans
Final Mock Ques 51 Ans
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);
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
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
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;
user_id username
1 John Doe
2 Jane Don
3 Alice Jones
4 Lisa Romero
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?)
-->select e1.sal
from (select e.*,rownum r
from (select hiredate
from emp
order by hiredate desc)e)e1
where r<=10;
Col1 Col2
1 0
0 1
0 1
0 1
1 0
0 1
1 0
1 0
or
-->UPDATE your_table_name
SET Col2 = 1 - Col1;
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
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;
or
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;
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;
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
);
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);
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(+);
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';
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;