reset password
Author Message
se1k1h1mawar1
Posts: 121
Posted 20:34 Oct 28, 2015 |

Could somebody please suggest to me why this (please see below) is the output I get from the shown query?
I know I should know much better, but my brain is just refusing to think in a SQL-friendly way, and I am not seeing things in the reasonable way anymore.

What I was trying to get as an output was what classes this student (student id = 300) has taken, and which grade s/he got in them...  As you can see on the table at the very bottom, student whose id is 300 has only taken one class.

Even a short one sentence suggestion would be greatly appreciated.
Thank you!

 

 select c.title, e.student_id, g.letter, s.id as "section id"  from enrol
lment e, courses c, sections s, grades g where e.section_id=e.section_id and s.c
ourse_id=c.id and e.grade_id=g.id and e.student_id=300;


   title    | student_id | letter | section id
------------+------------+--------+------------
 Databases  |        300 | A      |         23
 Databases  |        300 | A      |         13
 Databases  |        300 | A      |         12
 Compilers  |        300 | A      |         24
 Compilers  |        300 | A      |         14
 Calculus 1 |        300 | A      |         43
 Calculus 1 |        300 | A      |         33
 Calculus 1 |        300 | A      |         32
 Acting     |        300 | A      |         54
 Acting     |        300 | A      |         52
 Elocution  |        300 | A      |         53
 Elocution  |        300 | A      |         51
 Calculus 2 |        300 | A      |        500
 Topology   |        300 | A      |         34
(14 rows)
 

--- ----

cs422=> select * from enrollment;
  id  | student_id | section_id | grade_id
------+------------+------------+----------
   14 |          1 |         12 |        8
   15 |          1 |         13 |        3
   16 |          1 |         14 |        5
   17 |          1 |         32 |        1
   18 |          1 |         34 |        2
   19 |          1 |         53 |       13
   24 |          3 |         12 |        2
   25 |          3 |         14 |        5
   26 |          3 |         32 |        1
   27 |          3 |         34 |        2
   28 |          3 |         54 |        7
   34 |          2 |         43 |        3
   44 |          4 |         33 |        4
   54 |          4 |         53 |        1
   64 |          6 |         53 |        1
  100 |        100 |         34 |
 1000 |        100 |         32 |        8
 3000 |        300 |         32 |        1
 3001 |        300 |        500 |
(19 rows)

Last edited by se1k1h1mawar1 at 20:35 Oct 28, 2015.
meishu
Posts: 6
Posted 20:55 Oct 28, 2015 |

where e.section_id=e.section_id

I dunno if it'll fix it but you probably wanted e.section_id = s.id

cysun
Posts: 2935
Posted 20:58 Oct 28, 2015 |

You missed a join condition: the "e.section_id = e.section_id" should be "e.section_id = s.id". These types of errors are probably more obvious with the inner join syntax.

se1k1h1mawar1
Posts: 121
Posted 21:00 Oct 28, 2015 |
meishu wrote:

where e.section_id=e.section_id

I dunno if it'll fix it but you probably wanted e.section_id = s.id

I see! Thank you very much!

cysun
Posts: 2935
Posted 21:00 Oct 28, 2015 |
meishu wrote:

where e.section_id=e.section_id

I dunno if it'll fix it but you probably wanted e.section_id = s.id

Ha, looks like I'm too late.

se1k1h1mawar1
Posts: 121
Posted 21:02 Oct 28, 2015 |
cysun wrote:

These types of errors are probably more obvious with the inner join syntax.

Thank you for your suggestion. It must be a good idea to get in a habit of using inner join syntax, at least I have enough experience with SQL!