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
ColumnType
idINT
nameTEXT
classIdINT
Table:classes
ColumnType
idINT
titleTEXT

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|Math
Explanation:

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|Sci
Explanation:

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|History
Explanation:

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

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.