Students With Classes
MediumSQLDatabaseMathSorting
Description
Write a SQL query to return each student name with the title of the class they belong to, joining students to classes on classId. Order by student name ascending. **Output columns (in order):** name, title
Table:
students| Column | Type |
|---|---|
| id | INT |
| name | TEXT |
| classId | INT |
Table:
classes| Column | Type |
|---|---|
| id | INT |
| title | TEXT |
Examples
Input:
CREATE TABLE students (id INT, name TEXT, classId INT); CREATE TABLE classes (id INT, title TEXT); INSERT INTO classes VALUES (1,'Math'),(2,'Art'); INSERT INTO students VALUES (1,'Ann',1),(2,'Ben',2),(3,'Cal',1);Output:
Ann|Math
Ben|Art
Cal|MathExplanation:
Each student row is matched to its class through the shared class identifier, returning the student with the class title by name.
Input:
CREATE TABLE students (id INT, name TEXT, classId INT); CREATE TABLE classes (id INT, title TEXT); INSERT INTO classes VALUES (1,'Sci'); INSERT INTO students VALUES (1,'Zoe',1);Output:
Zoe|SciExplanation:
Each student row is matched to its class through the shared class identifier, returning the student with the class title by name.
Input:
CREATE TABLE students (id INT, name TEXT, classId INT); CREATE TABLE classes (id INT, title TEXT); INSERT INTO classes VALUES (5,'History'),(6,'Music'); INSERT INTO students VALUES (1,'Mia',6),(2,'Ned',5);Output:
Mia|Music
Ned|HistoryExplanation:
Each student row is matched to its class through the shared class identifier, returning the student with the class title by name.
Constraints
- •
Use standard SQL (SQLite).