Explain the SQL queries for the following two questions: (1) Retrieve distinct student_name, class, and section; (2) Find student names that appear more than once in the table.
💡 Model Answer
Q1 uses the DISTINCT keyword to eliminate duplicate rows across all selected columns: SELECT DISTINCT student_name, class, section FROM students; This scans the table once and removes exact duplicates, producing a set of unique combinations. Q2 identifies duplicate student names by grouping and counting: SELECT student_name, COUNT() AS count FROM students GROUP BY student_name HAVING COUNT() > 1; The GROUP BY aggregates rows by name, COUNT(*) tallies occurrences, and HAVING filters to those with more than one record. Both queries are O(n) in time, with the GROUP BY query also requiring a hash or sort operation to aggregate, which is typically O(n log n) in worst‑case implementations. These patterns are standard in relational databases for deduplication and duplicate detection.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500