I have two tables, Courses and Faculties.
instructor_id in Courses references faculty_id in Faculties.
I'm trying to write a query which lists all the instructors who teach more than one course. As I'm new to SQL in general I'm utterly stumped as to how to go about doing this. There are rows in the Courses table with the same value for instructor_id. Thus far I've already joined the tables like this:
SELECT "Courses".description, "Faculties".name FROM "Courses" INNER JOIN
"Faculties" ON "Courses".instructor = "Faculties".faculty_id;
But I don't know how to filter out the rows which which duplicate values under Instructor column (in other words filter the classes with the same instructor).
PostgreSQL SELECT statement syntax If you specify a list of columns, you need to place a comma ( , ) between two columns to separate them. If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names.
The SQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It does not remove duplicate rows between the various SELECT statements (all rows are returned). Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.
The GROUP BY clause is a SQL command that is used to group rows that have the same values. The GROUP BY clause is used in the SELECT statement.
This is an aggregation query. If you just want the instructor id, then you can use:
select instructor_id
from courses
group by instructor_id
having count(*) > 1;
To get additional information, join in the other table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With