Skip to content
Exercises · Q5
Q.

Consider the following tables Student and Stream in the Streams_of_Students database. The primary key of the Stream table is StCode (stream code), which is the foreign key in the Student table. The primary key of the Student table is AdmNo (admission number).

Student

AdmNoNameStCode
211JayNULL
241AdityaS03
290DikshaS01
333JasqueenS02
356VedikaS01
380AshpreetS03

Stream

StCodeStream
S01Science
S02Commerce
S03Humanities

Write SQL queries for the following:

  1. Create the database Streams_Of_Students.
  2. Create the table Student by choosing appropriate data types based on the data given in the table.
  3. Identify the Primary keys from tables Student and Stream. Also identify the foreign key from the table Stream.
  4. Jay has now changed his stream to Humanities. Write an appropriate SQL query to reflect this change.
  5. Display the names of students whose names end with the character 'a'. Also, arrange the students in alphabetical order.
  6. Display the names of students enrolled in Science and Humanities streams, ordered by student name in alphabetical order, then by admission number in ascending order (for duplicating names).
  7. List the number of students in each stream having more than 1 student.
  8. Display the names of students enrolled in different streams, where students are arranged in descending order of admission number.
  9. Show the Cartesian product on the Student and Stream table. Also mention the degree and cardinality produced after applying the Cartesian product.
  10. Add a new column 'TeacherIncharge' in the Stream table. Insert appropriate data in each row.
  11. List the names of teachers and students.
  12. If Cartesian product is again applied on Student and Stream tables, what will be the degree and cardinality of this modified table?
CBSENCERTSubjective· 5mImportance★★★★★
48% · 19/40 Questions
🔒 Locked · start free trial →

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 →

A comprehensive SQL exercise covering database creation, table definition with primary/foreign keys, DML operations (UPDATE, INSERT, ALTER), SELECT queries with filtering/sorting/grouping, joins, and Cartesian products on a student-stream database.


Understanding the Schema

The database models students enrolled in different academic streams. The Stream table holds the master list of streams (Science, Commerce, Humanities), each identified by StCode. The Student table references this through a foreign key StCode, establishing a many-to-one relationship: many students can belong to one stream, and a student may have no stream assigned (NULL).

Primary keys uniquely identify rows. Foreign keys enforce referential integrity — every non-NULL StCode in Student must exist in Stream. This design prevents orphaned records and maintains data consistency.


(a) Create the database

CREATE DATABASE Streams_Of_Students;
USE Streams_Of_Students;

The USE statement switches the active database context so subsequent commands operate on Streams_Of_Students.


(b) Create the Student table

CREATE TABLE Student (
    AdmNo INT PRIMARY KEY,
    Name VARCHAR(50) NOT NULL,
    StCode CHAR(3),
    FOREIGN KEY (StCode) REFERENCES Stream(StCode)
);

Key design choices:

  • AdmNo INT: Admission numbers are numeric identifiers; INT is appropriate.
  • Name VARCHAR(50): Variable-length string for names; 50 characters accommodate most names.
  • StCode CHAR(3): Fixed 3-character codes (S01, S02, S03) — CHAR is more efficient than VARCHAR for fixed-width data.
  • StCode allows NULL (no NOT NULL constraint) because Jay's record shows a student can exist without an assigned stream.
  • The foreign key constraint ensures StCode values (when not NULL) must exist in the Stream table.
Watch out

The Stream table must be created before Student because the foreign key references it. If you try to create Student first, the DBMS will reject the foreign key constraint.

Stream table creation (prerequisite):

CREATE TABLE Stream (
    StCode CHAR(3) PRIMARY KEY,
    Stream VARCHAR(20) NOT NULL
);

(c) Identify Primary and Foreign Keys

Primary Keys:

  • Student table: AdmNo — uniquely identifies each student.
  • Stream table: StCode — uniquely identifies each stream.

Foreign Key:

  • Student table: StCode references Stream(StCode).
Note

The question asks for the foreign key "from the table Stream," but this is a misstatement. The foreign key exists in the Student table and references the Stream table. The Stream table itself has no foreign key in this schema.


(d) Update Jay's stream to Humanities

Jay currently has StCode = NULL. Humanities corresponds to StCode = 'S03'.

UPDATE Student
SET StCode = 'S03'
WHERE Name = 'Jay';

Why this works:

  • UPDATE modifies existing rows.
  • WHERE Name = 'Jay' targets Jay's record specifically.
  • SET StCode = 'S03' assigns the Humanities stream code.
Tip

Using WHERE AdmNo = 211 is safer than WHERE Name = 'Jay' if names might duplicate. Primary keys guarantee uniqueness.

After this update, Jay's row becomes:

AdmNoNameStCode
211JayS03

(e) Display students whose names end with 'a', alphabetically

SELECT Name
FROM Student
WHERE Name LIKE '%a'
ORDER BY Name;

Explanation:

  • LIKE '%a': The % wildcard matches any sequence of characters; %a matches names ending with 'a'.
  • ORDER BY Name: Sorts results alphabetically (ascending by default).

Output:

Name
Aditya
Diksha
Vedika

Checking every student's name against the %a pattern: Jay (no), Aditya (yes — ends in 'a'), Diksha (yes), Jasqueen (no), Vedika (yes), Ashpreet (no). Three names match, sorted alphabetically.


(f) Display students in Science and Humanities, ordered by name then admission number

SELECT s.Name
FROM Student s
JOIN Stream st ON s.StCode = st.StCode
WHERE st.Stream IN ('Science', 'Humanities')
ORDER BY s.Name, s.AdmNo;

Key points:

  • JOIN combines Student and Stream on matching StCode values.
  • WHERE st.Stream IN ('Science', 'Humanities') filters to the two specified streams.
  • ORDER BY s.Name, s.AdmNo: Primary sort by name; if names tie, secondary sort by admission number.

Output (after Jay's update in part d):

Name
Aditya
Ashpreet
Diksha
Jay
Vedika
Note

The question mentions "for duplicating names," but the current data has no duplicate names. If two students named "Aditya" existed with AdmNo 241 and 250, they would appear as Aditya (241), then Aditya (250).


(g) List streams with more than 1 student

SELECT st.Stream, COUNT(*) AS StudentCount
FROM Student s
JOIN Stream st ON s.StCode = st.StCode
GROUP BY st.Stream
HAVING COUNT(*) > 1;

Why HAVING not WHERE:

  • WHERE filters rows before grouping.
  • HAVING filters groups after aggregation.
  • COUNT(*) > 1 is an aggregate condition, so HAVING is required.

Output (after Jay's update in part d):

StreamStudentCount
Science2
Humanities3

Science has Diksha and Vedika (2). Humanities has Aditya, Ashpreet, and Jay — updated in part (d) — for 3. Commerce has only Jasqueen (1 student), so it's excluded by the HAVING COUNT(*) > 1 filter.


(h) Display students and their streams, ordered by admission number descending

SELECT s.Name, st.Stream
FROM Student s
JOIN Stream st ON s.StCode = st.StCode
ORDER BY s.AdmNo DESC;

Output (after Jay's update):

NameStream
AshpreetHumanities
VedikaScience
JasqueenCommerce
DikshaScience
AdityaHumanities
JayHumanities

Jay (211) appears last because 211 is the smallest admission number.

Watch out

The JOIN excludes students with NULL StCode. If Jay's stream were still NULL, he wouldn't appear in this result. Use LEFT JOIN to include all students regardless of stream assignment.


(i) Cartesian product and its degree/cardinality

SELECT *
FROM Student, Stream;

Or equivalently:

SELECT *
FROM Student CROSS JOIN Stream;

What is a Cartesian product?

Every row in Student pairs with every row in Stream, producing all possible combinations.

Degree (number of columns):

  • Student has 3 columns (AdmNo, Name, StCode).
  • Stream has 2 columns (StCode, Stream).
  • Cartesian product: 3+2=53 + 2 = 5 columns.

Cardinality (number of rows):

  • Student has 6 rows.
  • Stream has 3 rows.
  • Cartesian product: 6×3=186 \times 3 = 18 rows.

Sample output (first few rows):

AdmNoNameStCodeStCodeStream
211JayS03S01Science
211JayS03S02Commerce
211JayS03S03Humanities
241AdityaS03S01Science
241AdityaS03S02Commerce
...............

Notice the column name collision: both tables have StCode, so the result has two StCode columns (the DBMS may rename them as StCode and StCode_1 or require table prefixes in practice).

--- …

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.