SQL Notes
Learn the most important SQL output-based interview questions with tables, queries, outputs, explanations, and interview tips.
Here's what happens in many interviews: the interviewer won't ask you to define terms.
Instead, they'll show you:
Queryand ask:
What Will Be The Output?
These test your real understanding:
SQL Logic
Execution Order
Joins
Grouping
Window Functions
NULL Handling
ThinkingYou'll see these in:
Placements
Coding Tests
SQL Interviews
Data Analyst InterviewsOutput
2
Explanation
COUNT(*) counts every single row, no exceptions.
Question 2
Table:
| Salary |
|---|
| --------- |
| 50000 |
| NULL |
| 70000 |
Query:
SELECT COUNT(Salary)
FROM Employees;Output
2
Explanation
COUNT(Salary) skips NULLs:
Ignores NULL ValuesQuestion 3
Query:
SELECT COUNT(*)
FROM Employees;Same table as Question 2.
Output
3
Explanation
COUNT(*) includes the NULL row. The table has 3 rows total.
Question 4
Table:
| Salary |
|---|
| --------- |
| 50000 |
| 70000 |
| 90000 |
Query:
SELECT AVG(Salary)
FROM Employees;Output
70000
Calculation:
(50000+70000+90000)/3Question 5
Table:
| Salary |
|---|
| --------- |
| 50000 |
| NULL |
| 90000 |
Query:
SELECT AVG(Salary)
FROM Employees;Output
70000
Explanation
NULLs are skipped:
(50000+90000)/2 = 70000Question 6
Table:
| Name |
|---|
| ------ |
| Rahul |
| Rahul |
| Priya |
Query:
SELECT DISTINCT Name
FROM Employees;Output
Rahul Priya
Question 7
Table:
| Salary |
|---|
| --------- |
| 50000 |
| 70000 |
| 60000 |
Query:
SELECT *
FROM Employees
ORDER BY Salary DESC;Output
70000 60000 50000
Question 8
Table:
| ID |
|---|
| ---- |
| 1 |
| 2 |
| 3 |
Query:
SELECT *
FROM Employees
WHERE ID > 2;Output
3
Question 9
Table:
| DepartmentID |
|---|
| -------------- |
| 1 |
| 1 |
| 2 |
| 2 |
| 2 |
Query:
SELECT DepartmentID,
COUNT(*)
FROM Employees
GROUP BY DepartmentID;Output
| DepartmentID | Count |
|---|---|
| 1 | 2 |
| 2 | 3 |
Question 10
Table:
| DepartmentID |
|---|
| -------------- |
| 1 |
| 1 |
| 2 |
| 2 |
| 2 |
Query:
SELECT DepartmentID,
COUNT(*)
FROM Employees
GROUP BY DepartmentID
HAVING COUNT(*) > 2;Output
| DepartmentID | Count |
|---|---|
| 2 | 3 |
Question 11
Employees:
| ID | Name |
|---|---|
| 1 | Rahul |
| 2 | Priya |
Departments:
| ID | Department |
|---|---|
| 1 | HR |
| 3 | IT |
Query:
SELECT *
FROM Employees E
INNER JOIN Departments D
ON E.ID = D.ID;Output
| ID | Name | Department |
|---|---|---|
| 1 | Rahul | HR |
Question 12
Same tables.
Query:
SELECT *
FROM Employees E
LEFT JOIN Departments D
ON E.ID = D.ID;Output
| ID | Name | Department |
|---|---|---|
| 1 | Rahul | HR |
| 2 | Priya | NULL |
Question 13
Query:
SELECT NULL = NULL;Output
Unknown / NULL
Explanation
NULL doesn't equal anything. Even:
NULL != NULLreturns unknown.
Question 14
Query:
SELECT *
FROM Employees
WHERE Salary = NULL;Output
No Rows
Correct Query
WHERE Salary IS NULLTips for Solving Output-Based SQL Questions
Output-based questions test your ability to mentally execute SQL queries. Here are strategies to solve them accurately:
Step 1: Understand the Data Before looking at the query, carefully examine the table data. Note the number of rows, data types, any NULL values, and duplicate values. These details often determine the output.
Step 2: Execute the Query Mentally Process the query in the correct SQL execution order:
- FROM (identify the table)
- WHERE (filter rows)
- GROUP BY (group remaining rows)
- HAVING (filter groups)
- SELECT (choose columns and apply functions)
- ORDER BY (sort results)
- LIMIT (restrict output rows)
Step 3: Watch for Common Traps
- NULL comparisons:
NULL = NULLreturns NULL (unknown), not TRUE - String comparison: 'A' < 'B' in SQL (alphabetical ordering)
- Integer division: In some databases,
5/2 = 2(not 2.5) - GROUP BY behavior: Non-aggregated columns in SELECT must appear in GROUP BY
- DISTINCT applies to the entire row, not just one column
Additional Practice Questions
Question 15:
Table Orders:
| OrderID | CustomerID | Amount |
|---|---|---|
| 1 | 101 | 500 |
| 2 | 102 | NULL |
| 3 | 101 | 300 |
Query:
SELECT CustomerID, SUM(Amount) as Total
FROM Orders
GROUP BY CustomerID;Output:
| CustomerID | Total |
|---|---|
| 101 | 800 |
| 102 | NULL |
Explanation: SUM ignores NULL values, but if ALL values are NULL, the result is NULL (not 0). Customer 101 has 500 + 300 = 800. Customer 102 has only NULL, so the sum is NULL.
Question 16:
Query:
SELECT COALESCE(NULL, NULL, 'Hello', 'World');Output:
Hello
Explanation: COALESCE returns the first non-NULL argument. It skips the two NULLs and returns 'Hello', ignoring 'World' since a non-NULL value was already found.
Question 17:
SELECT COUNT(*), COUNT(Amount)
FROM Orders;Output:
| COUNT(*) | COUNT(Amount) |
|---|---|
| 3 | 2 |
Explanation: COUNT(*) counts all rows regardless of NULL values (3 rows total). COUNT(Amount) counts only non-NULL values in the Amount column (2 non-NULL values).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for SQL Output-Based Interview Questions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL Complete Guide topic.
Search Terms
sql-complete-guide, sql complete guide, sql, complete, guide, interview, questions, output
Related SQL Complete Guide Topics