Professional Documents
Culture Documents
SQL Common Interview Questions
SQL Common Interview Questions
1. Novice questions
Novice questions are usually very simple. They immediately reveal only
limited degrees of familiarity with SQL. For example,
Page 1 of 8
say?
What is the difference between GROUP BY and ORDER BY clauses?
Answer: One does grouping, the other does ordering. That might
sound flippant, but it is not meant to be.
Several "it depends" questions seem to come up over and over, and we try
to answer them at least once. For example, here's one we've seen many
times:
Page 2 of 8
2. Homework questions
Notice anything about the following questions?
Write a query to get the second highest amount in the table
List last name and hire date of any employee in the same department as Zlotkey
I always get puzzled up when asked some typical questions in interview like the nth
highest salary in the EMP table
In SQL from two different tables select the first five highest salary
How can we find the second highest salary from three departments named D1, D2 and D3
How to select last ten max(sal) from EMP
List the highest salary, the lowest salary, the average salary and the total salaries of
employees
What are the different way of selecting 3rd highest salary from EMP table?
How to get Top 5th, 6th, Nth salary from emp table of oracle
Get the doctor with the highest count of visits
How to find the 2nd, 3rd, 4th, ... nth max of a column in a table
How to find out the fifth max salary from employee table
I want to know how to retrieve third highest and lowest from a column
Need help finding the second largest pay and second smallest pay from the salary table
How do I get the top 3 highest salary from the emp table
Dear Sir! How can I get the second maximum salary?
We will only occasionally answer a homework question. However, sometimes
the answer will not really be what the student is looking for. For example,
How can I select the third, fourth and fifth rows, out of a 100 rows?
Answer: Select 3rd, 4th, 5th of 100 rows (10 April, 2002)
Then there are more "substantial" homework questions, which require more
advanced SQL, such as complex joins and subqueries. We usually don't touch
these, for obvious reasons. Hint: no, it's not because they always somehow
seem to involve bosses.
Which are the workers that live in the same city as their bosses?
Display the name of the manager who is having maximum number of subordinates.
Page 3 of 8
Find employees in each department who make more money than their immediate
manager.
Page 4 of 8
How to get a list of employees who have the same last and first name?
Employees with the same first and last names in SQL (11 May, 2007)
Accidentally the key got dropped and some duplicate records were
inserted into the table....
Removing duplicate rows (9 February, 2005)
Top N
Often a popular homework question, the "Top N" problem also seems to
happen to everyone in real life.
I need to display the first 'X' number of rows in a query.
My select query comes back with 30,000 rows, and I just want the first 300.
I need to get only 100 records from the bottom of the database based on specific field.
It is such a common question that we created a mini-FAQ for it years ago:
FIRST N rows, TOP N rows, LAST N rows, BOTTOM N rows... (25 November 2002)
Top N for each X
More complex than simple Top N questions, but easy to recognize because
they invariably involve the word every or each.
How will I find the first 5 highest salaried employees in each dept?
How to find the first 5 highest salaried employees in each department
Page 5 of 8
As you can see, even though these are common questions, sometimes we
answer them again.
Pagination
Sadly, pagination seems to give everyone trouble. Sometimes the best
solutions are implemented with caching, at the application or middle tier
level. Pagination with SQL is positively fraught with difficulty.
Paging through a result set without TOP or LIMIT (24 October 2002)
Page 6 of 8
Two "copies" of a lookup table in the same query (17 May 2004)
Page 7 of 8
Page 8 of 8