You are on page 1of 3

Write SQL Query to display current date.

Ans: SQL has built in function called GetDate () which returns current timestamp
What type of Joins have you used?
Ans: The knowledge of Joins is a MUST for every interviewee. Most SQL programmers have
used inner join and (left/right) outer join; but the catch point here is to also mention cross join
and self-join.
How will you find the 3
rd
max salary in the employment
table?
Ans: Select distinct salary from employment e1 where 3= (select count (distinct salary) from
employment e2 where e1.salary<=e2.salary)
How would apply date range filter?
Ans: One can use simple conditions like >= and <=, or use between/and but the trick here is to
know your exact data type.
Sometimes date fields contain time and that is where the query can go wrong so it is
recommended to use some date related functions to remove the time issue. In SQL Server
common function for accomplishing the task datediff () function.
Interviewees also have to be aware of different time zones and server time zone.
To increase query performance you may still want to use between however you should be
aware of proper format you should use if not it might misbehave during filtering.

Select x,y,z
From A left Outer join B
On A.x = B.x
Where...
How to generate row number in SQL Without ROWNUM

Generating a row number that is a running sequence of numbers for each row is not easy using
plain SQL. In fact, the method I am going to show below is not very generic either. This method
only works if there is at least one unique column in the table. This method will also work if there
is no single unique column, but collection of columns that is unique. Anyway, here is the query:
SELECT name, sal, (SELECT COUNT(*) FROM EMPLOYEE i WHERE o.name >= i.name)
row_num
FROM EMPLOYEE o
order by row_num

How to select first 5 records from a table?

In SQL Server,
SELECT TOP 5 * FROM EMP;


Date Functions in SQL Server

Select DateDiff (d/m,D1,D2)
From A

Select DatePart(mm/yyyy,StartDateTime)
From [SBI-Dataset].dbo.Appointment

Select CONVERT(VARCHAR(10),GETDATE(),10/11/12)

SELECT GETDATE() AS CurrentDateTime

SELECT OrderId,DATEADD(day,45,OrderDate) AS OrderPayDate
FROM Orders

Group By
select *
from dbo.employee
EName ECity
Joe NewYork
Sunil India
Alex Russia
Alex Russia
Ivan Russia
Albert Canada
Jack New York

Without Aggregation:
select Ename
from dbo.employee
group by Ename

Albert
Alex
Ivan
Jack
Joe
Sunil

select Ecity, Ename
from dbo.employee
group by Ecity, Ename

Canada Albert
India Sunil
New York Jack
NewYork Joe
Russia Alex
Russia Ivan

You might also like