Can you write a SQL query or a Python script to find all duplicate rows in a tickets table?
💡 Model Answer
SQL: To find duplicate tickets based on a key column (e.g., ticket_id), use a GROUP BY with HAVING COUNT(*) > 1:
SELECT ticket_id, COUNT(*) AS dup_count
FROM tickets
GROUP BY ticket_id
HAVING COUNT(*) > 1;
If you want the full duplicate rows, join back to the original table:
SELECT t.*
FROM tickets t
JOIN (
SELECT ticket_id
FROM tickets
GROUP BY ticket_id
HAVING COUNT(*) > 1
) dup ON t.ticket_id = dup.ticket_id;
Python (pandas):
import pandas as pd
df = pd.read_sql('SELECT * FROM tickets', con)
# Find duplicates on ticket_id
dup_rows = df[df.duplicated(subset=['ticket_id'], keep=False)]
print(dup_rows)
Both approaches run in O(n) time, where n is the number of rows. The SQL version relies on the database's grouping engine, while the pandas version loads data into memory and uses vectorized operations. Choose the tool that best fits your environment and data size.
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