Consider the following tables PARTICIPANT and ACTIVITY :
Table : PARTICIPANT
| ADMNO | NAME | HOUSE | ACTIVITYCODE |
|---|---|---|---|
| 6473 | Kapil Shah | Gandhi | A105 |
| 7134 | Joy Mathew | Bose | A101 |
| 8786 | Saba Arora | Gandhi | A102 |
| 6477 | Kapil Shah | Bose | A101 |
| 7658 | Faizal Ahmed | Bhagat | A104 |
Table : ACTIVITY
| ACTIVITYCODE | ACTIVITYNAME | POINTS |
|---|---|---|
| A101 | Running | 200 |
| A102 | Hopping bag | 300 |
| A103 | Skipping | 200 |
| A104 | Bean bag | 250 |
| A105 | Obstacle | 350 |
Write command in SQL for the following : (i) To display Activity Code along with number of participants participating in each activity (Activity Code wise) from the table Participant. OR How many rows will be there in Cartesian product of the two tables in consideration here ?
🔒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 →Part (a)Concept understanding — Group By Aggregation
Group By Aggregation: A First Look
Think of a pile of laundry. You have socks, shirts, towels — all mixed together. If someone asks "how many socks are there?", you first separate the socks from everything else, then count them. That act of separating first, then summarising — that is the core of Group By Aggregation.
In data, we often have rows of information about many different things mixed together. Group By Aggregation is the operation that says: "Take all these rows, sort them into meaningful groups based on some common property, and then for each group, compute a single summary value."
The Two Steps, Always
Every Group By Aggregation has exactly two parts, and they happen in order:
- Grouping — You pick a column (or columns) whose values will define the groups. All rows that share the same value in that column become one group.
- Aggregating — You pick a summary operation to apply to each group. Common operations include counting how many rows are in the group, finding the total or average of a numeric column within the group, or picking the smallest or largest value.
The result is a new table: one row per group, with the group's label and the computed summary.
Why It Matters
Without Group By, you can only summarise the entire dataset at once — one total, one average for everything. That hides almost all the interesting variation. Group By lets you ask questions like:
- For each state, what is the total number of schools?
- For each year, what was the average rainfall?
- For each product category, how many items were sold?
Each of these questions picks a grouping column (state, year, category) and an aggregation (total, average, count). The answer reveals patterns that a single overall number would bury.
A Concrete Example (Without Numbers)
Imagine a table of exam scores for students from different cities. The table has columns: Student Name, City, Score.
If you want to know "how many students appeared from each city", you:
- Group by the
Citycolumn — all rows with the same city name become one group. - Apply the count aggregation — count how many rows are in each group.
The result is a table with two columns: City and Number of Students.
If instead you want "the highest score in each city", you:
- Group by
Cityagain. - Apply the maximum aggregation to the
Scorecolumn.
The result is a table with City and Highest Score.
The grouping column and the aggregation column are often different. The grouping column defines who is in each group; the aggregation column defines what you summarise about that group. You can also aggregate the same column you grouped by — for example, counting how many rows are in each group — but that is a special case, not the general rule.
Common Aggregation Operations
These are the standard summaries you can compute for each group:
- Count — How many rows are in the group.
- Sum — Total of a numeric column across all rows in the group.
- Average — Mean of a numeric column across the group.
- Minimum — Smallest value in a numeric column within the group.
- Maximum — Largest value in a numeric column within the group.
Each of these gives a different lens on the same grouped data.
What the NCERT Textbook Says …
Part (b)Concept understanding — Relational Algebra Operations
Relational Algebra Operations: A First Look
Think of a relational database as a collection of neat, rectangular tables. Each table has rows (records) and columns (attributes). Now, suppose you want to ask questions of this data — "Which customers live in Delhi?" or "Show me all orders placed last month." Relational algebra is the set of basic operations you use to answer such questions. It is the language of queries at the most fundamental level.
You do not need to write code or do math. You only need to understand what each operation does to a table — like a set of tools in a toolbox.
The Core Operations
There are eight classic operations. They fall into two groups: those that work on one table at a time, and those that combine two tables.
Operations on a Single Table
Select (also called Restrict) — This operation picks certain rows from a table based on a condition. For example, from a table of students, you might select only those rows where the city is "Mumbai". The result is a smaller table with the same columns but fewer rows.
Project — This operation picks certain columns from a table. For example, from a student table with columns Roll No, Name, City, and Marks, you might project only Name and City. The result is a table with fewer columns. Duplicate rows are automatically removed.
Rename — This operation simply gives a new name to the resulting table or to its columns. It is useful when you need to refer to the same table more than once in a query, or when you want clearer column headings.
Select and Project are the two most frequently used operations. Select narrows down rows; Project narrows down columns. Together they let you extract exactly the slice of data you need.
Operations That Combine Two Tables
Union — This combines two tables that have the same structure (same number of columns and compatible data types). The result contains all rows that appear in either table, with duplicates removed. Think of it as "add the rows of one table to the rows of another, but keep only unique ones."
Set Difference — This gives you rows that are in the first table but not in the second. For example, "Which students are enrolled in Course A but not in Course B?"
Intersection — This gives you rows that appear in both tables. For example, "Which customers have bought both a laptop and a printer?"
Cartesian Product — This pairs every row of the first table with every row of the second table. If the first table has 10 rows and the second has 5, the result has 50 rows. This operation is rarely used alone — it is the foundation for the most powerful operation of all.
Join — This is the heart of relational algebra. A join combines rows from two tables based on a related column between them. For example, you have a Customers table and an Orders table. The join operation matches each order to the customer who placed it, using the Customer ID column that appears in both tables. The result is a single table with all the customer details alongside their orders.
The Join operation is what makes relational databases relational. Without it, data in separate tables would remain isolated. Joins let you connect information across tables — customers to orders, students to courses, products to suppliers — and answer questions that span multiple pieces of data.
Why Relational Algebra Matters …
Part (a)
Group the PARTICIPANT table by ACTIVITYCODE and count the rows in each group:
SELECT ACTIVITYCODE, COUNT(*) FROM PARTICIPANT GROUP BY ACTIVITYCODE;
A101 appears twice (ADMNO 7134 and 6477); A102, A104, A105 once each. Output:
ACTIVITYCODE COUNT(*)
A101 2
A102 1
A104 1
A105 1
``` …
Part (a): GROUP BY ACTIVITYCODE with COUNT(*) — A101 = 2, A102/A104/A105 = 1 each.
Part (b): Cartesian product = 5 rows x 5 rows = 25 rows.
Part (a) — participants per activity
To show how many participants chose each activity, group the PARTICIPANT rows by ACTIVITYCODE and count each group:
SELECT ACTIVITYCODE, COUNT(*) FROM PARTICIPANT GROUP BY ACTIVITYCODE;
GROUP BY collapses rows sharing the same ACTIVITYCODE into one group; COUNT(*) tallies each group. From the data, A101 is chosen by two participants (Joy Mathew and the second Kapil Shah), while A102, A104 and A105 are chosen by one each:
ACTIVITYCODE COUNT(*)
A101 2
A102 1
A104 1
A105 1
``` …
- CBSE 2026Set 91/41 markMCQQ.A relation in MySQL database consists of 2 tuples and 3 attributes. If 2 attributes are deleted and 4 tuples are added, what will be the cardinality of the relation ? (A) 4 (B) 5 (C) 6 (D) 7
›Reveal solutionSolution
Cardinality refers to the number of tuples (rows) in a relation; deleting attributes affects only the structure, not the row count, so starting with 2 tuples and adding 4 more gives a cardinality of 6.
Understanding what happens to a relation when we modify its structure or content requires clarity on two fundamental terms: cardinality and degree. Cardinality is the number of tuples (rows) in a relation—essentially, how many records the table holds. Degree, on the other hand, is the number of attributes (columns)—the structure of the relation itself.
The question begins with a relation containing 2 tuples and 3 attributes. Picture a small table with three columns and two rows of data. Now, two operations are performed: first, 2 attributes are deleted, and second, 4 tuples are added.
Deleting attributes changes the degree of the relation. If we remove 2 out of 3 attributes, we are left with a single-column table. This operation affects the structure—the width of the table—but it does not touch the number of rows. The 2 tuples that were already present remain in the relation; they simply have fewer fields now. …
- CBSE 2025Set 91/41 markQ.State True or False : If table A has 6 rows and 3 columns, and table B has 5 rows and 2 columns, the Cartesian product of A and B will have 30 rows and 5 columns.
›Reveal solutionSolution
The statement is True — the Cartesian product of two tables combines every row of one with every row of the other, so the number of rows multiplies and the number of columns adds.
Let's think about what the Cartesian product actually does. In relational algebra, when you take the Cartesian product of two tables (say A and B), you are pairing each row of A with every row of B. The result is a new table that contains all possible combinations of rows from the two original tables.
The number of rows in the product is the product of the row counts of A and B. Here, A has 6 rows and B has 5 rows, so the result has 6 x 5 = 30 rows.
What about the columns? The Cartesian product does not merge or eliminate any columns — it simply places all columns from A side by side with all columns from B. So the total number of columns is the sum of the column counts of A and B. A has 3 columns, B has 2 columns, so the result has 3 + 2 = 5 columns.
NoteThis is exactly how the Cartesian product works in relational algebra — it is a cross join, not a natural join. No matching or merging of columns happens; every column from both tables appears in the output. …
- CBSE 2019Set 90/1/11 markQ.Consider the following table ‘Transporter’ that stores the order details about items to be transported. Table : TRANSPORTERWrite the output for the following SQL query :
ORDERNO DRIVERNAME DRIVERGRADE ITEM TRAVELDATE DESTINATION 10012 RAM YADAV A TELEVISION 2019-04-19 MUMBAI 10014 SOMNATH SINGH FURNITURE 2019-01-12 PUNE 10016 MOHAN VERMA B WASHING MACHINE 2019-06-06 LUCKNOW 10018 RISHI SINGH A REFRIGERATOR 2019-04-07 MUMBAI 10019 RADHE MOHAN TELEVISION 2019-05-30 UDAIPUR 10020 BISHEN PRATAP B REFRIGERATOR 2019-05-02 MUMBAI 10021 RAM TELEVISION 2019-05-03 PUNE (ix) SELECT ITEM, COUNT() FROM TRANSPORTER GROUP BY ITEM HAVING COUNT() >1;›Reveal solutionSolution
This query groups orders by the item being transported and then lists only those items that appear in more than one order.
When working with databases, we often need to summarise information rather than just retrieve raw data. The
GROUP BYclause in SQL is a powerful tool for this, allowing us to aggregate rows that share common values into a set of summary rows. This particular query asks us to identify items that have been ordered multiple times, providing a count for each.Let's break down the query step by step to understand its logic:
FROM TRANSPORTER: This is the starting point. The query begins by looking at all the records in theTRANSPORTERtable.GROUP BY ITEM: This is the core of the aggregation. The database will go through theTRANSPORTERtable and collect all rows that have the same value in theITEMcolumn into a single group. For example, all rows whereITEMis 'TELEVISION' will form one group, all rows whereITEMis 'FURNITURE' will form another, and so on.SELECT ITEM, COUNT(*): After grouping, for each distinct group formed by theITEMcolumn, the query will select two pieces of information:- The
ITEMitself (which is the common value for that group). COUNT(*): This is an aggregate function that counts the total number of rows within each specific group. So, for the 'TELEVISION' group,COUNT(*)will tell us how many times 'TELEVISION' appears in the table.
- The
HAVING COUNT(*) > 1: This is a crucial filtering condition. WhileWHEREclauses filter individual rows before grouping,HAVINGclauses filter entire groups after they have been formed and aggregate functions (likeCOUNT(*)) have been calculated. In this case,HAVING COUNT(*) > 1means that only those groups where the count of items is strictly greater than 1 will be included in the final output. Groups with a count of 1 (meaning the item appeared only once) will be discarded.
NoteThe distinction between
WHEREandHAVINGis fundamental.WHEREfilters individual rows beforeGROUP BYprocesses them, whileHAVINGfilters the groups themselves afterGROUP BYhas aggregated the data. You cannot use aggregate functions directly in aWHEREclause.Let's apply this logic to the
TRANSPORTERtable:- Identify unique
ITEMvalues and group them:- TELEVISION
- FURNITURE
- WASHING MACHINE …
🎓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.