All Exams Test series for 1 year @ ₹349 only
Question

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);

The correct answer is

A and B only

Understanding the Database Schema

The question involves two database tables (relations):

  • STUDENT: Contains information about students.
  • COURSE: Contains information about courses.

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 Goal: Finding Total Students per Course

The objective is to write SQL queries that return two pieces of information for each course:

  1. The name of the course (`coursename`).
  2. The total number of students enrolled in that specific 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).

Analyzing Each SQL Query

Query A Analysis: Using NATURAL JOIN

The first query is:

SELECT coursename, count(*) 'total' from STUDENT natural join COURSE group by coursename;

  • FROM STUDENT natural join COURSE: This performs a NATURAL JOIN between the `STUDENT` table and the `COURSE` table. A NATURAL JOIN automatically joins two tables based on columns with the same name and data type in both tables. In this case, the join will happen on the `courseno` column because it's the only common column name. This effectively links each student record to their corresponding course record.
  • SELECT coursename, count(*) 'total': This specifies the columns to be returned. `coursename` is selected, and `count(*)` is used to count the number of rows in each group. The result of `count(*)` is given an alias 'total'.
  • group by coursename: This is the crucial part for getting counts per course. The rows resulting from the join are grouped based on their `coursename`. The `count(*)` function then operates on each group separately, counting the number of rows (students) within that group (course).

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.

Query B Analysis: Using Implicit Join with WHERE Clause

The second query is:

SELECT C.coursename, count(*) 'total' from STUDENT S, COURSE C where S.courseno = C.courseno group by coursename;

  • FROM STUDENT S, COURSE C: This lists the tables involved, `STUDENT` aliased as `S` and `COURSE` aliased as `C`. Using a comma here indicates an implicit join, initially forming a Cartesian product of the two tables.
  • where S.courseno = C.courseno: This is the join condition applied to the Cartesian product. It filters the results to include only rows where the `courseno` from the `STUDENT` table matches the `courseno` from the `COURSE` table. This achieves the same result as an INNER JOIN on `courseno`.
  • SELECT C.coursename, count(*) 'total': Similar to Query A, this selects the course name (using the alias `C` for the `COURSE` table) and counts the rows in each group, aliasing the count as 'total'.
  • group by coursename: Again, this groups the results by `coursename`, ensuring the `count(*)` is performed for each individual course.

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.

Query C Analysis: Using Subquery and Filtering

The third query is:

SELECT coursename, count(*) 'total' from COURSE C where courseno in (SELECT courseno from STUDENT);

  • FROM COURSE C: This specifies that the query primarily operates on the `COURSE` table.
  • where courseno in (SELECT courseno from STUDENT): This is a filtering condition. The subquery (SELECT courseno from STUDENT) returns a list of all `courseno` values that exist in the `STUDENT` table (i.e., courses that have at least one student enrolled). The outer query then filters the `COURSE` table to include only those courses whose `courseno` is in this list. This correctly identifies courses with enrolled students.
  • SELECT coursename, count(*) 'total': This selects the `coursename` and uses `count(*)`. However, there is no GROUP BY clause in this query. When `count(*)` is used without a `GROUP BY` clause, it counts all the rows that satisfy the `WHERE` condition in the entire result set. In this case, it will count the total number of rows from the `COURSE` table that have at least one student enrolled. It will likely return a single row containing one of the course names (depending on SQL implementation) and the total count of courses with students, not the count of students *per* course.

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.

Summary of Query Analysis

  • Query A correctly uses NATURAL JOIN and GROUP BY to count students per course.
  • Query B correctly uses an implicit join with a WHERE clause condition and GROUP BY to count students per course.
  • Query C uses a subquery to filter courses but lacks a GROUP BY clause, thus failing to count students *per course*.

Therefore, queries A and B correctly solve the problem.

Final Answer

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.

Revision Table: Key SQL Concepts

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.

Additional Information: Counting with GROUP BY

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:

  • SELECT count(*) from STUDENT; — Returns the total number of students in the table.
  • SELECT courseno, count(*) from STUDENT group by courseno; — Returns the count of students for each unique courseno.

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.

Was this answer helpful?

Important Questions from SQL

  1. 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) < 7
  2. Match the following -

    List IList II
    (a)DDL(i)LOCK TABLE
    (b)DML(ii)COMMIT
    (c)TCL(iii)Natural Difference
    (d)Binary operation(iv)REVOKE
  3. Which of the following is used to create a database schema?

  4. 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?

  5. Which of the following statements are DML statements?

    (a) Update [tablename] Set [columnname] = VALUE

    (b) Delete [tablename]

    (c) Select * from [tablename]

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App