In this post, we will see how to use Order by keyword and is null operator. The order by keyword is used to sort the data in ascending order or in descending order. We need to specify the order or else by default it sorts the data in ascending order.
Order By Syntax
select * from table_name
order by column_name1 ASC/DESC;
Order by ASC
The following command will sort the data from the student’s table according to the alphabetical order of the Name column.
select * from students
order by Name ASC;
Order by DESC
The following command will sort the employee’s table in descending order of the EmployeeId column.
select * from employees
order by EmployeeId DESC;
DESC and ASC in one command
In the following command, the table will be sorted alphabetically for the Name column, and if similar names exist for records, those records will be ordered in descending order among them.
select * from employees
order by Name ASC, City DESC;
What is NULL?
The field which does not hold any value is said to be NULL. It is difficult to use comparison operators with NULL values.
IS NULL Syntax
This returns the records, which contain NULL values.
select * from table_name
where column_name1 IS NULL;
IS NOT NULL Syntax
This return the records, which don’t have NULL values.
select * from table_name
where column_name1 IS NOT NULL;