Professional Documents
Culture Documents
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.
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,
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
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,
• 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
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.
• FIRST N rows, TOP N rows, LAST N rows, BOTTOM N rows... (25 November 2002)
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)
A subtle variation of the "Top N for each X" problem, involving dates:
As you can see, even though these are common questions, sometimes we
answer them again.
Pagination
Page 6 of 8
Miscellaneous questions
Page 7 of 8
Page 8 of 8