{ "data": { "question": { "questionId": "1415", "questionFrontendId": "1280", "boundTopicId": null, "title": "Students and Examinations", "titleSlug": "students-and-examinations", "content": "

Table: Students

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| student_id    | int     |\n| student_name  | varchar |\n+---------------+---------+\nstudent_id is the primary key (column with unique values) for this table.\nEach row of this table contains the ID and the name of one student in the school.\n
\n\n

 

\n\n

Table: Subjects

\n\n
\n+--------------+---------+\n| Column Name  | Type    |\n+--------------+---------+\n| subject_name | varchar |\n+--------------+---------+\nsubject_name is the primary key (column with unique values) for this table.\nEach row of this table contains the name of one subject in the school.\n
\n\n

 

\n\n

Table: Examinations

\n\n
\n+--------------+---------+\n| Column Name  | Type    |\n+--------------+---------+\n| student_id   | int     |\n| subject_name | varchar |\n+--------------+---------+\nThere is no primary key (column with unique values) for this table. It may contain duplicates.\nEach student from the Students table takes every course from the Subjects table.\nEach row of this table indicates that a student with ID student_id attended the exam of subject_name.\n
\n\n

 

\n\n

Write a solution to find the number of times each student attended each exam.

\n\n

Return the result table ordered by student_id and subject_name.

\n\n

The result format is in the following example.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nStudents table:\n+------------+--------------+\n| student_id | student_name |\n+------------+--------------+\n| 1          | Alice        |\n| 2          | Bob          |\n| 13         | John         |\n| 6          | Alex         |\n+------------+--------------+\nSubjects table:\n+--------------+\n| subject_name |\n+--------------+\n| Math         |\n| Physics      |\n| Programming  |\n+--------------+\nExaminations table:\n+------------+--------------+\n| student_id | subject_name |\n+------------+--------------+\n| 1          | Math         |\n| 1          | Physics      |\n| 1          | Programming  |\n| 2          | Programming  |\n| 1          | Physics      |\n| 1          | Math         |\n| 13         | Math         |\n| 13         | Programming  |\n| 13         | Physics      |\n| 2          | Math         |\n| 1          | Math         |\n+------------+--------------+\nOutput: \n+------------+--------------+--------------+----------------+\n| student_id | student_name | subject_name | attended_exams |\n+------------+--------------+--------------+----------------+\n| 1          | Alice        | Math         | 3              |\n| 1          | Alice        | Physics      | 2              |\n| 1          | Alice        | Programming  | 1              |\n| 2          | Bob          | Math         | 1              |\n| 2          | Bob          | Physics      | 0              |\n| 2          | Bob          | Programming  | 1              |\n| 6          | Alex         | Math         | 0              |\n| 6          | Alex         | Physics      | 0              |\n| 6          | Alex         | Programming  | 0              |\n| 13         | John         | Math         | 1              |\n| 13         | John         | Physics      | 1              |\n| 13         | John         | Programming  | 1              |\n+------------+--------------+--------------+----------------+\nExplanation: \nThe result table should contain all students and all subjects.\nAlice attended the Math exam 3 times, the Physics exam 2 times, and the Programming exam 1 time.\nBob attended the Math exam 1 time, the Programming exam 1 time, and did not attend the Physics exam.\nAlex did not attend any exams.\nJohn attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.\n
\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 1237, "dislikes": 161, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "{\"headers\":{\"Students\":[\"student_id\",\"student_name\"],\"Subjects\":[\"subject_name\"],\"Examinations\":[\"student_id\",\"subject_name\"]},\"rows\":{\"Students\":[[1,\"Alice\"],[2,\"Bob\"],[13,\"John\"],[6,\"Alex\"]],\"Subjects\":[[\"Math\"],[\"Physics\"],[\"Programming\"]],\"Examinations\":[[1,\"Math\"],[1,\"Physics\"],[1,\"Programming\"],[2,\"Programming\"],[1,\"Physics\"],[1,\"Math\"],[13,\"Math\"],[13,\"Programming\"],[13,\"Physics\"],[2,\"Math\"],[1,\"Math\"]]}}", "categoryTitle": "Database", "contributors": [], "topicTags": [ { "name": "Database", "slug": "database", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below\n", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef students_and_examinations(students: pd.DataFrame, subjects: pd.DataFrame, examinations: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"129.9K\", \"totalSubmission\": \"230.9K\", \"totalAcceptedRaw\": 129906, \"totalSubmissionRaw\": 230884, \"acRate\": \"56.3%\"}", "hints": [], "solution": { "id": "2018", "canSeeDetail": false, "paidOnly": true, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "{\"headers\":{\"Students\":[\"student_id\",\"student_name\"],\"Subjects\":[\"subject_name\"],\"Examinations\":[\"student_id\",\"subject_name\"]},\"rows\":{\"Students\":[[1,\"Alice\"],[2,\"Bob\"],[13,\"John\"],[6,\"Alex\"]],\"Subjects\":[[\"Math\"],[\"Physics\"],[\"Programming\"]],\"Examinations\":[[1,\"Math\"],[1,\"Physics\"],[1,\"Programming\"],[2,\"Programming\"],[1,\"Physics\"],[1,\"Math\"],[13,\"Math\"],[13,\"Programming\"],[13,\"Physics\"],[2,\"Math\"],[1,\"Math\"]]}}", "metaData": "{\"mysql\": [\"Create table If Not Exists Students (student_id int, student_name varchar(20))\", \"Create table If Not Exists Subjects (subject_name varchar(20))\", \"Create table If Not Exists Examinations (student_id int, subject_name varchar(20))\"], \"mssql\": [\"Create table Students (student_id int, student_name varchar(20))\", \"Create table Subjects (subject_name varchar(20))\", \"Create table Examinations (student_id int, subject_name varchar(20))\"], \"oraclesql\": [\"Create table Students (student_id int, student_name varchar(20))\\n\", \"Create table Subjects (subject_name varchar(20))\\n\", \"Create table Examinations (student_id int, subject_name varchar(20))\"], \"database\": true, \"name\": \"students_and_examinations\", \"pythondata\": [\"Students = pd.DataFrame([], columns=['student_id', 'student_name']).astype({'student_id':'Int64', 'student_name':'object'})\", \"Subjects = pd.DataFrame([], columns=['subject_name']).astype({'subject_name':'object'})\", \"Examinations = pd.DataFrame([], columns=['student_id', 'subject_name']).astype({'student_id':'Int64', 'subject_name':'object'})\"], \"manual\": false, \"postgresql\": [\"\\nCreate table If Not Exists Students (student_id int, student_name varchar(20))\\n\", \"Create table If Not Exists Subjects (subject_name varchar(20))\", \"Create table If Not Exists Examinations (student_id int, subject_name varchar(20))\"], \"database_schema\": {\"Students\": {\"student_id\": \"INT\", \"student_name\": \"VARCHAR(20)\"}, \"Subjects\": {\"subject_name\": \"VARCHAR(20)\"}, \"Examinations\": {\"student_id\": \"INT\", \"subject_name\": \"VARCHAR(20)\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Students (student_id int, student_name varchar(20))", "Create table If Not Exists Subjects (subject_name varchar(20))", "Create table If Not Exists Examinations (student_id int, subject_name varchar(20))", "Truncate table Students", "insert into Students (student_id, student_name) values ('1', 'Alice')", "insert into Students (student_id, student_name) values ('2', 'Bob')", "insert into Students (student_id, student_name) values ('13', 'John')", "insert into Students (student_id, student_name) values ('6', 'Alex')", "Truncate table Subjects", "insert into Subjects (subject_name) values ('Math')", "insert into Subjects (subject_name) values ('Physics')", "insert into Subjects (subject_name) values ('Programming')", "Truncate table Examinations", "insert into Examinations (student_id, subject_name) values ('1', 'Math')", "insert into Examinations (student_id, subject_name) values ('1', 'Physics')", "insert into Examinations (student_id, subject_name) values ('1', 'Programming')", "insert into Examinations (student_id, subject_name) values ('2', 'Programming')", "insert into Examinations (student_id, subject_name) values ('1', 'Physics')", "insert into Examinations (student_id, subject_name) values ('1', 'Math')", "insert into Examinations (student_id, subject_name) values ('13', 'Math')", "insert into Examinations (student_id, subject_name) values ('13', 'Programming')", "insert into Examinations (student_id, subject_name) values ('13', 'Physics')", "insert into Examinations (student_id, subject_name) values ('2', 'Math')", "insert into Examinations (student_id, subject_name) values ('1', 'Math')" ], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"

MySQL 8.0.

\"], \"mssql\": [\"MS SQL Server\", \"

mssql server 2019.

\"], \"oraclesql\": [\"Oracle\", \"

Oracle Sql 11.2.

\"], \"pythondata\": [\"Pandas\", \"

Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0

\"], \"postgresql\": [\"PostgreSQL\", \"

PostgreSQL 16

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }