Need a solution to solve sql problem

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3153073%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3ENeed%20a%20solution%20to%20solve%20sql%20problem%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3153073%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%3EHello%20everyone%2C%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EThis%20is%20Sophie%2C%20I%20am%20new%20to%20this%20community.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3ECurrently%20I%20am%20stuck%20on%20an%20sql%20question%2C%20is%20anyone%20able%20to%20help%20on%20it%3F%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CU%3E%3CEM%3EQuestion%26lt%3B%5C%2FEM%26gt%3B%26lt%3B%5C%2FU%26gt%3B%26lt%3B%5C%2FSTRONG%26gt%3B%3A%26lt%3B%5C%2FP%26gt%3B%3C%2FEM%3E%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EYou%20are%20given%20two%20tables%3A%26nbsp%3B%3CEM%3EStudents%26lt%3B%5C%2FEM%26gt%3B%26nbsp%3Band%20%3CEM%3EGrades%26lt%3B%5C%2FEM%26gt%3B.%26nbsp%3B%3CEM%3EStudents%26lt%3B%5C%2FEM%26gt%3B%26nbsp%3Bcontains%20three%20columns%20%3CEM%3EID%26lt%3B%5C%2FEM%26gt%3B%2C%20%3CEM%3EName%26lt%3B%5C%2FEM%26gt%3B%20and%20%3CEM%3EMarks%26lt%3B%5C%2FEM%26gt%3B.%26lt%3B%5C%2FP%26gt%3B%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FP%3E%3CDIV%20class%3D%22%5C%26quot%3B%5C%26quot%3B%22%3E%3CSPAN%20class%3D%22%5C%26quot%3Blia-inline-image-display-wrapper%22%20lia-image-align-inline%3D%22%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fgxcuf89792%2F%5C%26quot%3Bhttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F347244iFC0118D13E7BD0DB%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%5C%26quot%3B%22%20role%3D%22%5C%26quot%3Bbutton%5C%26quot%3B%22%20title%3D%22Students.png%22%20alt%3D%22%5C%26quot%3BStudents.png%5C%26quot%3B%22%20%2F%3E%26lt%3B%5C%2Fspan%26gt%3B%26lt%3B%5C%2FDIV%26gt%3B%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CEM%3EGrades%26lt%3B%5C%2FEM%26gt%3B%20contains%20the%20following%20data%3A%26lt%3B%5C%2FP%26gt%3B%3C%2FEM%3E%3C%2FP%3E%3CDIV%20class%3D%22%5C%26quot%3B%5C%26quot%3B%22%3E%3CSPAN%20class%3D%22%5C%26quot%3Blia-inline-image-display-wrapper%22%20lia-image-align-inline%3D%22%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fgxcuf89792%2F%5C%26quot%3Bhttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F347245i0EBB1D109A687AB5%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%5C%26quot%3B%22%20role%3D%22%5C%26quot%3Bbutton%5C%26quot%3B%22%20title%3D%22Grades.png%22%20alt%3D%22%5C%26quot%3BGrades.png%5C%26quot%3B%22%20%2F%3E%26lt%3B%5C%2Fspan%26gt%3B%26lt%3B%5C%2FDIV%26gt%3B%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CEM%3EKetty%26lt%3B%5C%2FEM%26gt%3B%20gives%20%3CEM%3EEve%26lt%3B%5C%2FEM%26gt%3B%20a%20task%20to%20generate%20a%20report%20containing%20three%20columns%3A%20%3CEM%3EName%26lt%3B%5C%2FEM%26gt%3B%2C%20%3CEM%3EGrade%26lt%3B%5C%2FEM%26gt%3B%20and%20%3CEM%3EMark%26lt%3B%5C%2FEM%26gt%3B.%20%3CEM%3EKetty%26lt%3B%5C%2FEM%26gt%3B%20doesn't%20want%20the%20NAMES%20of%20those%20students%20who%20received%20a%20grade%20lower%20than%20%3CEM%3E8%26lt%3B%5C%2FEM%26gt%3B.%20The%20report%20must%20be%20in%20descending%20order%20by%20grade%20--%20i.e.%20higher%20grades%20are%20entered%20first.%20If%20there%20is%20more%20than%20one%20student%20with%20the%20same%20grade%20(8-10)%20assigned%20to%20them%2C%20order%20those%20particular%20students%20by%20their%20name%20alphabetically.%20Finally%2C%20if%20the%20grade%20is%20lower%20than%208%2C%20use%20%22NULL%22%20as%20their%20name%20and%20list%20them%20by%20their%20grades%20in%20descending%20order.%20If%20there%20is%20more%20than%20one%20student%20with%20the%20same%20grade%20(1-7)%20assigned%20to%20them%2C%20order%20those%20particular%20students%20by%20their%20marks%20in%20ascending%20order.%26lt%3B%5C%2FP%26gt%3B%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3EWrite%20a%20query%20to%20help%20Eve.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EBelow%20is%20my%20%3CSTRONG%3E%3CU%3Esolution%26lt%3B%5C%2FU%26gt%3B%26lt%3B%5C%2FSTRONG%26gt%3B%2C%20but%20I%20am%20aware%20I%20am%20missing%20the%20key%20to%20join%20the%20table%2C%20which%20is%20exactly%20where%20I%20am%20stuck%2C%20as%20I%20can't%20find%20a%20proper%20way%20to%20join.%20Can%20anybody%20help%20to%20look%20at%20it%20and%20give%20a%20hint%3F%26lt%3B%5C%2FP%26gt%3B%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThanks%20guys.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3ESELECT%20Students.Name%2C%20Students.Marks%2C%20CASE%20WHEN%20Marks%20BETWEEN%2090%20AND%20100%20THEN%20Grade%20%3D%2010%20ELSE%20NULL%20END%2C%20CASE%20WHEN%20Marks%20BETWEEN%2080%20AND%2089%20THEN%20Grade%20%3D%209%20ELSE%20NULL%20END%2C%20CASE%20WHEN%20Marks%20BETWEEN%2070%20AND%2079%20THEN%20Grade%20%3D%208%20ELSE%20NULL%20END%20FROM%20Students%20FULL%20JOIN%20Grades%20GROUP%20BY%20Grade%20ORDER%20BY%20Name%20ASC%2C%20Marks%20ASC%3B%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3C%2FP%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3C%2FLINGO-SUB%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3153073%22%20slang%3D%22en-US%22%3ENeed%20a%20solution%20to%20solve%20sql%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3153073%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone%2C%3C%2FP%3E%3CP%3EThis%20is%20Sophie%2C%20I%20am%20new%20to%20this%20community.%3C%2FP%3E%3CP%3ECurrently%20I%20am%20stuck%20on%20an%20sql%20question%2C%20is%20anyone%20able%20to%20help%20on%20it%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CU%3E%3CEM%3EQuestion%3C%2FEM%3E%3C%2FU%3E%3C%2FSTRONG%3E%3A%3C%2FP%3E%3CP%3EYou%20are%20given%20two%20tables%3A%26nbsp%3B%3CEM%3EStudents%3C%2FEM%3E%26nbsp%3Band%20%3CEM%3EGrades%3C%2FEM%3E.%26nbsp%3B%3CEM%3EStudents%3C%2FEM%3E%26nbsp%3Bcontains%20three%20columns%20%3CEM%3EID%3C%2FEM%3E%2C%20%3CEM%3EName%3C%2FEM%3E%20and%20%3CEM%3EMarks%3C%2FEM%3E.%3C%2FP%3E%3CDIV%20class%3D%22%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Students.png%22%20style%3D%22width%3A%20313px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F347244iFC0118D13E7BD0DB%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Students.png%22%20alt%3D%22Students.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EGrades%3C%2FEM%3E%20contains%20the%20following%20data%3A%3C%2FP%3E%3CDIV%20class%3D%22%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Grades.png%22%20style%3D%22width%3A%20317px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F347245i0EBB1D109A687AB5%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Grades.png%22%20alt%3D%22Grades.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EKetty%3C%2FEM%3E%20gives%20%3CEM%3EEve%3C%2FEM%3E%20a%20task%20to%20generate%20a%20report%20containing%20three%20columns%3A%20%3CEM%3EName%3C%2FEM%3E%2C%20%3CEM%3EGrade%3C%2FEM%3E%20and%20%3CEM%3EMark%3C%2FEM%3E.%20%3CEM%3EKetty%3C%2FEM%3E%20doesn't%20want%20the%20NAMES%20of%20those%20students%20who%20received%20a%20grade%20lower%20than%20%3CEM%3E8%3C%2FEM%3E.%20The%20report%20must%20be%20in%20descending%20order%20by%20grade%20--%20i.e.%20higher%20grades%20are%20entered%20first.%20If%20there%20is%20more%20than%20one%20student%20with%20the%20same%20grade%20(8-10)%20assigned%20to%20them%2C%20order%20those%20particular%20students%20by%20their%20name%20alphabetically.%20Finally%2C%20if%20the%20grade%20is%20lower%20than%208%2C%20use%20%22NULL%22%20as%20their%20name%20and%20list%20them%20by%20their%20grades%20in%20descending%20order.%20If%20there%20is%20more%20than%20one%20student%20with%20the%20same%20grade%20(1-7)%20assigned%20to%20them%2C%20order%20those%20particular%20students%20by%20their%20marks%20in%20ascending%20order.%3C%2FP%3E%3CP%3EWrite%20a%20query%20to%20help%20Eve.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20my%20%3CSTRONG%3E%3CU%3Esolution%3C%2FU%3E%3C%2FSTRONG%3E%2C%20but%20I%20am%20aware%20I%20am%20missing%20the%20key%20to%20join%20the%20table%2C%20which%20is%20exactly%20where%20I%20am%20stuck%2C%20as%20I%20can't%20find%20a%20proper%20way%20to%20join.%20Can%20anybody%20help%20to%20look%20at%20it%20and%20give%20a%20hint%3F%3C%2FP%3E%3CP%3EThanks%20guys.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESELECT%20Students.Name%2C%20Students.Marks%2C%20CASE%20WHEN%20Marks%20BETWEEN%2090%20AND%20100%20THEN%20Grade%20%3D%2010%20ELSE%20NULL%20END%2C%20CASE%20WHEN%20Marks%20BETWEEN%2080%20AND%2089%20THEN%20Grade%20%3D%209%20ELSE%20NULL%20END%2C%20CASE%20WHEN%20Marks%20BETWEEN%2070%20AND%2079%20THEN%20Grade%20%3D%208%20ELSE%20NULL%20END%20FROM%20Students%20FULL%20JOIN%20Grades%20GROUP%20BY%20Grade%20ORDER%20BY%20Name%20ASC%2C%20Marks%20ASC%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

Hello everyone,

This is Sophie, I am new to this community.

Currently I am stuck on an sql question, is anyone able to help on it?

 

Question:

You are given two tables: Students and GradesStudents contains three columns ID, Name and Marks.

Students.png

 

Grades contains the following data:

Grades.png

 

Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8. The report must be in descending order by grade -- i.e. higher grades are entered first. If there is more than one student with the same grade (8-10) assigned to them, order those particular students by their name alphabetically. Finally, if the grade is lower than 8, use "NULL" as their name and list them by their grades in descending order. If there is more than one student with the same grade (1-7) assigned to them, order those particular students by their marks in ascending order.

Write a query to help Eve.

 

Below is my solution, but I am aware I am missing the key to join the table, which is exactly where I am stuck, as I can't find a proper way to join. Can anybody help to look at it and give a hint?

Thanks guys.

 

SELECT Students.Name, Students.Marks, CASE WHEN Marks BETWEEN 90 AND 100 THEN Grade = 10 ELSE NULL END, CASE WHEN Marks BETWEEN 80 AND 89 THEN Grade = 9 ELSE NULL END, CASE WHEN Marks BETWEEN 70 AND 79 THEN Grade = 8 ELSE NULL END FROM Students FULL JOIN Grades GROUP BY Grade ORDER BY Name ASC, Marks ASC;

0 Replies