Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

using distinct and orderby in the select statment

Former Member
0 Kudos

Hi All,

Can anyone tell me what is the purpose of using distinct and orderby in the select statment.

5 REPLIES 5

Former Member
0 Kudos

Hi,

DISTINCT as name suggest to select only distinct records.

ORDER BY to sort the data.

Regards,

Atish

Former Member
0 Kudos

HI,

select distinct will avoid the repetitive records being selected.

The addition ORDER BY sorts a multiline resulting set by the content of the specified column. The order of the rows in the result set refers to all columns that are not listed after ORDER BY, is undefined, and can be different in repeated executions of the same SELECT statement.

rgds,

bharat.

Former Member
0 Kudos

HI,

distinct will avoid the duplicate records being selected.

The addition ORDER BY sorts the selected records in the field specified by you after the ORDER BY.

thanks & regards

Kishore Kumar Maram

Former Member
0 Kudos

Hi,

Distinct is used when , for example you have a table with multiple entries corresponding to one particular field,suppose external id..

so distinct used in a select query vil show the external id only once n not mulptiple times.

While order by is used in a query if we want the result of a query to ordered by a particular field or column..

The selection will show the results which is ordered by that field.

Thanks.

Revert back if any further queries

Reward points if useful

Former Member
0 Kudos

Hi,

Using the distinct function with more than one column yields some substantial results. SQL will return the rows with distinct or unique combinations of those columns. Assume this same employee table has another column including the salary of each employee. With the use of the distinct function we can pull a list of unique job titles - salaries.

SQL Code:

SELECT DISTINCT job_titles, salary FROM employees;

SQL has returned all the rows with unique combinations of job titles and salaries. Any duplicate combinations of job titles and salaries will be ignored. For example if we had two CEOs in the table making the same salary each year, only one row would be returned but if we had two CEOs with different salaries, both rows would be returned.

Order by

The SQL ORDER BY clause comes in handy when you want to sort your SQL result sets by some column(s). For example if you want to select all the persons from the already familiar Customers table and order the result by date of birth, you will use the following statement:

SELECT * FROM Customers

ORDER BY DOB

As you can see the rows are sorted in ascending order by the DOB column, but what if you want to sort them in descending order? To do that you will have to add the DESC SQL keyword after your SQL ORDER BY clause:

SELECT * FROM Customers

ORDER BY DOB DESC

If you don't specify how to order your rows, alphabetically or reverse, than the result set is ordered alphabetically, hence the following to SQL expressions produce the same result:

SELECT * FROM Customers

ORDER BY DOB

SELECT * FROM Customers

ORDER BY DOB ASC

You can sort your result set by more than one column by specifying those columns in the SQL ORDER BY list. The following SQL expression will order by DOB and LastName:

SELECT * FROM Customers

ORDER BY DOB, LastName

if its useful reward points