You are on page 1of 8

SQL COMMON INTERVIEW QUESTIONS

> QUESTION POSED ON: 05 July 2007


> EXPERT RESPONSE

To celebrate our 600th "Ask The Expert" SQL column, we thought we'd run
a special three-part answer, to highlight some of the common or frequently
asked SQL questions we've seen over the past six years.

Please do keep sending in your questions. We love ’em.

Types of common SQL questions

Common SQL questions fall into three broad categories:

1. Novice questions
2. Homework questions
3. Serious SQL questions

1. Novice questions

Novice questions are usually very simple. They immediately reveal only
limited degrees of familiarity with SQL. For example,

• How can I delete all rows before a particular date?


Answer: With a WHERE condition in the DELETE statement. Every
basic SQL tutorial covers this.
• I want only rows which have the same IDs in both tables
Answer: Use an INNER JOIN. Actually not a bad question, if you've
never seen or heard of joins, which many new programmers
apparently have not.
• What is the difference between Distinct and Unique?
Answer: Also quite a reasonable question. They aren't synonyms in
SQL, because each can be used only in very specific places in SQL
statements—DISTINCT in a SELECT clause or COUNT(DISTINCT)
expression, and UNIQUE when declaring a constraint. On the other
hand, they are synonyms when talking about SQL, because when you
use SELECT DISTINCT, you do get unique rows. So what to answer?

"What is the difference between ..." seems to be a common question.

• What is the difference between "inner" and "outer" joins?


Answer: Outer joins return unmatched rows. What more is there to

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.

We really do like the short questions, because it gives us so much latitude


in choosing the examples with which to illustrate the topic. But sometimes
the question just isn't specific enough.
Then there are the "It depends" questions. These questions often exhibit
reasonable knowledge of SQL. For example,

• Which is faster, Functions Or Procedures, and why? Thanks, it's


urgent!
Answer: Both functions and procedures require more than a
beginner's level of knowledge of SQL. Knowing which is faster might
—might—sway the decision to use one or the other. However, too
much depends on factors completely outside the question, such as
what you're trying to accomplish. Furthermore, it is naïve to think
there might be a difference in performance for all contexts. That's
about all we can reasonably say on the subject. Try some
benchmarks on your own database system. As for it being urgent,
this does not influence whether we will answer your question.

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:

• JOIN, SUB-QUERY, which one is more efficient...


Answer: Which is faster, subquery or join? (4 November, 2005)

We get a steady supply of novice questions, and this is good. It indicates


that new blood is constantly coming into the world of relational databases.
While most of the questions are not interesting enough to be selected for
an answer, we do read them all.

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,

• Find the three smallest numbers from a column of numbers.


Answer: The three smallest numbers (16 December, 2005)
• How to select nth row from a table if they are not in any order?
Answer: The Nth row in a table (30 September, 2005)
• What is the query to select empname and his managername from an
emp table?
Answer: Select empname and managername (17 January, 2006)
• 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.
• Find employees in each department who make more money than their immediate
manager.

Page 3 of 8
We answer obvious homework questions only if there's something
interesting for all readers, or some subtle nuance about SQL that's worth
discussing. For example,

• I want to display all the employees who report to a person.


Answer: All employees under a given manager (23 April, 2007). This
question was selected because it's a very common scenario (especially
if not involving bosses and employees, but similar structures). It's a
tough problem, and there is comprehensive discussion.

Finally, there are the "esoteric" homework questions. We speculate that


these are questions from university assignments. We never answer these,
either.

• Why are query languages such as SQL based on relational calculus rather than relational
algebra?
• SQL is a set oriented language. Explain this statement and outline why in some
circumstances, a navigational query language might be prefered.
• Explain what is meant by query decomposition and query optimization.

Page 4 of 8
3. Serious SQL questions

Not surprisingly, there are several common if not "classic" types of complex
SQL questions.

Duplicates

Finding "duplicates" is probably the #1 or #2 most common SQL question.


The scenario usually involves an autonumbering surrogate key, with no
additional unique constraint on the "real" (candidate) key.

• How I can extract the all the duplicate rows in a table?


• Hi sir, I want to delete duplicate data from the table
• How do I find duplicate entries in a table?

Lots of solutions exist, each involving a number of steps, many of which are
dictated by what you mean by "duplicates" and how you want to handle
disparities and missing data. So the solution must be tailored to your specific
scenario, and is of little use to other people.
Occasionally, though, the problem is tricky, and therefore interesting:

• 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.

Page 5 of 8
• 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
(25 April, 2001)
Top 5 salaried employees, not using TOP (13 July, 2001)
• I want a query that will give me the top ten sales for each salesman.
Top ten sales for each salesman (17 May, 2004)
• How do I retrieve the top 10 brands(sorted by units) for each category-
size combination?
Top N rows for each X (21 March, 2005)

Latest X for each Y

A subtle variation of the "Top N for each X" problem, involving dates:

• ... the most current wage for each employee.


Rows having maximum group value in MySQL (15 November 2002)
• ... only latest record from each group.
Latest row for each group (17 December 2003)
• ... three latest payments for each account.
Latest three payments for each account (13 April, 2007)

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 with SQL (16 July 2002)


• Paging through SQL query results (14 February 2005)
• Paging through a result set without TOP or LIMIT (24 October 2002)

Comma-delimited string questions

For some reason, denormalizing one-to-many data into comma-delimited


strings is a very common requirement:

• Concatenate into a single row (28 June 2006)


• Concatenate values into comma-delimited string (14 February 2005)

Denormalizing on output isn't a sin, by the way. Storing comma-delimited


strings is a bad idea, though.

Page 6 of 8
Miscellaneous questions

Finally, a few oddball questions that seem to come up often:

• More about ORDER BY for a specified sequence (13 March, 2007)


• COUNT(*) or COUNT(1) (17 January, 2007)
• Two "copies" of a lookup table in the same query (17 May 2004)
• The "any" option in dynamic search SQL (8 April 2003)

Remember, please do keep sending in your questions. We love ’em.

Page 7 of 8
Page 8 of 8

You might also like