You are on page 1of 24

DATABASE INTERVIEW QUESTIONS & ANSWERS

SQL & Type of SQL Statement


SQL is a Structured Query Language that allows you to communicate with a database and handle the
data it contains in all kinds of ways. It consists of Data Definition Language commands, such as
Create, Alter, Truncate and Drop, and Data Manipulation Language commands, such as Select, Insert,
Update and Delete. The most common databases that use SQL as their query language are Oracle,
SQL Server, DB2, and MySQL.

SQL Statement

DML DDL DCL TCL DQL

Insert Create GRANT COMMIT SELECT


Update Alter REVOKE ROLLBACK
Delete DROP
Truncate

WHAT ARE JOINS?

An SQL JOIN clause is used to combine rows from two or more tables, based
on a common field between them.

JOINS

Inner Join Outer Join Cross Join Self Join

Left Outer Join Right Outer Join Full Outer Join

Inner Join

This is the default type of join. It picks all rows that have matching fields, or in other words, that meet
the join condition.

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

Outer Join

1. A left outer join returns all rows of the left-side table, irrespective of their match with the
right-side table.
2. A right outer join picks all rows from the table on the right, even if they do not meet the join
condition. Some fields in such rows may have null values in the resulting table.
3. A full outer join returns all rows of the left- and right-side tables.

Left Outer Join Right Outer Join Full Outer Join

Self Join

This is a special type of join where a table joins to itself.

Cross Join

This is the Cartesian product of rows from the tables included in the join query statement. In other
words, every row from the first table is combined with every row of the second table, one at a time.

DELETE TRUNCATE DROP

 It’s a DML Command It’s a DDL Command It’s a DDL Command

 It is used to delete Will remove all the records from table Delete the entire schema of
particular record from table that table

 This can be rolledback* Not possible to rollback. Not possible to rollback.

 Logs get generated No Logs generated No Logs generated

*In case of DELETE we can use Commit command to make permanent changes.

Primary Key Unique Key Foreign Key

Cannot have a NULL Value May Have a NULL Value Accept multiple NULL values.

Only one primary exist in Can have more than one unique table It is a field in a table which is
table constraint primary key another table.

Can be related to another Not possible in this case More than one Foreign key
table as foreign key exist

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

 Support autoincrement No Such support


feature

Question 1: SQL Query to find second highest salary of Employee

Answer:

There are many ways to find second highest salary of Employee in SQL, you can either use SQL
Join or Subquery to solve this problem. Here is SQL query using Subquery:

Select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );

SELECT max(salary) FROM Employee WHERE salary < (SELECT max(salary) FROM Employee);

See How to find second highest salary in SQL for more ways to solve this problem.

------------------------------------------------------------------------------------------------------------------

Question 2: SQL Query to find Max Salary from each department.

Answer :

SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.

------------------------------------------------------------------------------------------------------------------

Question 3: Write SQL Query to display current date.

 Answer:

SQL has built in function called GetDate() which returns current timestamp.

SELECT GetDate();

------------------------------------------------------------------------------------------------------------------

Question 4:Write an SQL Query to check whether date passed to Query is date of given
format or not.

Answer :

SQL has IsDate() function which is used to check passed value is date or not of specified format ,it
returns 1(true) or 0(false) accordingly.

SELECT ISDATE('1/08/13') AS "MM/DD/YY";

It will return 0 because passed date is not in correct format.


Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

------------------------------------------------------------------------------------------------------------------

Question 5: Write a SQL Query to print the name of distinct employee whose DOB is
between 01/01/1960 to 31/12/1975.

Answer:

SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN ‘01/01/1960’ AND
‘31/12/1975’;

------------------------------------------------------------------------------------------------------------------

Question 6:Write an SQL Query find number of employees according to gender  whose DOB
is between 01/01/1960 to 31/12/1975.

Answer : 

SELECT COUNT(*), sex from Employees  WHERE  DOB BETWEEN ‘01/01/1960 ' AND


‘31/12/1975’  GROUP BY sex;

------------------------------------------------------------------------------------------------------------------

Question 7:Write an SQL Query to find employee whose Salary is equal or greater than
10000.

Answer : 

SELECT EmpName FROM  Employees WHERE  Salary>=10000;

------------------------------------------------------------------------------------------------------------------

Question 8:Write an SQL Query to find name of employee whose name Start with ‘M’

Answer:

SELECT * FROM Employees WHERE EmpName like 'M%';

------------------------------------------------------------------------------------------------------------------

Question 9: find all Employee records containing the word "Joe", regardless of whether it
was stored as JOE, Joe, or joe.

Answer : 

SELECT  * from Employees  WHERE  upper(EmpName) like upper('joe%');

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

------------------------------------------------------------------------------------------------------------------

Question 10: Write a SQL Query to find  year from date.

Answer : 

SELECT YEAR(GETDATE()) as "Year";

------------------------------------------------------------------------------------------------------------------

Question 11: To fetch ALTERNATE records from a table. (EVEN NUMBERED)

Answer:

Select * from emp where rowid in (select decode(mod(rownum,2),0,rowid, null) from emp);

------------------------------------------------------------------------------------------------------------------

Question 12: To select ALTERNATE records from a table. (ODD NUMBERED)

Answer:

Select * from emp where rowid in (select decode(mod(rownum,2),0,null ,rowid) from emp);

------------------------------------------------------------------------------------------------------------------

Question 13: To find the 3rd MAX salary in the emp table.

Answer:

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

-----------------------------------------------------------------------------------------------------------------

Question 14: Find the 3rd MIN salary in the emp table.

Answer:

Select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2where e1.sal >=
e2.sal);

-----------------------------------------------------------------------------------------------------------------

Question 15: Select FIRST n records from a table.

Answer:

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

Select * from emp where rownum <= &n;

Question 16: Select LAST n records from a table

Answer:

Select * from emp minus select * from emp where rownum <= (select count(*) - &n from emp);

-----------------------------------------------------------------------------------------------------------------

Question 17: List dept no., Dept name for all the departments in which there are no
employees in the department.

Answer:

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 Method:  select empno,ename,b.deptno,dname from emp a, dept b where a.deptno(+) =


b.deptno and empno is null;

-----------------------------------------------------------------------------------------------------------------

Question 18: How to get 3 Max salaries ?

Answer:

Select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal <=
b.sal) order by a.sal desc;

-----------------------------------------------------------------------------------------------------------------

Question 19: How to get 3 Min salaries ?

Answer:

Select distinct sal from emp a  where 3 >= (select count(distinct sal) from emp b  where a.sal >=
b.sal);

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

-----------------------------------------------------------------------------------------------------------------
Question 20: How to get nth max salaries ?

Answer:

select distinct hiredate from emp a where &n =  (select count(distinct sal) from emp b where a.sal >=
b.sal);

-----------------------------------------------------------------------------------------------------------------

Question 21: Select DISTINCT RECORDS from emp table.

Answer:

select * from emp a where  rowid = (select max(rowid) from emp b where  a.empno=b.empno);

-----------------------------------------------------------------------------------------------------------------

Question 22: How to delete duplicate rows in a table?

Answer:

delete from emp a where rowid != (select max(rowid) from emp b where  a.empno=b.empno);

-----------------------------------------------------------------------------------------------------------------

Question 23: Count of number of employees in  department  wise.

Answer:

select count(EMPNO), b.deptno, dname from emp a, dept b  where a.deptno(+)=b.deptno  group by
b.deptno,dname;

-----------------------------------------------------------------------------------------------------------------

Question 24: Suppose there is annual salary information provided by emp table. How to
fetch monthly salary of each and every employee?

Answer:

Select ename,sal/12 as monthlysal from emp;

-----------------------------------------------------------------------------------------------------------------

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

Question 25: Select all record from emp table where deptno =10 or 40.

Answer:

select * from emp where deptno=30 or deptno=10;

-----------------------------------------------------------------------------------------------------------------

Question 26: Select all record from emp table where deptno=30 and sal>1500.

Answer:

select * from emp where deptno=30 and sal>1500;

-----------------------------------------------------------------------------------------------------------------

Question 27: Select  all record  from emp where job not in SALESMAN  or CLERK.

Answer:

select * from emp where job not in ('SALESMAN','CLERK');

-----------------------------------------------------------------------------------------------------------------

Question 28: Select all record from emp where ename in 'BLAKE','SCOTT','KING'and'FORD'.

Answer:

select * from emp where ename in('JONES','BLAKE','SCOTT','KING','FORD');

-----------------------------------------------------------------------------------------------------------------

Question 29: Select all records where ename starts with ‘S’ and its lenth is 6 char.

Answer:

select * from emp where ename like'S____';

-----------------------------------------------------------------------------------------------------------------

Question 30: Select all records where ename may be any no of  character but it should end
with ‘R’.

Answer:

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

select * from emp where ename like'%R';

-----------------------------------------------------------------------------------------------------------------

Question 31: Count  MGR and their salary in emp table.

Answer:

select count(MGR),count(sal) from emp;

-----------------------------------------------------------------------------------------------------------------

Question 32: In emp table add comm+sal as total sal  .

Answer:

select ename,(sal+nvl(comm,0)) as totalsal from emp;

Question 33: Select  any salary <3000 from emp table. 

Answer:

select * from emp  where sal> any(select sal from emp where sal<3000);

Question 34: Select  all salary <3000 from emp table. 

Answer:

select * from emp  where sal> all(select sal from emp where sal<3000);

-----------------------------------------------------------------------------------------------------------------

Question 35: Select all the employee  group by deptno and sal in descending order.

Answer:

Select ename,deptno,sal from emp order by deptno,sal desc;

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

-----------------------------------------------------------------------------------------------------------------

Question 36: How can I create an empty table emp1 with same structure as emp?

Answer:

Create table emp1 as select * from emp where 1=2;

-----------------------------------------------------------------------------------------------------------------

Question 37: How to retrive record where sal between 1000 to 2000?

Answer:

Select * from emp where sal>=1000 And  sal<2000

-----------------------------------------------------------------------------------------------------------------

Question 38: Select all records where dept no of both emp and dept table matches.

Answer:

select * from emp where exists(select * from dept where emp.deptno=dept.deptno)

-----------------------------------------------------------------------------------------------------------------
Question 39: 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?

Answer:

(Select * from emp) Union (Select * from emp1)

-----------------------------------------------------------------------------------------------------------------

Question 40: How to fetch only common records from two tables emp and emp1?

Answer:

(Select * from emp) Intersect (Select * from emp1)

-----------------------------------------------------------------------------------------------------------------

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

Question 41: How can I retrive all records of emp1 those should not present in emp2?

Answer:

(Select * from emp) Minus (Select * from


emp1)-----------------------------------------------------------------------------------------------------------
------

Question 42: Count the totalsa  deptno wise where more than 2 employees exist.

Answer:

SELECT  deptno, sum(sal) As totalsal


FROM emp
GROUP BY deptno
HAVING COUNT(empno) > 2

-----------------------------------------------------------------------------------------------------------------

Question43. What is the SQL syntax for sorting, and which is the default order?

Answer:

The default sorting order is ascending. These two statements are identical:

select from order by

select from order by asc

For descending order, simply replace “asc” with “desc.”

-----------------------------------------------------------------------------------------------------------------

Question44: Explain the use of the Join keyword and its various types.

Answer.

The join keyword is very powerful in SQL. It can be used to combine rows from multiple tables by
using common values in certain fields. The type of join decides which rows are to be selected, while
the select statement specifies which fields to include in the combination table.

Question45:. Write a SQL query to find the largest element in a column. To make it a little
tricky, write another query, this time to find the nth largest element.

Answer. You can find the largest element using:

Select max(ElementName) from TableName.

To find the nth highest element, you need to use “where” and “in” keywords as follows:
Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

select min(ElementName) from TableName where ElementName in (select distinct top n ElementName
from TableName order by ElementName desc)

-----------------------------------------------------------------------------------------------------------------

Question 46 : What are cursors? Explain different types of cursors. What are the
disadvantages of cursors? How can you avoid cursors?

Answer :

Cursors allow row-by-row prcessing of the resultsets.

Types of cursors: Static, Dynamic, Forward-only, Keyset-driven. See books online for more
information.

Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a network
roundtrip, where as a normal SELECT query makes only one rowundtrip, however large the resultset
is. Cursors are also costly because they require more resources and temporary storage (results in
more IO operations). Furthere, there are restrictions on the SELECT statements that can be used with
some types of cursors.

Most of the times, set based operations can be used instead of cursors.

-----------------------------------------------------------------------------------------------------------------

Question 47: What are triggers? How to invoke a trigger on demand?

Answer:

Triggers are special kind of stored procedures that get executed automatically when an INSERT,
UPDATE or DELETE operation takes place on a table. 

Triggers can't be invoked on demand. They get triggered only when an associated action (INSERT,
UPDATE, DELETE) happens on the table on which they are defined.

Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend
the referential integrity checks, but wherever possible, use constraints for this purpose, instead of
triggers, as constraints are much faster.

Question48: What are various type of Constraints in SQL?

Answer:

There are 6 type of constraints in SQL:

1. Primary Key
2. Unique Key
3. Foreign Key
Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

4. Not NULL
5. Check
6. Default

------------------------------------------------------------------------------------------------------------

Question 49: What is the difference between inner and outer join? Explain with example.

Answer:

Inner Join

Inner join is the most common type of Join which is used to combine the rows from two tables and
create a result set containing only such records that are present in both the tables based on the
joining condition (predicate).

Inner join returns rows when there is at least one match in both tables

If none of the record matches between two tables, then INNER JOIN will return a NULL set. Below is
an example of INNER JOIN and the resulting set.

SELECT dept.name DEPARTMENT, emp.name EMPLOYEE

FROM DEPT dept, EMPLOYEE emp

WHERE emp.dept_id = dept.id

Department Employee

HR Inno

HR Privy

Engineering Robo

Engineering Hash

Engineering Anno

Engineering Darl

Marketing Pete

Marketing Meme

Sales Tomiti

Sales Bhuti

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

Outer Join

Outer Join can be full outer or single outer

Outer Join, on the other hand, will return matching rows from both tables as well as any unmatched
rows from one or both the tables (based on whether it is single outer or full outer join respectively).

Notice in our record set that there is no employee in the department 5 (Logistics). Because of this if
we perform inner join, then Department 5 does not appear in the above result. However in the below
query we perform an outer join (dept left outer join emp), and we can see this department.

SELECT dept.name DEPARTMENT, emp.name EMPLOYEE

FROM DEPT dept, EMPLOYEE emp

WHERE dept.id = emp.dept_id (+)

Department Employee

HR Inno

HR Privy

Engineering Robo

Engineering Hash

Engineering Anno

Engineering Darl

Marketing Pete

Marketing Meme

Sales Tomiti

Sales Bhuti

Logistics

The (+) sign on the emp side of the predicate indicates that emp is the outer table here. The above
SQL can be alternatively written as below (will yield the same result as above):

SELECT dept.name DEPARTMENT, emp.name EMPLOYEE

FROM DEPT dept LEFT OUTER JOIN EMPLOYEE emp

ON dept.id = emp.dept_id
Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

-----------------------------------------------------------------------------------------------------------------

Question 50: What is the difference between JOIN and UNION?

Answer:

SQL JOIN allows us to “lookup” records on other table based on the given conditions between two
tables. For example, if we have the department ID of each employee, then we can use this
department ID of the employee table to join with the department ID of department table to lookup
department names.

UNION operation allows us to add 2 similar data sets to create resulting data set that contains all the
data from the source data sets. Union does not require any condition for joining. For example, if you
have 2 employee tables with same structure, you can UNION them to create one result set that will
contain all the employees from both of the tables.

SELECT * FROM EMP1

UNION

SELECT * FROM EMP2;

----------------------------------------------------------------------------------------------------------

Question 51: What is the difference between UNION and UNION ALL?

Answer:

UNION and UNION ALL both unify for add two structurally similar data sets, but UNION operation
returns only the unique records from the resulting data set whereas UNION ALL will return all the
rows, even if one or more rows are duplicated to each other.

In the following example, I am choosing exactly the same employee from the emp table and
performing UNION and UNION ALL. Check the difference in the result.

SELECT * FROM EMPLOYEE WHERE ID = 5

UNION ALL

ID MGR_ID DEPT_ID NAME SAL DOJ

5.0 2.0 2.0 Anno 80.0 01-Feb-2012

5.0 2.0 2.0 Anno 80.0 01-Feb-2012

SELECT * FROM EMPLOYEE WHERE ID = 5

SELECT * FROM EMPLOYEE WHERE ID = 5

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

UNION

SELECT * FROM EMPLOYEE WHERE ID = 5

ID MGR_ID DEPT_ID NAME SAL DOJ

5.0 2.0 2.0 Anno 80.0 01-Feb-2012

-----------------------------------------------------------------------------------------------------------

Question 52: What is the difference between WHERE clause and HAVING clause?

Answer:

WHERE and HAVING both filters out records based on one or more conditions. The difference is,
WHERE clause can only be applied on a static non-aggregated column whereas we will need to use
HAVING for aggregated columns.

To understand this, consider this example. 


Suppose we want to see only those departments where department ID is greater than 3. There is no
aggregation operation and the condition needs to be applied on a static field. We will use WHERE
clause here:

SELECT * FROM DEPT WHERE ID > 3

ID NAME

4 Sales

5 Logistics

Next, suppose we want to see only those Departments where Average salary is greater than 80. Here
the condition is associated with a non-static aggregated information which is “average of salary”. We
will need to use HAVING clause here:

SELECT dept.name DEPARTMENT, avg(emp.sal) AVG_SAL

FROM DEPT dept, EMPLOYEE emp

WHERE dept.id = emp.dept_id (+)

GROUP BY dept.name

HAVING AVG(emp.sal) > 80

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

DEPARTMENT AVG_SAL

Engineering 90

As you see above, there is only one department (Engineering) where average salary of employees is
greater than 80.

-----------------------------------------------------------------------------------------------------------------

Question 53: What is the difference among UNION, MINUS and INTERSECT?

Answer:

UNION combines the results from 2 tables and eliminates duplicate records from the result set.

MINUS operator when used between 2 tables, gives us all the rows from the first table except the
rows which are present in the second table.

INTERSECT operator returns us only the matching or common rows between 2 result sets.

To understand these operators, let’s see some examples. We will use two different queries to extract
data from our emp table and then we will perform UNION, MINUS and INTERSECT operations on these
two sets of data.

UNION

SELECT * FROM EMPLOYEE WHERE ID = 5

UNION

SELECT * FROM EMPLOYEE WHERE ID = 6

ID MGR_ID DEPT_ID NAME SAL DOJ

5 2 2.0 Anno 80.0 01-Feb-2012

6 2 2.0 Darl 80.0 11-Feb-2012

MINUS

SELECT * FROM EMPLOYEE

MINUS

SELECT * FROM EMPLOYEE WHERE ID > 2

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

ID MGR_ID DEPT_ID NAME SAL DOJ

1 2 Hash 100.0 01-Jan-2012

2 1 2 Robo 100.0 01-Jan-2012

INTERSECT

SELECT * FROM EMPLOYEE WHERE ID IN (2, 3, 5)

INTERSECT

SELECT * FROM EMPLOYEE WHERE ID IN (1, 2, 4, 5)

ID MGR_ID DEPT_ID NAME SAL DOJ

5 2 2 Anno 80.0 01-Feb-2012

2 1 2 Robo 100.0 01-Jan-2012

-----------------------------------------------------------------------------------------------------------------

Question 54: What is Self Join and why is it required?

Answer:

Self Join is the act of joining one table with itself.

Self Join is often very useful to convert a hierarchical structure into a flat structure

In our employee table example above, we have kept the manager ID of each employee in the same
row as that of the employee. This is an example of how a hierarchy (in this case employee-manager
hierarchy) is stored in the RDBMS table. Now, suppose if we need to print out the names of the
manager of each employee right beside the employee, we can use self join. See the example below:

SELECT e.name EMPLOYEE, m.name MANAGER

FROM EMPLOYEE e, EMPLOYEE m

WHERE e.mgr_id = m.id (+)

EMPLOYEE MANAGER

Pete Hash

Darl Hash

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

Inno Hash

Robo Hash

Tomiti Robo

Anno Robo

Privy Robo

Meme Pete

Bhuti Tomiti

Hash

The only reason we have performed a left outer join here (instead of INNER JOIN) is we have one
employee in this table without a manager (employee ID = 1). If we perform inner join, this employee
will not show-up.

-----------------------------------------------------------------------------------------------------------------

Question 55: How can we transpose a table using SQL (changing rows to column or vice-
versa) ?

Answer:

The usual way to do it in SQL is to use CASE statement or DECODE statement.

-----------------------------------------------------------------------------------------------------------------

Question56: How to generate row number in SQL Without ROWNUM

Answer:

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

NAME SAL ROW_NUM

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

Anno 80 1

Bhuti 60 2

Darl 80 3

Hash 100 4

Inno 50 5

Meme 60 6

Pete 70 7

Privy 50 8

Robo 100 9

Tomiti 70 10

The column that is used in the row number generation logic is called “sort key”. Here sort key is
“name” column. For this technique to work, the sort key needs to be unique. We have chosen the
column “name” because this column happened to be unique in our Employee table. If it was not
unique but some other collection of columns was, then we could have used those columns as our sort
key (by concatenating those columns to form a single sort key).

Also notice how the rows are sorted in the result set. We have done an explicit sorting on the
row_num column, which gives us all the row numbers in the sorted order. But notice that name
column is also sorted (which is probably the reason why this column is referred as sort-key). If you
want to change the order of the sorting from ascending to descending, you will need to change “>=”
sign to “<=” in the query.

As I said before, this method is not very generic. This is why many databases already implement other
methods to achieve this. For example, in Oracle database, every SQL result set contains a hidden
column called ROWNUM. We can just explicitly select ROWNUM to get sequence numbers.

-----------------------------------------------------------------------------------------------------------------

Question 57: How to select first 5 records from a table?

Answer:

This question, often asked in many interviews, does not make any sense to me. The problem here is
how do you define which record is first and which is second. Which record is retrieved first from the
database is not deterministic. It depends on many uncontrollable factors such as how database works
at that moment of execution etc. So the question should really be – “how to select any 5 records from
the table?” But whatever it is, here is the solution:

In Oracle,

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

SELECT *

FROM EMP

WHERE ROWNUM <= 5;

In SQL Server,

SELECT TOP 5 * FROM EMP;

Generic solution,

I believe a generic solution can be devised for this problem if and only if there exists at least one
distinct column in the table. For example, in our EMP table ID is distinct. We can use that distinct
column in the below way to come up with a generic solution of this question that does not require
database specific functions such as ROWNUM, TOP etc.

SELECT name

FROM EMPLOYEE o

WHERE (SELECT count(*) FROM EMPLOYEE i WHERE i.name < o.name) < 5

name

Inno

Anno

Darl

Meme

Bhuti

I have taken “name” column in the above example since “name” is happened to be unique in this
table. I could very well take ID column as well.

In this example, if the chosen column was not distinct, we would have got more than 5 records
returned in our output.

-----------------------------------------------------------------------------------------------------------------

Question 58: What is the difference between ROWNUM pseudo column and ROW_NUMBER()
function?

Answer:

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

ROWNUM is a pseudo column present in Oracle database returned result set prior to ORDER BY being
evaluated. So ORDER BY ROWNUM does not work.

ROW_NUMBER() is an analytical function which is used in conjunction to OVER() clause wherein we


can specify ORDER BY and also PARTITION BY columns.

Suppose if you want to generate the row numbers in the order of ascending employee salaries for
example, ROWNUM will not work. But you may use ROW_NUMBER() OVER() like shown below:

SELECT name, sal, row_number() over(order by sal desc) rownum_by_sal

FROM EMPLOYEE o

name Sal ROWNUM_BY_SAL

Hash 100 1

Robo 100 2

Anno 80 3

Darl 80 4

Tomiti 70 5

Pete 70 6

Bhuti 60 7

Meme 60 8

Inno 50 9

Privy 50 10

-----------------------------------------------------------------------------------------------------------------
Question59: What are the differences among ROWNUM, RANK and DENSE_RANK?

Answer:

ROW_NUMBER assigns contiguous, unique numbers from 1.. N to a result set.

RANK does not assign unique numbers—nor does it assign contiguous numbers. If two records tie for
second place, no record will be assigned the 3rd rank as no one came in third, according to RANK. See
below:

SELECT name, sal, rank() over(order by sal desc) rank_by_sal

FROM EMPLOYEE o
Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

name Sal RANK_BY_SAL

Hash 100 1

Robo 100 1

Anno 80 3

Darl 80 3

Tomiti 70 5

Pete 70 5

Bhuti 60 7

Meme 60 7

Inno 50 9

Privy 50 9

name Sal DENSE_RANK_BY_SAL

Hash 100 1

Robo 100 1

Anno 80 2

Darl 80 2

Tomiti 70 3

Pete 70 3

Bhuti 60 4

Meme 60 4

Inno 50 5

Privy 50 5

DENSE_RANK, like RANK, does not assign unique numbers, but it does assign contiguous numbers.
Even though two records tied for second place, there is a third-place record. See below:

SELECT name, sal, dense_rank() over(order by sal desc) dense_rank_by_sal

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com
DATABASE INTERVIEW QUESTIONS & ANSWERS

FROM EMPLOYEE o

Feel free to write @ er.jagtinder@gmail.com regarding your feedback or any query.

Compiled By:
Jagtinder Pal Singh
QA Lead Gurgaon (India)
er.jagtinder@gmail.com

You might also like