Question discussion

What is the output of the following SQL query?CREATE TABLE dept(id INT, name TEXT); CREATE TABLE emp(id INT, name TEXT, dept_id INT); INSER…

1147Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following SQL query?

CREATE TABLE dept(id INT, name TEXT);
CREATE TABLE emp(id INT, name TEXT, dept_id INT);
INSERT INTO dept VALUES (1,'HR'),(2,'IT'),(3,'Ops');
INSERT INTO emp VALUES (1,'Ann',1),(2,'Bob',2),
                       (3,'Cid',2),(4,'Dee',2);
SELECT d.name, COUNT(e.id) FROM dept d
LEFT JOIN emp e ON e.dept_id = d.id
GROUP BY d.name ORDER BY d.name;
  • AHR 1, IT 3
  • BHR 1, IT 3, Ops 1
  • CHR 1, IT 3, Ops 0
  • DHR 4, IT 4, Ops 4
Correct answer: C

Explanation

The LEFT JOIN keeps Ops with no employees, and COUNT(e.id) ignores the NULL, giving 0 for Ops.

Discussion 0

Nobody has replied yet. Start the discussion!