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
| AdmNo | Name | StCode |
|---|---|---|
| 211 | Jay | NULL |
| 241 | Aditya | S03 |
| 290 | Diksha | S01 |
| 333 | Jasqueen | S02 |
| 356 | Vedika | S01 |
| 380 | Ashpreet | S03 |
Stream
| StCode | Stream |
|---|---|
| S01 | Science |
| S02 | Commerce |
| S03 | Humanities |
Write SQL queries for the following:
- Create the database Streams_Of_Students.
- Create the table Student by choosing appropriate data types based on the data given in the table.
- Identify the Primary keys from tables Student and Stream. Also identify the foreign key from the table Stream.
- Jay has now changed his stream to Humanities. Write an appropriate SQL query to reflect this change.
- Display the names of students whose names end with the character 'a'. Also, arrange the students in alphabetical order.
- 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).
- List the number of students in each stream having more than 1 student.
- Display the names of students enrolled in different streams, where students are arranged in descending order of admission number.
- Show the Cartesian product on the Student and Stream table. Also mention the degree and cardinality produced after applying the Cartesian product.
- Add a new column 'TeacherIncharge' in the Stream table. Insert appropriate data in each row.
- List the names of teachers and students.
- If Cartesian product is again applied on Student and Stream tables, what will be the degree and cardinality of this modified table?
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;INTis appropriate.Name VARCHAR(50): Variable-length string for names; 50 characters accommodate most names.StCode CHAR(3): Fixed 3-character codes (S01,S02,S03) —CHARis more efficient thanVARCHARfor fixed-width data.StCodeallowsNULL(noNOT NULLconstraint) because Jay's record shows a student can exist without an assigned stream.- The foreign key constraint ensures
StCodevalues (when not NULL) must exist in theStreamtable.
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:
StCodereferencesStream(StCode).
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:
UPDATEmodifies existing rows.WHERE Name = 'Jay'targets Jay's record specifically.SET StCode = 'S03'assigns the Humanities stream code.
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:
| AdmNo | Name | StCode |
|---|---|---|
| 211 | Jay | S03 |
(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;%amatches 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:
JOINcombinesStudentandStreamon matchingStCodevalues.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 |
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:
WHEREfilters rows before grouping.HAVINGfilters groups after aggregation.COUNT(*) > 1is an aggregate condition, soHAVINGis required.
Output (after Jay's update in part d):
| Stream | StudentCount |
|---|---|
| Science | 2 |
| Humanities | 3 |
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):
| Name | Stream |
|---|---|
| Ashpreet | Humanities |
| Vedika | Science |
| Jasqueen | Commerce |
| Diksha | Science |
| Aditya | Humanities |
| Jay | Humanities |
Jay (211) appears last because 211 is the smallest admission number.
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):
Studenthas 3 columns (AdmNo,Name,StCode).Streamhas 2 columns (StCode,Stream).- Cartesian product: columns.
Cardinality (number of rows):
Studenthas 6 rows.Streamhas 3 rows.- Cartesian product: rows.
Sample output (first few rows):
| AdmNo | Name | StCode | StCode | Stream |
|---|---|---|---|---|
| 211 | Jay | S03 | S01 | Science |
| 211 | Jay | S03 | S02 | Commerce |
| 211 | Jay | S03 | S03 | Humanities |
| 241 | Aditya | S03 | S01 | Science |
| 241 | Aditya | S03 | S02 | Commerce |
| ... | ... | ... | ... | ... |
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.