Q.Aman has used the following SQL command to create a table ‘stu’ : CREATE TABLE stu ( id INTEGER, name VARCHAR(100) ); Then, Aman enters the following SQL statements to enter 4 rows : INSERT INTO stu VALUES (1, "abc"); INSERT INTO stu VALUES (2, "abc"); INSERT INTO stu VALUES (3, "bcd"); Write the output that will be produced by the following SQL statement : SELECT name, Count(*) FROM stu GROUP BY name;
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The query will return two rows: one showing "abc" appears 2 times, and another showing "bcd" appears 1 time, because GROUP BY name clusters identical names together and Count(*) tallies rows in each cluster.
When Aman creates the table stu with columns id and name, he builds a simple two-column structure. The three INSERT statements then populate it with data. Notice that the name "abc" appears twice (with id 1 and id 2), while "bcd" appears once (with id 3). The table now holds three rows in total.
The SELECT statement Aman writes next does something powerful: it groups rows by the name column. Think of GROUP BY name as sorting the table into piles—one pile for each distinct name. Here, we get two piles: one for "abc" (containing two rows) and one for "bcd" (containing one row).
The Count(*) function then counts how many rows sit in each pile. It doesn't care about the content of individual columns; it simply tallies the number of rows that share the same name. So for the "abc" group, the count is 2. For the "bcd" group, the count is 1.
The asterisk in Count(*) means "count all rows in this group," regardless of whether any particular column is NULL or not. It's the most inclusive way to count.
The output format will be two columns: the first showing the name, the second showing the count. SQL typically returns grouped results in the order it encounters distinct values (though this isn't guaranteed without an ORDER BY clause). In practice, you'll see:
| name | Count(*) |
|------|----------| …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.