Database Basics
The dept column of an employee table contains, from the top row down, Sales, Dev, Sales, Admin, Dev, Sales (six rows in total). How many rows does SELECT DISTINCT dept FROM employee return?
- A2 rows
- B3 rows
- C6 rows
Show answer and explanation
Answer: B. 3 rows
DISTINCT removes duplicate rows from the result, collapsing identical values into one row. There are three distinct dept values, Sales, Dev and Admin, so the result has three rows. Without DISTINCT, SELECT dept FROM employee returns all six rows, duplicates included. Two rows is the count of only the departments that appear two or more times (Sales and Dev), which corresponds to the result of a query such as SELECT dept FROM employee GROUP BY dept HAVING COUNT(*) >= 2.
- A. This counts only Sales and Dev, which appear more than once. Admin, which appears once, also remains as a row.
- B. Correct. Removing duplicates leaves three rows: Sales, Dev and Admin.
- C. This is the number of rows without DISTINCT.