LearnFrenzy
Sign in Take a free test

Database Management interview · Set 9 of 9

SQL Tricky and Output-Based Questions

  • 6 follow-up questions
  • 4 key points
  • Code examples
Test yourself

NULL is the usual suspect

Three-valued logic, aggregate rules, join filters and type behaviour explain almost every tricky SQL question. Explain the rule, then the safe query.

  • NULLsComparisons, NOT IN and aggregates.
  • JoinsLEFT JOIN filters and row multiplication.
  • RulesGROUP BY, division and NULL ordering.

SQL looks simple until NULL shows up. Most tricky SQL questions — and many production bugs — come from three-valued logic, join filters and aggregate rules. Predict the output, then read the rule.

1. What does this return? (NULL comparison)

No rows. Any comparison with NULL yields UNKNOWN, which WHERE treats as false. Use IS NULL.

SELECT * FROM employees WHERE manager_id = NULL;     -- always empty
SELECT * FROM employees WHERE manager_id IS NULL;    -- correct

2. COUNT(*) vs COUNT(column) vs COUNT(DISTINCT column)?

For values (1), (NULL), (1): COUNT(*) = 3 (all rows), COUNT(col) = 2 (non-NULL values), COUNT(DISTINCT col) = 1. Aggregates such as SUM and AVG also ignore NULLs — AVG divides by non-NULL rows only.

SELECT COUNT(*), COUNT(bonus), COUNT(DISTINCT bonus), AVG(bonus)
FROM (VALUES (100), (NULL), (100)) AS t(bonus);
-- 3 | 2 | 1 | 100

3. Why does NOT IN return nothing here?

If the subquery returns any NULL, x NOT IN (…, NULL) is never true (it becomes UNKNOWN). Use NOT EXISTS, which handles NULLs as expected.

SELECT name FROM departments
WHERE id NOT IN (SELECT dept_id FROM employees);    -- empty if any dept_id is NULL

SELECT d.name FROM departments d
WHERE NOT EXISTS (SELECT 1 FROM employees e WHERE e.dept_id = d.id);   -- correct

4. Why did my LEFT JOIN turn into an INNER JOIN?

A condition on the right table in the WHERE clause removes the NULL-extended rows. Put such conditions in the ON clause to keep unmatched left rows.

-- loses customers without 2026 orders
SELECT c.name, o.id FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= '2026-01-01';

-- keeps every customer
SELECT c.name, o.id FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= '2026-01-01';

5. What does this return? (integer division)

0 in PostgreSQL and SQL Server — integer divided by integer truncates. Multiply by 1.0 or cast to a decimal. (MySQL's / returns a decimal; DIV is integer division.)

SELECT 1 / 2;        -- 0 in PostgreSQL / SQL Server
SELECT 1.0 / 2;      -- 0.5

6. Why does this GROUP BY query fail?

Every selected column must be grouped or aggregated; name is neither, so most databases reject it (MySQL with ONLY_FULL_GROUP_BY too). Aggregate it, group by it, or use a window function.

SELECT dept_id, name, MAX(salary) FROM employees GROUP BY dept_id;   -- error

7. How many rows does a join produce with duplicate keys?

Joins multiply matches: if a customer has 3 orders and 2 addresses, joining all three tables gives 3 × 2 = 6 rows, and SUM(amount) is doubled. Aggregate in subqueries before joining, or join only what you need.


8. What does ORDER BY do with NULLs?

It depends on the database: PostgreSQL and Oracle put NULLs last in ascending order (first in descending); MySQL and SQL Server put them first in ascending order. Use NULLS FIRST/NULLS LAST (PostgreSQL, Oracle) or an expression to be explicit.

SELECT name, bonus FROM employees ORDER BY bonus DESC NULLS LAST;
-- MySQL: ORDER BY bonus IS NULL, bonus DESC

Remember

Key points

  • IS NULL, never = NULL

    Comparisons with NULL are unknown.

  • NOT EXISTS over NOT IN

    Safe when NULLs exist.

  • Right-table filters in ON

    For outer joins.

  • Aggregate before joining

    Avoid double counting.

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 What is NULL + 5? Basic

    NULL — any arithmetic with NULL gives NULL. Use COALESCE(col, 0) to treat it as zero.

  2. 2 Does UNIQUE allow multiple NULLs? Intermediate

    In PostgreSQL and MySQL yes (NULLs are not equal to each other); SQL Server allows only one NULL in a standard unique constraint.

  3. 3 What is the difference between WHERE and HAVING with aggregates? Basic

    WHERE filters rows before grouping and cannot use aggregates; HAVING filters groups after aggregation.

  4. 4 What does SELECT COUNT(1) mean? Basic

    It counts rows, the same as COUNT(*); there is no meaningful performance difference in modern databases.

  5. 5 Why can BETWEEN with timestamps miss rows? Intermediate

    BETWEEN '2026-01-01' AND '2026-01-31' stops at midnight of the 31st; use >= start AND < next day.

  6. 6 What does COALESCE do compared with ISNULL/IFNULL? Basic

    COALESCE returns the first non-NULL of any number of arguments and is standard SQL; IFNULL/ISNULL take two arguments and are vendor-specific.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links