I have a table called "Studies" and it has these columns:
+-------------+---------------+-------------+------------+-------------+
| CodeStudent | Type Study | Title Study | Date Study | Place Study |
+-------------+---------------+-------------+------------+-------------+
| 10 | Technical | TitleOne | 01-02-2005 | Narnia |
+-------------+---------------+-------------+------------+-------------+
| 10 | Technological | TitleTwo | 01-05-2009 | Mars |
+-------------+---------------+-------------+------------+-------------+
| 10 | University | TitleThree | 01-08-2012 | Gotham City |
+-------------+---------------+-------------+------------+-------------+
| 20 | Technical | OtherTitle | 01-06-2011 | Namek |
+-------------+---------------+-------------+------------+-------------+
And I have a table called "Students" with the following columns and information:
+-------------+---------------+-------------+------------+
| CodeStudent | Name | LastName | BirthDate |
+-------------+---------------+-------------+------------+
| 10 | Hug | Lobezno | 02-02-2002 |
+-------------+---------------+-------------+------------+
| 20 | Son | Gokú | 05-06-2007 |
+-------------+---------------+-------------+------------+
Where the following should be considered:
- The CodeStudent column of the "Students" table is a primary key and the CodeStudent column of the "Studies" table is its respective foreign key
- The only types of studies that exist are Technical, Technological and University
- It is necessary to say that not all students have the three types of studies (Technical, Technological and University), some of them may have two studies, a single study (as in the case of Son Goku student) or none.
Do I need to know if it is possible to show all the information in a single row?
If the student's information with code 10 is requested, it should show this:
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
| CodeStudent | Name | LastName | BirthDate | TitleTechnical | DateTechnical | PlaceTechnical | TitleTechnological | DateTechnological | PlaceTechnological | TitleUniversity | DateUniversity | PlaceUniversity |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
| 10 | Hug | Lobezno | 02-02-2002 | TitleOne | 01-02-2005 | Narnia | TitleTwo | 01-05-2009 | Mars | TitleThree | 01-08-2012 | Gotham City |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
And if the student's information with code 20 is requested, it should show this:
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
| CodeStudent | Name | LastName | BirthDate | TitleTechnical | DateTechnical | PlaceTechnical | TitleTechnological | DateTechnological | PlaceTechnological | TitleUniversity | DateUniversity | PlaceUniversity |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
| 20 | Son | Goku | 05-06-2007 | OtherTitle | 01-06-2011 | Namek | NULL | NULL | NULL | NULL | NULL | NULL |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
I know there is a way and it is this:
SELECT
S.CodeStudent,
S.Name,
S.LastName,
S.BirthDate,
(SELECT TOP 1 ST.TitleStudy FROM Studies ST WHERE ST.CodeStudent = 10 AND ST.TypeStudy = 'Technical') AS TitleTechnical,
(SELECT TOP 1 ST.DateStudy FROM Studies ST WHERE ST.CodeStudent = 10 AND ST.TypeStudy = 'Technical') AS DateTechnical,
...
FROM Student S
WHERE S.CodeStudent = 10
But is there a better and optimal way to do it? :p