Complex Queries in SQL
Complex Queries in SQL
7. List dept no., Dept name for all the departments in which there are no
employees in the department.
select * from dept where deptno not in (select deptno from emp);
alternate solution: select * from dept a where not exists (select * from emp b
where a.deptno = b.deptno);
altertnate solution: select empno,ename,b.deptno,dname from emp a, dept b
where a.deptno(+) = b.deptno and empno is null;
14. Suppose there is annual salary information provided by emp table. How to
fetch monthly salary of each and every employee?
15. Select all record from emp table where deptno =10 or 40.
16. Select all record from emp table where deptno=30 and sal>1500.
17. Select all record from emp where job not in SALESMAN or CLERK.
19. Select all records where ename starts with S and its lenth is 6 char.
20. Select all records where ename may be any no of character but it should
end with R.
select * from emp where sal> any(select sal from emp where sal<3000);
select * from emp where sal> all(select sal from emp where sal<3000);
25. Select all the employee group by deptno and sal in descending order.
26. How can I create an empty table emp1 with same structure as emp?
28. Select all records where dept no of both emp and dept table matches.
select * from emp where exists(select * from dept where
emp.deptno=dept.deptno)
29. If there are two tables emp1 and emp2, and both have common record. How
can I fetch all the recods but common records only once?
(Select * from emp) Union (Select * from emp1)
30. How to fetch only common records from two tables emp and emp1?
(Select * from emp) Intersect (Select * from emp1)
31. How can I retrive all records of emp1 those should not present in emp2?
(Select * from emp) Minus (Select * from emp1)
32. Count the totalsa deptno wise where more than 2 employees exist.
SELECT deptno, sum(sal) As totalsal
FROM emp
GROUP BY deptno
HAVING COUNT(empno) >
Question 1: What are the results returned from the
T-SQL below?
SELECT 1, 3
UNION ALL
SELECT 2, NULL
UNION ALL
SELECT 3, 3;
SELECT
COUNT(*) AS CountTotal
, COUNT(second_id) AS CountSecondId
, COUNT(DISTINCT second_id) AS CountDistinctSecondId
FROM a;
Answer 1:
3,2,1
(Because Count(Column_Name) doesnt consider NULL Value)
***************************************************
Answer 2:
As races table contains the record with NULL value the Query will not
yield the desired result
Correct Query: select * from runners where id not in (select
winner_id from races where winner_id is not null)
***************************************************
Question 3:
What will be the result of the query below? Explain your answer and
provide a version that behaves correctly.
select case when null = null then 'Yup' else 'Nope' end as Result;
Answer 3:
Result: Nope
***************************************************
Question 4:
Given a table, such as the one below, that hasm = male and f = female
Swap all f and m values (i.e., change all f values to m and vice versa)
with a single update query and no intermediate temp table.
1 A m 25002
2 B f 15003
3 C m 55004
4 D f 500
Answer 4:
Update table
Set Sex=(Case Sex when m then f else m end)
***************************************************
Question 5:
Assume a schema of
If there are 10 records in the Emp table and 5 records in the Dept table,
how many rows will be displayed in the result of the following SQL
query:
Select * From Emp, Dept
Answer 5:
Would be 17*3=51
***************************************************
Question 6:
Answer 6:
The where clause cannot read the name given to the column in the Select
clause because the execution of the Query takes place in following order
From
Where
Groub by
Having
Select
Order by
Correct Query:
SELECT Id, YEAR(BillingDate) AS BillingYear
FROM Invoices
WHERE YEAR(BillingDate) >= 2010;
***************************************************
Question 7:
***************************************************
Question 8:
Answer 8:
Select
sum(case when num > 0 then num else null end) pos,
sum(case when num < 0 then num else null end) neg
from temp
***************************************************
Question 9:
you have to insert the records dynamically determining the page range
between the StartPg and EndPg from the first table i.e. [dbo].[Policy]
and insert the page numbers individually for each policy number in
second table i.e. [dbo].[PolicyPg]
Answer 9:
***************************************************
Question 10:
1 23 32 44 55
2 44 65 78 24
3 67 99 22 56
4 12 15 18 19
Answer 10:
I can think again of making use of CTE expression in this case, but
experts out there can follow any approach which they feel is more
simple and easy to write
;With tabB as
(Select ColA,ColB,ColC,ColD,ROW_NUMBER() over(order by
ColA) as RowNum from TabA),
tabC as
(Select RowNum,ColA as Col from tabB
union all
Select RowNum,ColB from tabB
union all
Select RowNum,ColC from tabB
union all
Select RowNum,ColD from tabB),
tabD as
(Select B.RowNum,ColA,ColB,ColC,ColD,max(Col) as MaxVal from
tabB B,tabC C where B.RowNum=C.RowNum group by
ColA,ColB,ColC,ColD,B.RowNum)
***************************************************
Question 11:
A. INNER JOIN
Answer A:
ColumnA ColumnB
1 1
1 1
Select * from
dbo.tableA A
left join tableB B on A.ColumnA=B.ColumnB
Answer B:
ColumnA ColumnB
1 1
1 1
2 NULL
NULL NULL
Answer C:
ColumnA ColumnB
1 1
1 1
NULL NULL