Search code examples
sqlaliases

Using Aliases in SQL


I use the following code, but I am trying to understand Aliases, I get an error with this code "SQL command not properly ended", line 2

SELECT student.lastname, student.firstname
FROM student AS Student_Name
INNER JOIN memberof ON (Student_Name.SID = memberof.studentid)
INNER JOIN studentgroup ON (memberof.groupid = studentgroup.gid)
GROUP BY Student_Name.lastname , Student_Name.firstname 
HAVING COUNT(memberof.groupid) >= 2

Solution

  • I'm thinking that you are using Oracle which apparently does not support the as keyword for table aliases.

    SELECT s.lastname, 
           s.firstname
    FROM student s
    INNER JOIN memberof 
        ON s.SID = memberof.studentid
    INNER JOIN studentgroup 
        ON memberof.groupid = studentgroup.gid
    GROUP BY s.lastname, 
             s.firstname 
    HAVING COUNT(*) >= 2;