Department that contains some NULL values. What will be the result of the following query?SELECT DISTINCT Department FROM Employees ORDER BY Department;
CREATE TABLE Employees (ID INT, Department VARCHAR(20)); INSERT INTO Employees VALUES (1, 'Sales'), (2, NULL), (3, 'HR'), (4, NULL), (5, 'Sales'); SELECT DISTINCT Department FROM Employees ORDER BY Department;
The DISTINCT keyword treats all NULLs as one group, so only one NULL appears. ORDER BY sorts NULLs before non-NULL values by default, so NULL appears first, then 'HR' and 'Sales' alphabetically.
Region that contains NULL values, what will be the output of this query?SELECT Region, COUNT(*) FROM Orders GROUP BY Region ORDER BY Region;
CREATE TABLE Orders (OrderID INT, Region VARCHAR(20)); INSERT INTO Orders VALUES (1, 'East'), (2, NULL), (3, 'West'), (4, NULL), (5, 'East'); SELECT Region, COUNT(*) FROM Orders GROUP BY Region ORDER BY Region;
GROUP BY groups NULL values together as one group. ORDER BY sorts NULLs before non-NULL values, so the output has three rows with NULL first, then 'East', then 'West'.
Category column with NULLs appearing last?SELECT Category FROM Products ORDER BY Category NULLS LAST;
Option A uses the correct standard SQL syntax to order NULLs last. Option A is valid but orders descending first, which changes the order. Option A is a workaround but not standard syntax. Option A is invalid syntax.
Status which contains many NULL values. Which approach can improve performance while keeping NULLs grouped correctly?Using COALESCE replaces NULLs with a known value, which can improve grouping performance and indexing. Excluding NULLs changes results. GROUP BY alone works but may be slower. DISTINCT does not replace GROUP BY functionality.
NULLs are treated as equal for DISTINCT and GROUP BY, so they appear as one group. ORDER BY placement of NULLs varies by SQL dialect; some put NULLs first, others last. This can be controlled explicitly with NULLS FIRST or NULLS LAST.