Hi
In a table i want to check if Student Id exists for more than 1 record.
Thanks
Hi
In a table i want to check if Student Id exists for more than 1 record.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 17, 2023, 4:25 AM EDT
Hello Ramco,
You can use having caluse with count() > 1 ;
SELECT Id, COUNT(*) FROM YourTableName GROUP BY Id HAVING COUNT(*) > 1Amit MohantyRank #112 · 20K reputationPosted Apr 17, 2023, 2:37 AM EDT
You can use a SQL query with the
GROUP BYclause and theHAVINGclause to check if a Student ID exists for more than one record in a table. Here's an example:SELECT StudentID, COUNT(*) as NumRecords FROM YourTable GROUP BY StudentID HAVING COUNT(*) > 1;Vishal JoshiRank #377 · 6.7K reputationPosted Apr 17, 2023, 3:15 AM EDT
Hello
You can simply get a count of records that exists for the same id like the below query and in code, you can check if the count is greater than 1.
SELECT COUNT(1) as TotalCount FROM YourTable Where StudentID = YourSearchIdServer side you can check like below code
if (TotalCound > 1) { // Code } else { // existing behaviour }Thanks
Vishal
Jay Krishna ReddyRank #152 · 15.2K reputationPosted Apr 17, 2023, 3:51 AM EDT
SELECT StudentId, COUNT(*) as Count FROM Students GROUP BY StudentId HAVING COUNT(*) > 1This query will group the records in the "Students" table by the "StudentId" column, count the number of records for each group, and then return only the groups where the count is greater than 1.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 18, 2023, 3:16 AM EDT
You can use aggregation pipelines to check if a student ID exists for more than one record. Depending on the specific NoSQL database you're using, the syntax and exact approach may vary.
db.students.aggregate([ { $group: { _id: "$student_id", count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } } ])This pipeline groups documents by the "student_id" field, calculates a count for each group, and filters out groups with a count of 1. The resulting output will include the IDs of any students that have more than one record.