Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

SQL Order by Clause

SQL Order by:

The SQL ORDER BY clause is used to sort the result set in ascending or descending order, based on one or more columns.

For some RDBMS database default order is ascending, to sort the records in a descending order, you can use the DESC keyword.

Syntax:

SELECT column_1, column_2..column_n
FROM table_name ORDER BY some_column ASC|DESC, column_name ASC|DESC;


Example:

On one column:

Ascending:

SELECT * FROM Categories ORDER BY CategoryName
FROM Categories ORDER BY CategoryName

Descending:
SELECT *
FROM Categories ORDER
BY CategoryName DESC

On two columns:

Ascending:
SELECT * FROM Categories ORDER BY CategoryName,[Description]
Descending:
SELECT * FROM Categories ORDER BY CategoryName,[Description] DESC

SQL Where

SQL where clause used to specify condition and select statement extract only those records that fulfil a specified condition.

Syntax:

SELECT column_1,column_2..column n
FROM table_name
WHERE some_column operator value;


Example:

SELECT DISTINCT CategoryName,[Description] FROM Categories WHERE CategoryName='books
CategoryName,[Description] FROM Categories WHERE CategoryName='books

Result:



SQL SELECT DISTINCT statement

The SQL SELECT DISTINCT statement is used to remove duplicates from the result set of a SELECT statement and return only distinct values.

Syntax:

SELECT DISTINCT column 1,column 2…..column n
FROM table_name;



Example:

SELECT DISTINCT CategoryName,[Description] FROM Categories

Result:


Example with all columns:

SELECT DISTINCT * FROM Categories 

In this example distinct keyword compares all column values if found different all columns values then only return distinct values. 

Result:

above original diagram with duplicate values.