I am new to PostgreSQL. Trying to understand the reason for erroneous results and would like to learn how to rectify it.
I have a students table. When I execute the query
SELECT stud_id, batch_type, subject, jdate ,subj_id, sdtime, subj_hours
FROM studs
ORDER BY jdate DESC, subject DESC, stud_id ASC
LIMIT 15 OFFSET 0
I get the following correct result as expected:
Stud_id bpchar | Batch_typeCharacter varying | SubjectCharacter varying | Jdatedate | Subj_idinteger | SdtimeTimestamp without time zone | Subj_hoursinteger |
---|---|---|---|---|---|---|
3591f1gff960h09l | Residential | Software Systems Development 7 | null | 35 | 2024-03-20 12:42:55.040189 | 10 |
983e292ce1xy69c0 | Residential | Software Systems Development 5 | null | 35 | 2024-03-20 12:42:55.040189 | 14 |
7k992ad576842507 | Residential | Software Systems Development 4 | null | 35 | 2024-03-20 12:42:55.040189 | 12 |
b9ahibji8jgaeth2 | Residential | Software Systems Development 3 | null | 35 | 2024-03-20 12:42:55.040189 | 10 |
5758uh95376eb0b4 | Residential | Software Systems Development 2 | 2024-03-20 | 35 | 2024-03-20 12:42:55.040189 | 12 |
6ch9b2f7894740er | Residential | Software Systems Development | 2024-03-20 | 35 | 2024-03-20 12:42:55.040189 | 14 |
a877450586v3869h | Residential | Software Systems Development | 2024-03-20 | 35 | 2024-03-20 12:42:55.040189 | 14 |
78635a8s1586d95c | Residential | Software Systems Development | 2024-03-15 | 35 | 2024-03-20 12:42:55.040189 | 14 |
b58c7fbf57328468 | Residential | Software Systems Development | 2024-03-15 | 35 | 2024-03-20 12:42:55.040189 | 16 |
acb8g94351e84226 | Residential | Software Systems Development | 2024-02-25 | 35 | 2024-03-20 12:42:55.040189 | 15 |
fvhud7653fd85m1s | Residential | Software Systems Development | 2024-02-20 | 35 | 2024-03-20 12:42:55.040189 | 16 |
b7hf962ffb6he96j | Residential | Design and Technology | 2024-02-19 | 35 | 2024-03-20 12:42:55.040189 | 14 |
394ij84275c6e97d | Residential | Software Systems Development | 2024-01-15 | 35 | 2024-03-20 12:42:55.040189 | 16 |
When I try filter the results by adding WHERE
condition in the query as below:
SELECT stud_id, batch_type, subject, jdate ,subj_id, sdtime, subj_hours form studs
WHERE subject < ’ Software Systems Development 5’
AND stud_id > ‘983e292ce1xy69c0’
ORDER BY join_date DESC, subject DESC, stud_id ASC
LIMIT 15 OFFSET 0
I get erroneous result as follows:
Stud_idbpchar | Batch_typeCharacter varying | SubjectCharacter varying | Jdatedate | Subj_idinteger | SdtimeTimestamp without time zone | Subj_hoursinteger |
---|---|---|---|---|---|---|
b9ahibji8jgaeth2 | Residential | Software Systems Development 3 | null | 35 | 2024-03-20 12:42:55.040189 | 10 |
a877450586v3869h | Residential | Software Systems Development | 2024-03-20 | 35 | 2024-03-20 12:42:55.040189 | 14 |
b58c7fbf57328468 | Residential | Software Systems Development | 2024-03-15 | 35 | 2024-03-20 12:42:55.040189 | 16 |
acb8g94351e84226 | Residential | Software Systems Development | 2024-02-25 | 35 | 2024-03-20 12:42:55.040189 | 15 |
fvhud7653fd85m1s | Residential | Software Systems Development | 2024-02-20 | 35 | 2024-03-20 12:42:55.040189 | 16 |
b7hf962ffb6he96j | Residential | Design and Technology | 2024-02-19 | 35 | 2024-03-20 12:42:55.040189 | 14 |
but I was expecting the following result:
Stud_idbpchar | Batch_typeCharacter varying | SubjectCharacter varying | Jdatedate | Subj_idinteger | SdtimeTimestamp without time zone | Subj_hoursinteger |
---|---|---|---|---|---|---|
7k992ad576842507 | Residential | Software Systems Development 4 | null | 35 | 2024-03-20 12:42:55.040189 | 12 |
b9ahibji8jgaeth2 | Residential | Software Systems Development 3 | null | 35 | 2024-03-20 12:42:55.040189 | 10 |
5758uh95376eb0b4 | Residential | Software Systems Development 2 | 2024-03-20 | 35 | 2024-03-20 12:42:55.040189 | 12 |
6ch9b2f7894740er | Residential | Software Systems Development | 2024-03-20 | 35 | 2024-03-20 12:42:55.040189 | 14 |
a877450586v3869h | Residential | Software Systems Development | 2024-03-20 | 35 | 2024-03-20 12:42:55.040189 | 14 |
78635a8s1586d95c | Residential | Software Systems Development | 2024-03-15 | 35 | 2024-03-20 12:42:55.040189 | 14 |
b58c7fbf57328468 | Residential | Software Systems Development | 2024-03-15 | 35 | 2024-03-20 12:42:55.040189 | 16 |
acb8g94351e84226 | Residential | Software Systems Development | 2024-02-25 | 35 | 2024-03-20 12:42:55.040189 | 15 |
fvhud7653fd85m1s | Residential | Software Systems Development | 2024-02-20 | 35 | 2024-03-20 12:42:55.040189 | 16 |
b7hf962ffb6he96j | Residential | Design and Technology | 2024-02-19 | 35 | 2024-03-20 12:42:55.040189 | 14 |
394ij84275c6e97d | Residential | Software Systems Development | 2024-01-15 | 35 | 2024-03-20 12:42:55.040189 | 16 |
Some rows are missed in the result. Can someone help me to understand the reason and how to fix it?
qquest is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1