Conditional Retrieval
Conditional retrieval enables selective rows to be selected. Although selecting rows, restriction can be applied by a condition which governs the selection. Further clause called WHERE must be provided along with the SELECT statement to apply the condition to select a specific set of rows.The order of prior first goes to the WHERE clause and the records that match the condition are alone selected.
Syntax:
Select (column name(s)) FROM (table name(s)) WHERE condition(s)
Let consider selecting employee records whose salary is equivalent to or greater than 3000. The query can be written as follows,
Example:
SELECT empno,empname,salary FROM employee where salary >=3000; The records selected will be,
Example:
# for select the employee who have 1000 salary# SELECT * FROM employee WHERE salary=1000;
The display would be no rows selected because there are no records matching the condition specified in the WHERE clause.
Retrieving Records in a sorted order
Records selected can be displayed in two forms either in ascending order or in descending order based on the column specified. The ORDER BY clause is used to perform this operation.
Syntax:
Select (column name(s))
FROM (table name(s)) WHERE condition(s) ORDER BY <column name(s)>
Example:
# for select the employee by name order# SELECT * FROM employee ORDER BY empname;
This would shows
There is a difference in the looks. This query displays all the records in the employee table sorted in ascending order of the employee name. Ascending is the order by default in that the records are displayed. If the record needs to be displayed in descending order, use DESC along with the column name.
Instance, selecting employee via name in descending order SELECT * FROM employee ORDER BY empname DESC;Here list will be sorted in the descending order. So the looks will be totally different as shown as follows:
Sorting records can be done on more than one column. In that case, sorting is done on the first column and then within that sorting is done on the second column. The following example describes this.
Consider that sorting is to be done on the salary column in ascending order and then within that on the empname column in descending order.SELECT empno,empname,salary FROM employee ORDER BY salary,empname DESC; This query sorts first the salary and within that the employee name that would look like:
Note: DESC indicates descending order and this is not the similar as DESC in SQL * Plus.