TechBuzzPro

Oracle SQL Queries for Interviews: Examples and Answers

· 7 min read · Apps & Browsers

In this article
  1. What Oracle SQL Interviews Usually Test
  2. Filtering, Sorting, and Limiting Rows
  3. Joins and Missing Matches
  4. Grouping, Aggregates, and Duplicate Detection
  5. Subqueries and Common Table Expressions
  6. Analytic Functions and Ranking
  7. Oracle-Specific Details and Common Mistakes
  8. Frequently Asked Questions

Oracle SQL interview preparation is strongest when you can write and explain queries for filtering, joins, aggregation, subqueries, and analytic functions—not just recall syntax. The examples below use Oracle SQL and a small employee-and-department schema, with notes on common interview traps and how Oracle-specific behavior differs from other databases.

What Oracle SQL Interviews Usually Test

Interview questions often begin with basic retrieval and become more demanding as the interviewer adds conditions: find duplicate values, compare each employee with a department average, or return the second-highest salary. A good answer states any assumptions, produces the requested rows, and handles ties or null values deliberately.

These examples assume two tables: employees(employee_id, employee_name, department_id, salary, manager_id, hire_date) and departments(department_id, department_name). Unless stated otherwise, each employee has a unique employee_id, but departments can have multiple employees. Oracle SQL syntax is shown; table and column names are illustrative.

  • Core skills: SELECT, WHERE, ORDER BY, joins, GROUP BY, HAVING, and subqueries.
  • More advanced skills: common table expressions, set operators, analytic functions, and careful treatment of nulls and ties.
  • Communication: explain why a query works and what happens when data is missing, duplicated, or tied.

Filtering, Sorting, and Limiting Rows

Start with the simplest useful query: select only the columns needed, filter rows with WHERE, and specify a predictable order. For example, to list employees earning at least 80,000, highest salary first:

SELECT employee_name, salary FROM employees WHERE salary >= 80000 ORDER BY salary DESC, employee_name;

The second sort key makes the result more stable when salaries match. Oracle sorts null values differently depending on direction unless you specify their placement. For example, ORDER BY salary DESC NULLS LAST explicitly puts missing salaries at the end.

To return the five highest-paid employees in current Oracle versions, use the row-limiting clause:

SELECT employee_name, salary FROM employees ORDER BY salary DESC FETCH FIRST 5 ROWS ONLY;

When an interview asks for “top five salaries,” clarify whether it means five employees or the five highest distinct salary values. The query above returns at most five rows and may split a tie at the cutoff. FETCH FIRST 5 ROWS WITH TIES includes all rows tied with the fifth row, provided the ordering identifies the relevant tie. For older Oracle releases, a common alternative uses ROWNUM in an outer query; applying ROWNUM before sorting does not reliably select the highest values.

Joins and Missing Matches

A join combines related rows. To show employees alongside department names, including employees whose department is absent or unmatched, use a left join:

SELECT e.employee_name, d.department_name FROM employees e LEFT JOIN departments d ON d.department_id = e.department_id;

A frequent follow-up asks for departments with no employees. A left join plus a null check finds them:

SELECT d.department_id, d.department_name FROM departments d LEFT JOIN employees e ON e.department_id = d.department_id WHERE e.employee_id IS NULL;

The test uses a non-nullable key from the right-hand table. Checking a nullable business column instead could mistakenly label a matched row as missing. Also be careful when placing filters on the right table: putting a condition in WHERE can remove the unmatched rows and make a left join behave like an inner join for that condition. If unmatched departments must remain, the filter may belong in the ON clause.

Oracle supports standard explicit join syntax. Older Oracle code may use the legacy (+) outer-join operator, but it is less clear and has restrictions; interview answers are generally easier to read with LEFT JOIN or RIGHT JOIN.

Grouping, Aggregates, and Duplicate Detection

Use GROUP BY to calculate one result per group, such as employee counts and average salaries by department:

SELECT department_id, COUNT(*) AS employee_count, AVG(salary) AS average_salary FROM employees GROUP BY department_id;

Use HAVING to filter groups after aggregation. For example, departments with at least five employees:

SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id HAVING COUNT(*) >= 5;

By contrast, WHERE filters individual rows before grouping. A typical duplicate-finding question asks which salary values occur more than once:

SELECT salary, COUNT(*) AS occurrences FROM employees WHERE salary IS NOT NULL GROUP BY salary HAVING COUNT(*) > 1;

This identifies duplicated salary values, not duplicate employee records. To find repeated employee names, group by employee_name instead. State which columns define a duplicate; otherwise, the question is ambiguous. Remember that aggregate functions such as AVG and COUNT(column) ignore null values, while COUNT(*) counts rows.

Subqueries and Common Table Expressions

A scalar subquery can compare each employee with the company-wide average salary:

SELECT employee_name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

For the second-highest distinct salary, one readable approach is to find the maximum salary below the maximum:

SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

This returns one value, or null if there is no lower non-null salary. Because it compares distinct salary levels implicitly, multiple employees can share that second-highest value. If the question asks for the second employee by rank instead, use an analytic ranking function and define how ties should count.

A common table expression (CTE), introduced with WITH, can make a multi-step query easier to follow. For example, calculate department averages first, then compare each employee with the average for their department:

WITH department_pay AS (SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id) SELECT e.employee_name, e.salary, p.avg_salary FROM employees e JOIN department_pay p ON p.department_id = e.department_id WHERE e.salary > p.avg_salary;

This inner join excludes employees whose department is null or has no matching group. That is appropriate for this comparison, but explain the choice if the interviewer expects such rows to appear.

Analytic Functions and Ranking

Analytic, or window, functions calculate across related rows without collapsing them into one row per group. To rank employees by salary within each department:

SELECT department_id, employee_name, salary, DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank FROM employees;

DENSE_RANK gives equal salaries the same rank and leaves no gaps afterward. RANK also assigns ties the same rank, but skips subsequent rank numbers. ROW_NUMBER assigns every row a distinct number; to make that ordering deterministic when salaries tie, add a unique key to the window ordering, such as ORDER BY salary DESC, employee_id.

To return everyone with the second-highest distinct salary in each department, rank salaries with DENSE_RANK in a CTE and filter for rank 2 in the outer query. Oracle does not allow filtering a newly calculated analytic alias in the same query block’s WHERE clause, so the extra query level matters.

Oracle-Specific Details and Common Mistakes

Oracle uses NULL to represent a missing or unknown value. Comparisons such as salary = NULL do not find nulls; use IS NULL or IS NOT NULL. Likewise, NOT IN can produce surprising results if its subquery returns a null. For anti-join questions, NOT EXISTS is often a safer expression:

SELECT d.department_id FROM departments d WHERE NOT EXISTS (SELECT 1 FROM employees e WHERE e.department_id = d.department_id);

Oracle treats an empty character string as null for SQL character values, a behavior that can differ from other database systems. Oracle also provides NVL and the standard COALESCE for substituting a value when an expression is null. Do not replace nulls automatically: a substitute such as zero may change the meaning of an average or comparison.

Other common errors include selecting a column that is neither grouped nor aggregated, confusing WHERE with HAVING, and assuming row order without ORDER BY. For date questions, use typed date values or explicit format models when converting text; implicit conversions can depend on session settings.

Frequently Asked Questions

What are the most common Oracle SQL interview queries?
Expect filtering and sorting, inner and outer joins, group totals, duplicate detection, subqueries, top-N results, and ranking with analytic functions. The exact mix depends on the role and expected SQL experience.

How do I find the second-highest salary in Oracle?
For the second-highest distinct salary overall, use MAX(salary) over rows where salary is below the overall maximum. Use DENSE_RANK when you need the result per department or need to return the employees who share that salary.

What is the difference between RANK and DENSE_RANK?
Both give tied values the same rank. RANK leaves gaps after ties; DENSE_RANK does not. Choose based on whether the question means position in a list or rank among distinct values.

Can I use LIMIT in Oracle SQL?
LIMIT is not Oracle’s row-limiting syntax. Use FETCH FIRST n ROWS ONLY in supported versions, or a correctly structured ROWNUM query for older releases.

How should I explain an interview query?
Describe the rows being selected, how joins affect unmatched data, whether ties are included, and how nulls behave. Then mention an assumption or edge case and, if useful, how you would check performance on real data.