What is the main purpose of finding duplicates in a database?
To identify records that appear more than once based on certain columns, which helps in cleaning data and ensuring accuracy.
Click to reveal answer
beginner
Which SQL clause is commonly used to group rows when searching for duplicates?
The GROUP BY clause groups rows that have the same values in specified columns, allowing aggregate functions like COUNT() to find duplicates.
Click to reveal answer
beginner
Write a simple SQL query to find duplicate email addresses in a users table.
SELECT email, COUNT(*) AS count FROM users GROUP BY email HAVING COUNT(*) > 1;
Click to reveal answer
intermediate
Why is using HAVING COUNT(*) > 1 important in finding duplicates?
Because HAVING filters groups after aggregation, it selects only those groups where the count of records is more than one, indicating duplicates.
Click to reveal answer
intermediate
How can indexing help in finding duplicates efficiently?
Indexing the columns used to find duplicates speeds up grouping and counting by allowing the database to quickly locate matching values.
Click to reveal answer
Which SQL clause helps group rows to find duplicates?
AWHERE
BORDER BY
CGROUP BY
DJOIN
✗ Incorrect
GROUP BY groups rows with the same values, which is essential for counting duplicates.
What does HAVING COUNT(*) > 1 do in a duplicate search query?
AFilters groups with more than one record
BOrders the results by count
CLimits the output to one record
DJoins tables
✗ Incorrect
HAVING filters groups after aggregation, selecting only those with more than one record.
Which function counts the number of rows in each group?
ASUM()
BMAX()
CAVG()
DCOUNT()
✗ Incorrect
COUNT() returns the number of rows in each group.
Why is indexing useful when finding duplicates?
AIt speeds up grouping and searching
BIt deletes duplicates automatically
CIt slows down queries
DIt changes data types
✗ Incorrect
Indexing speeds up data retrieval, making duplicate searches faster.
Which SQL keyword is used to filter rows before grouping?
AHAVING
BWHERE
CGROUP BY
DSELECT
✗ Incorrect
WHERE filters rows before grouping; HAVING filters groups after aggregation.
Explain how to write a SQL query to find duplicate records based on one or more columns.
Think about grouping and counting rows to spot duplicates.
You got /3 concepts.
Describe why indexing columns can improve the performance of duplicate detection queries.
Consider how databases find data quickly.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using GROUP BY with HAVING COUNT(*) > 1 in SQL?
easy
A. To sort the table alphabetically
B. To find duplicate values in a column
C. To delete rows from a table
D. To create a new table
Solution
Step 1: Understand GROUP BY usage
GROUP BY groups rows that have the same value in specified columns.
Step 2: Use HAVING COUNT(*) > 1 to filter groups
HAVING COUNT(*) > 1 keeps only groups with more than one row, meaning duplicates.
Final Answer:
To find duplicate values in a column -> Option B
Quick Check:
GROUP BY + HAVING COUNT(*) > 1 = Find duplicates [OK]
Hint: Use GROUP BY with HAVING COUNT(*) > 1 to spot duplicates fast [OK]
Common Mistakes:
Confusing HAVING with WHERE clause
Using COUNT(column) instead of COUNT(*)
Not grouping by the correct column
2. Which of the following SQL queries correctly finds duplicate email values in a table named users?
easy
A. SELECT email FROM users GROUP BY email HAVING COUNT(*) > 1;
B. SELECT email FROM users WHERE COUNT(email) > 1;
C. SELECT email FROM users GROUP BY email WHERE COUNT(*) > 1;
D. SELECT email FROM users HAVING COUNT(email) > 1;
Solution
Step 1: Check correct syntax for grouping and filtering
GROUP BY groups rows; HAVING filters groups. WHERE cannot filter aggregates.
Step 2: Identify correct query
SELECT email FROM users GROUP BY email HAVING COUNT(*) > 1; uses GROUP BY email and HAVING COUNT(*) > 1 correctly.
Final Answer:
SELECT email FROM users GROUP BY email HAVING COUNT(*) > 1; -> Option A
Quick Check:
GROUP BY + HAVING filters duplicates correctly [OK]
Hint: Use HAVING, not WHERE, to filter grouped results [OK]
Common Mistakes:
Using WHERE with aggregate functions
Placing WHERE after GROUP BY
Using COUNT(column) incorrectly in WHERE
3. Given the table orders with column customer_id, what will this query return?
SELECT customer_id FROM orders GROUP BY customer_id HAVING COUNT(*) > 2;
medium
A. All customer IDs with exactly 2 orders
B. All customer IDs regardless of order count
C. All customer IDs with less than 2 orders
D. All customer IDs with more than 2 orders
Solution
Step 1: Understand HAVING COUNT(*) > 2 condition
This filters groups where the count of rows is greater than 2.
Step 2: Apply to customer_id groups
Only customer IDs with more than 2 orders appear in the result.
Final Answer:
All customer IDs with more than 2 orders -> Option D
Quick Check:
HAVING COUNT(*) > 2 means count > 2 [OK]
Hint: HAVING COUNT(*) > N finds groups with more than N rows [OK]
Common Mistakes:
Thinking it finds exactly 2 instead of more than 2
Confusing WHERE and HAVING clauses
Ignoring GROUP BY effect
4. Identify the error in this query to find duplicate username values:
SELECT username FROM users WHERE COUNT(username) > 1 GROUP BY username;
medium
A. Missing ORDER BY clause
B. GROUP BY should come before WHERE
C. COUNT() cannot be used in WHERE clause
D. COUNT() should be COUNT(DISTINCT username)
Solution
Step 1: Check WHERE clause usage
WHERE cannot filter on aggregate functions like COUNT().
Step 2: Correct clause to use HAVING
HAVING is used to filter groups after GROUP BY with aggregates.
Final Answer:
COUNT() cannot be used in WHERE clause -> Option C
Quick Check:
Aggregates need HAVING, not WHERE [OK]
Hint: Use HAVING, not WHERE, to filter aggregates [OK]
Common Mistakes:
Using WHERE with aggregate functions
Placing GROUP BY after WHERE
Confusing COUNT() usage
5. You want to find duplicate product_code values in a large inventory table but also want to see how many times each duplicate appears. Which query is the most efficient and correct?
hard
A. SELECT product_code, COUNT(*) AS count FROM inventory GROUP BY product_code HAVING count > 1;
B. SELECT product_code, COUNT(*) AS count FROM inventory WHERE COUNT(*) > 1 GROUP BY product_code;
C. SELECT product_code FROM inventory GROUP BY product_code HAVING COUNT(*) > 1 ORDER BY product_code;
D. SELECT product_code, COUNT(product_code) FROM inventory GROUP BY product_code WHERE COUNT(product_code) > 1;
Solution
Step 1: Check correct use of GROUP BY and HAVING
SELECT product_code, COUNT(*) AS count FROM inventory GROUP BY product_code HAVING count > 1; groups by product_code and filters groups with count > 1 using HAVING, which is correct.
Step 2: Verify alias usage and efficiency
Using COUNT(*) AS count allows filtering with HAVING count > 1 efficiently and shows counts.
Final Answer:
SELECT product_code, COUNT(*) AS count FROM inventory GROUP BY product_code HAVING count > 1; -> Option A
Quick Check:
GROUP BY + HAVING with alias count shows duplicates and counts [OK]
Hint: Use HAVING with COUNT(*) alias to filter and show duplicates count [OK]
Common Mistakes:
Using WHERE with COUNT()
Placing WHERE after GROUP BY
Not selecting count to see duplicate frequency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.