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);
A and B only
The question involves two database tables (relations):
Let's look at the attributes (columns) in each table:
| Table | Attributes | Primary Key |
|---|---|---|
| STUDENT | Rollno, Name, courseno | Rollno |
| COURSE | courseno, coursename, capacity | courseno |
The `courseno` attribute is present in both tables and links students to the courses they are enrolled in. The `coursename` attribute in the `COURSE` table is unique.
The objective is to write SQL queries that return two pieces of information for each course:
This requires joining the `STUDENT` and `COURSE` tables based on the common attribute `courseno` and then grouping the results by `coursename` to count the students for each group (course).
The first query is:
SELECT coursename, count(*) 'total' from STUDENT natural join COURSE group by coursename;
Conclusion for Query A: This query correctly joins the tables on `courseno`, groups the results by `coursename`, and counts the number of students in each group. It successfully finds the total number of students enrolled in each course along with its name.
The second query is:
SELECT C.coursename, count(*) 'total' from STUDENT S, COURSE C where S.courseno = C.courseno group by coursename;
Conclusion for Query B: This query also correctly joins the tables based on `courseno` using a WHERE clause condition, groups the results by `coursename`, and counts the students per course. It is a standard way to write an inner join query for this task.
The third query is:
SELECT coursename, count(*) 'total' from COURSE C where courseno in (SELECT courseno from STUDENT);
Conclusion for Query C: While the `WHERE` clause correctly filters for courses with students, the lack of a `GROUP BY coursename` clause means it does not count the students *for each specific course*. It counts the total number of qualifying rows from the `COURSE` table, which is not the required output.
Therefore, queries A and B correctly solve the problem.
Based on the analysis, both Query A and Query B correctly find the total number of students enrolled in each course, along with the coursename. Query C does not produce the required per-course count.
| Concept | Description | Purpose in Queries A & B |
|---|---|---|
| JOIN (Implicit/Explicit) | Combining rows from two or more tables based on a related column. | Used to link students to their respective courses via courseno. |
| NATURAL JOIN | A type of join that automatically joins tables based on columns with the same name. | Used in Query A to join STUDENT and COURSE on courseno. |
| WHERE Clause | Filters records based on a specified condition. Can be used for join conditions. | Used in Query B (S.courseno = C.courseno) to match student records with course records. Used in Query C to filter courses with students. |
| GROUP BY Clause | Groups rows that have the same values in specified columns into summary rows. | Essential in Queries A & B to group results by coursename before counting students for each course. |
| Aggregate Function: COUNT(*) | Counts the number of rows in a group or the total number of rows if no GROUP BY is specified. | Used in all queries to count rows; correctly counts students per group in A & B, counts filtered courses in C. |
| Subquery (Nested Query) | A query nested inside another query. Executes first, and its result is used by the outer query. | Used in Query C (SELECT courseno from STUDENT) to find courses with students. |
When using aggregate functions like COUNT(), SUM(), AVG(), MIN(), or MAX(), the GROUP BY clause is fundamental for performing calculations on subsets of rows rather than the entire result set.
If you use an aggregate function in a SELECT statement and you also select non-aggregated columns, you must include all the non-aggregated columns in the GROUP BY clause. For example, in Queries A and B, we select coursename (a non-aggregated column) and count(*) (an aggregate function), so we must group by coursename.
Consider the difference:
In our problem, we needed the count of students *per course name*, which is why joining with the `COURSE` table to get the `coursename` and then grouping by `coursename` was necessary for queries A and B.
Consider a relation book (title, price) which contains the titles and prices of different books.
Assuming that no two books have the same price, what does the following SQL query list ?
Select title
from book as B
where (select count (*)
from book as T
where T.price > B.price) < 7Match 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 |
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]