ORDER BY and DISTINCT tutorial in php

 SELECT with ORDER BY Syntax :

SELECT column1, column2, column3... FROM table_name ORDER  BY column1, column2, column3...ASC | DESC;


If we want to arrange all records names in Ascending Order from table name "personal" then we can use ORDER BY as shown below sql query :

SELECT * FROM personal ORDER BY name;


If we want to arrange all records names in Descending Order from table name "personal" then we can use ORDER BY as shown below sql query :

SELECT * FROM personal ORDER BY name DESC;


If we want to select records in Descending Order and city must be "Agra" (with condition) then we can use ORDER BY as shown below sql query :

SELECT * FROM personal WHERE city = "Agra" ORDER BY name DESC;

For arrange Ascending order to above records, then we can use below query :

SELECT * FROM personal WHERE city = "Agra" ORDER  BY name;


If we want to arrange records in Ascending Order depending upon Age from table name "personal" using ORDER BY then we can use below sql query :

SELECT * FROM personal ORDER BY age;


If we want to arrange records in Ascending Order depending upon city Names from table name "personal" using ORDER BY then we can use below sql query :

SELECT * FROM personal ORDER BY city;


If we want to Ascending Order to multiple Columns from table name "personal" using ORDER BY as shown below sql query :

SELECT * FROM personal ORDER BY name, city;


SELECT Data with DISTINCT :

SELECT DISTINCT column1, column2,... FROM table_name;


If we want to select city names from records with DISTINCT value from table name "personal" using DISTINCT then we can use below sql query :

SELECT DISTINCT city FROM personal;


If we want to select age from records with DISTINCT value with ASCENDING ORDER from table name "personal" using DISTINCT and ORDER BY then we can use  below sql query :

SELECT DISTINCT age FROM personal ORDER BY age;


MAIN USE OF "DISTINCT" IS FOR GIVING UNIQUE DATA BY REMOVING DUPLICACY AND "ORDER BY" IS FOR ARRANGING COLUMN DATA IN ASCENDING ORDER AND DESCENDING ORDER.


Comments