Skip to content

Tag: postgresql

Postgresql constraint violated at restore

I have a problem with a constraint that is being violated by some rows when restoring my DB, but has never been violated at usage. I have 3 tables : some_table here is another table, which I think is not relevant for this problem. Now what I want is to have a unicity constraint between as.id and bs.some_id th…

Syntax error while using WHERE clause in PostgreSQL

I’m trying to run some queries related to the University database in PostgreSQL. To find students advised by instructors from different departments, I used- However im getting the following error: ERROR: syntax error at or near “where” LINE 3: student join instructor where student.dept_name …

Count number of students per one role SQL

I have a table students with student id, course id and role number looking like this (ordered by student_id): How to count number of students for each role? If student is enrolled in multiple courses with the same role number, then count this as one. The expected output for above table would be similar to thi…

Set minutes of timestamp to a specific value

What is the easiest way to set the exact minutes value of a timestamp? This only adds minutes instead of setting the exact value: Answer Use date_trunc() before you add 2 minutes: Or, to retain seconds and sub-seconds: Demo: Should be substantially faster than manipulating the text representation and casting …

Find students who take most courses SQL

Assume there’s a table students containing student id, semester id and number of courses taken in each semester: How can I find students in each semester who took the largest number of courses? Expected output would be: I thought of using OVER PARTITION BY, but I’m not sure how to use it correctly…