0

I have a question about joining two tables with no relationship.

Students Table

enter image description here

Grades Table

enter image description here

I want to give Grade to each students, but I don't know how to join properly.

Is any good approach to join two table?

Thanks

jtoyhh
  • 55
  • 5
  • 1
    A JOIN is an excellent approach. – jarlh Apr 12 '22 at 11:25
  • 1
    What have you tried so far? Why didn't it work? – Martin Apr 12 '22 at 11:25
  • `with no relationship` then you probably want to `UNION`ise the two tables. – Martin Apr 12 '22 at 11:26
  • Does this answer your question? [SQL join two tables without keys/relations](https://stackoverflow.com/questions/1797785/sql-join-two-tables-without-keys-relations) – Martin Apr 12 '22 at 11:27
  • Actually there is a relationship between marks and grade..so a join would be appropriate. – P.Salmon Apr 12 '22 at 11:28
  • 1
    Show us sample data for both tables, and specify the expected result as well. [mcve] – jarlh Apr 12 '22 at 12:01
  • @jarlh Oh Sorry, Here's the problem : [link] (http://hackerrank.com/challenges/the-report/problem?isFullScreen=true) – jtoyhh Apr 12 '22 at 12:11
  • @YongHunJeong if the question is resolved please can you either delete the question or post an answer to highlight how this was fixed. This will help others in future searching for the same thing. Thank you `:-)` – Martin Apr 12 '22 at 12:28

3 Answers3

3

maybe use a query like below

select s.*,g.grade from students s 
left join grades g on s.marks between min_mark and max_mark
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
2

Actually there is a relationship between marks and grade..so a join would be appropriate

Select id,name,marks,grade
    from student
    join grades on marks between min_mark and max_mark
P.Salmon
  • 17,104
  • 2
  • 12
  • 19
  • 2
    Awkward, because that's *assumed* when the OP has stated there's no relationship. We shall see, if they update their Q.... – Martin Apr 12 '22 at 11:32
  • @Martin I bet OP thinks because there is no FK then there is no relationship...but you are right a clarification would be useful. – P.Salmon Apr 12 '22 at 11:34
  • Here's the problem: https://www.hackerrank.com/challenges/the-report/problem?isFullScreen=true. – lemon Apr 12 '22 at 11:40
0

There is a relationship, but it is not equals.
You could also use BETWEEN min_mark AND max_mark but you will need to check that your version of MySQL supports it.

SELECT
  s.ID,
  s.Name,
  s.Marks,
  g.grade
FROM
  Students s,
  Marks s
WHERE
  s.Marks >= g.Min_Mark
AND
  s.Marks <= g.max_Mark;
  • 1
    Tip of today: Switch to modern, explicit `JOIN` syntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed. – jarlh Apr 12 '22 at 11:59