DISTINCTROW

DISTINCTROW is an SQL clause that is used to ensure that all the rows returned from a query are unique. It takes into account all the columns in the result set to eliminate duplicate rows. This clause is particularly useful in database scenarios where you need to streamline your dataset by removing redundancy.

Example

SELECT DISTINCTROW City
FROM Customers;

Output

| City |
|------------|
| Berlin |
| London |
| Madrid |
| Paris |

Explanation

The above SQL statement is used to select distinct (different) cities from the “Customers” table. The DISTINCTROW keyword ensures that each row is unique for the column specified.

Example

SELECT DISTINCTROW Orders.OrderID, Orders.CustomerID
FROM Orders

Output

OrderID CustomerID
--------- ----------
1 ACME Corp
2 Globex Inc
3 Soylent Co
4 Acme Corp

Explanation

The DISTINCTROW keyword can be used in a SQL SELECT statement to omit duplicate records from the results. In this example, the SELECT statement returns a list of unique order IDs and associated customer IDs from the Orders table. Records where both the OrderID and CustomerID are identical to another record are excluded from these results.

Example

SELECT DISTINCTROW Student_ID, Name
FROM Students

Output

Student_ID Name
------------- ------------
1 John
2 Joseph
3 Martha
4 Susan
5 Ronald

Explanation

The DISTINCTROW keyword in the SQL command is used to fetch distinct or unique records. In the example provided, DISTINCTROW is used to fetch unique Student_ID and Name from the table named Students. As a result, any duplicate rows with the same combination of Student_ID and Name are removed from the returned result set.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.