Which of the following keyword is used to eliminate duplicate records in SQL?
DISTINCT
In SQL databases, you often encounter tables with multiple rows. Sometimes, you might want to retrieve only the unique rows, meaning you want to eliminate duplicate records from the result set of a query. Duplicate records occur when two or more rows have identical values across all selected columns.
SQL provides specific keywords to handle this requirement. The goal is to filter the query output so that each distinct combination of values in the selected columns appears only once.
Let's look at the options provided and determine which SQL keyword is used to eliminate duplicate records:
Standard SQL uses the DISTINCT keyword to achieve this. When you include DISTINCT in your SELECT statement, it tells the database engine to return only unique rows in the result set, effectively eliminating duplicate records.
Let's consider a simple example. Suppose you have a table called Customers with columns CustomerID and City, and you want to list all the unique cities where your customers are located.
Original Data (Customers Table):
| CustomerID | City |
|---|---|
| 101 | New York |
| 102 | Los Angeles |
| 103 | New York |
| 104 | Chicago |
| 105 | Los Angeles |
If you select the City column without using DISTINCT:
SELECT City FROM Customers;
Result:
| City |
|---|
| New York |
| Los Angeles |
| New York |
| Chicago |
| Los Angeles |
This result includes duplicate cities ('New York' appears twice, 'Los Angeles' appears twice).
Now, if you use the DISTINCT keyword:
SELECT DISTINCT City FROM Customers;
Result:
| City |
|---|
| New York |
| Los Angeles |
| Chicago |
This result set only contains the unique cities, successfully eliminating duplicate records.
The other options listed (ELIMINATE, DUPLICATE, NODUPLICATE) are not standard SQL keywords used for eliminating duplicate records in this manner.
Therefore, the correct keyword used to eliminate duplicate records in SQL is DISTINCT.
| Concept | SQL Keyword | Purpose | Example Use |
|---|---|---|---|
| Eliminating Duplicate Records | DISTINCT |
To retrieve only unique combinations of values in the selected columns from a query result set. | SELECT DISTINCT column1, column2 FROM table_name; |
| Selecting All Records (including duplicates) | ALL (default behavior, often omitted) |
To retrieve all rows from the table, including duplicate records. | SELECT ALL column1 FROM table_name; (Same as SELECT column1 FROM table_name;) |
While DISTINCT is the primary way to remove duplicate rows in a SELECT statement's result set, there are other concepts related to handling duplicates in SQL:
GROUP BY and HAVING clauses to count occurrences.
PRIMARY KEY (which enforces uniqueness and non-nullability for one or more columns) or a UNIQUE constraint (which enforces uniqueness, allowing NULLs unless also specified as NOT NULL).
UNION vs. UNION ALL: The UNION operator combines the result sets of two or more SELECT statements and automatically removes duplicate rows. The UNION ALL operator combines result sets but includes all rows, including duplicates. This is different from SELECT DISTINCT, which works within a single SELECT statement.
Understanding how to handle and eliminate duplicate records is a fundamental skill in SQL querying and database management.
In SQL, _______ is an Aggregate function.
Match the following -
| List I | List II | ||
| (a) | DDL | (i) | LOCK TABLE |
| (b) | DML | (ii) | COMMIT |
| (c) | TCL | (iii) | Natural Difference |
| (d) | Binary operation | (iv) | REVOKE |
In SQL, which of the following command is used to modify a column in a table?
_______ SQL command changes one or more fields in a record.
Given two relations R 1(A, B) and R 2(C, D), the result of following query
Select distinct A, B
from R 1, R 2
is guaranteed to be same as R 1 provided one of the following condition is satisfied.