Question discussion

What is the output of the following SQL query?CREATE TABLE c(id INT, name TEXT); INSERT INTO c VALUES (1,'A'),(2,'B'),(3,'C'); CREATE TABLE…

1319Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following SQL query?

CREATE TABLE c(id INT, name TEXT);
INSERT INTO c VALUES (1,'A'),(2,'B'),(3,'C');
CREATE TABLE o(id INT, cid INT, amt INT);
INSERT INTO o VALUES (1,1,100),(2,1,50),(3,3,70);
SELECT c.name, COUNT(o.id) FROM c LEFT JOIN o
ON c.id = o.cid GROUP BY c.name ORDER BY c.name;
  • AA 2, C 1
  • BA 150, B 0, C 70
  • CA 2, B 1, C 1
  • DA 2, B 0, C 1
Correct answer: D

Explanation

The LEFT JOIN keeps customer B with no orders (o.id is NULL), and COUNT(o.id) ignores NULLs, giving 2, 0 and 1.

Discussion 0

Nobody has replied yet. Start the discussion!