CS FundamentalsModerateTCS NQT · Infosys pattern

Table Emp(id, name, dept, salary, mgr) rows: (1, 'Asha', 'HR', 50000, NULL), (2, 'Ravi', 'IT', 70000, 1), (3, 'Meena', 'IT', 70000, 2), (4, 'John', 'Sales', 40000, 1), (5, 'Kiran', 'IT', 90000, 2), (6, 'Dev', NULL, 45000, 4), (7, 'Lata', 'Sales', NULL, 4). Table Dept(dname, city) rows: ('HR', 'Pune'), ('IT', 'Delhi'), ('Ops', 'Mumbai'). What does this query return?

Table Emp(id, name, dept, salary, mgr) rows: (1, 'Asha', 'HR', 50000, NULL), (2, 'Ravi', 'IT', 70000, 1), (3, 'Meena', 'IT', 70000, 2), (4, 'John', 'Sales', 40000, 1), (5, 'Kiran', 'IT', 90000, 2), (6, 'Dev', NULL, 45000, 4), (7, 'Lata', 'Sales', NULL, 4). Table Dept(dname, city) rows: ('HR', 'Pune'), ('IT', 'Delhi'), ('Ops', 'Mumbai'). What does this query return?
SELECT COUNT(*), COUNT(dept), COUNT(DISTINCT dept)
FROM Emp;
  1. A. 6, 6, 3
  2. B. 7, 6, 4
  3. C. 7, 6, 3
  4. D. 7, 7, 4

Correct answer: C. 7, 6, 3

COUNT(*) counts all 7 rows. COUNT(dept) skips Dev's NULL: 6. COUNT(DISTINCT dept) counts HR, IT, Sales = 3; NULL is not counted as a distinct value.

More DBMS and SQL questions

Exams that ask this topic