Mysql Help with INNER JOIN

Asked

Viewed 43 times

1

Talk people, good night! I’m breaking my balls here, I wonder if questions 3 and 4 are correct and how the syntax would be structured if I answer them using INNER JOIN and if it is more suitable.

We have a teacher and student management system in a school based on a Mysql database, which has 3 tables (teacher, student and relationship) with the following content:

tabela professor e aluno

tabela relacionamento

All tables have a primary index called "id", which uniquely identifies each record. The "relationship" table is the one that relates teachers to students. We need you to build the necessary Mysql commands to get the following information:

1. Name of the science teacher

SELECT * FROM professor WHERE assinatura=ciencias;

2. Count how many students are in school

SELECT COUNT(id) FROM aluno;

3. List of names with all students of the science teacher, taking into account that the only data we have about the teacher is that his signature is "science".

SELECT *
FROM aluno a,
           professor p,
           relacionamento r
WHERE p.assinatura = 'ciencias'
AND p.id = r.professor_id
AND r.aluno_id = a.id

4. List of students who are not in science class, taking into account that the only data we have on the teacher is that his signature is "science".

SELECT *
FROM aluno a,
           professor p,
           relacionamento r
WHERE p.assinatura != 'ciencias'
AND p.id = r.professor_id
AND r.aluno_id = a.id

2 answers

0


  1. Name of the science teacher

    SELECT * FROM teacher WHERE subscription='ciencias';

The quotes were missing.

  1. List of names of all the science teacher’s students, taking into account that the only data we have on the teacher is that his signature is "science". Using joint:

    SELECT * FROM student to INNER JOIN relationship r ON (r.aluno_id = a.id) INNER JOIN teacher p ON (p.id = r.professor_id) WHERE p.signature = 'ciencias'

  2. List of students who are not in science class, taking into account that the only data we have on the teacher is that his signature is "science". Using joint:

    SELECT * FROM student to INNER JOIN relationship r ON (r.aluno_id = a.id) INNER JOIN teacher p ON (p.id = r.professor_id) WHERE p.signature = 'science'

Although in your table above there is no record with signature = 'ciencia' (exists with 'ciencias').

0

No, you are wrong. Until it works, but after 92 started to use INNER Join

  1. SELECT * FROM student to INNER JOIN relationship r ON (r.aluno_id = a.id) INNER JOIN teacher p ON (p.id = r.professor_id) WHERE p.signature = 'ciencias'

  2. SELECT * FROM student to INNER JOIN relationship r ON (r.aluno_id = a.id) INNER JOIN teacher p ON (p.id = r.professor_id) WHERE p.signature <> 'ciencias'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.