Consider the following tables (relations): Students: Roll-No Name 18CS101 Ramesh 18CS102 Mukesh 18CS103 Ramesh Performance: Roll-No Course Marks 18CS101 DBMS 60 18CS101 Compiler Design 65 18CS102 DBMS 80 18CS103 DBMS 85 18CS102 Compiler Design 75 18CS103 Operating System 70 Primary keys in the tables are shown using Underline. Now, Consider the following query: SELECT S.Name, Sum (P.Marks) FROM Students S, Performance P WHERE S.Roll-No = P.Roll-No GROUP BY S.Name The number of rows returned by the above query is
2
The question asks us to determine the number of rows returned by a given SQL query executed on two tables: Students and Performance. Let's first examine the structure and data in these tables.
| Roll-No | Name |
|---|---|
| 18CS101 | Ramesh |
| 18CS102 | Mukesh |
| 18CS103 | Ramesh |
| Roll-No | Course | Marks |
|---|---|---|
| 18CS101 | DBMS | 60 |
| 18CS101 | Compiler Design | 65 |
| 18CS102 | DBMS | 80 |
| 18CS103 | DBMS | 85 |
| 18CS102 | Compiler Design | 75 |
| 18CS103 | Operating System | 70 |
The primary keys are Roll-No in both tables. Note that the Students table has duplicate names ('Ramesh') associated with different Roll-Nos.
The SQL query is:
SELECT S.Name, Sum (P.Marks) FROM Students S, Performance P WHERE S.Roll-No = P.Roll-No GROUP BY S.Name
The WHERE S.Roll-No = P.Roll-No clause joins rows from Students and Performance where the Roll-No matches. The intermediate result looks like this:
| S.Roll-No | S.Name | P.Roll-No | P.Course | P.Marks |
|---|---|---|---|---|
| 18CS101 | Ramesh | 18CS101 | DBMS | 60 |
| 18CS101 | Ramesh | 18CS101 | Compiler Design | 65 |
| 18CS102 | Mukesh | 18CS102 | DBMS | 80 |
| 18CS102 | Mukesh | 18CS102 | Compiler Design | 75 |
| 18CS103 | Ramesh | 18CS103 | DBMS | 85 |
| 18CS103 | Ramesh | 18CS103 | Operating System | 70 |
The query now groups the rows from the joined result based on the unique values in the S.Name column. The unique names are 'Ramesh' and 'Mukesh'.
The Sum(P.Marks) aggregate function is applied to the P.Marks column within each group.
The final result set contains one row for each group, showing the group name (S.Name) and the calculated sum of marks.
| S.Name | Sum(P.Marks) |
|---|---|
| Ramesh | 280 |
| Mukesh | 155 |
The final result table shows 2 rows.
Therefore, the number of rows returned by the query is 2.
| Concept | Explanation | Relevance to Query |
|---|---|---|
| JOIN | Combines rows from two or more tables based on a related column. The query uses an inner join on Roll-No. | Merges data from Students and Performance tables to link student names with their marks. |
| GROUP BY | Groups rows that have the same values in specified columns into summary rows. Aggregate functions operate on these groups. | Groups all marks belonging to the same student Name, regardless of their specific Roll-No if different Roll-Nos share the same name. |
| Aggregate Function (Sum) | Performs a calculation on a set of values (a group) and returns a single value. Sum() adds up numerical values. | Calculates the total marks obtained by each student identified by their Name within their respective group. |
| Alias (S, P) | Temporary names given to tables or columns for easier reference. | Used for brevity (S for Students, P for Performance) and to specify which table a column comes from (e.g., S.Name). |
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases. Queries are used to retrieve data from a database. Key clauses include:
In this specific query, the GROUP BY S.Name clause is the most critical part for determining the number of output rows. Because 'Ramesh' appears with two different Roll Numbers (18CS101 and 18CS103) in the Students table, and both of these Roll Numbers have associated performance records, all these performance records for 18CS101 and 18CS103 are grouped under the single name 'Ramesh' by the GROUP BY S.Name clause. Mukesh (18CS102) forms a separate group.
The final result set will contain one row for each unique value produced by the GROUP BY clause.
Match the following -
| List I | List II | ||
| (a) | DDL | (i) | LOCK TABLE |
| (b) | DML | (ii) | COMMIT |
| (c) | TCL | (iii) | Natural Difference |
| (d) | Binary operation | (iv) | REVOKE |
Given the following STUDENT‐COURSE scheme : STUDENT (Rollno, Name, courseno) COURSE (courseno, coursename, capacity), where Rollno is the primary key of relation STUDENT and courseno is the primary key of relation COURSE. Attribute coursename of COURSE takes unique values only. Which of the following query(ies) will find total number of students enrolled in each course, along with its coursename.
A. SELECT coursename, count(*) 'total' from STUDENT natural join COURSE group by coursename;
B. SELECT C.coursename, count(*) 'total' from STUDENT S, COURSE C where S.courseno = C.courseno group by coursename;
C. SELECT coursename, count(*) 'total' from COURSE C where courseno in (SELECT courseno from STUDENT);
Which of the following is used to create a database schema?
Which of the component module of DBMS does rearrangement and possible ordering of operations, eliminate redundancy in query and use efficient algorithms and indexes during the execution of a query?
Which of the following statements are DML statements?
(a) Update [tablename] Set [columnname] = VALUE
(b) Delete [tablename]
(c) Select * from [tablename]