select advanced. sorting data in a table the order by clause is used for sorting the data in either...

14
SELECT Advanced

Upload: marybeth-lucas

Post on 24-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

SELECT Advanced

Page 2: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

Sorting data in a table

• The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition specified in the select statement. In increasing order, lowest numeric values, earliest date and alphabetical characters will be displayed first.

SELECT * FROM <tablename> ORDER BY <columnname> [sort order]

Page 3: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

Sorting data in a table

Sort order can be:• Ascending (ASC)• Descending (DESC)• By Default: Sort order is ascending.

Page 4: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

ORDER BY CLAUSE

SELECT * FROM emp ORDER BY sal;It will give the data in the increasing order of salary, all records having null values in the salary column will be displayed in the last.

For getting the data in decreasing order of salary, we use DESC at the end of the above syntax.

SELECT * FROM emp ORDER BY sal DESC;

Page 5: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

ORDER BY CLAUSE

For getting data in descending order of deptno and ascending order of sal, we will use the following syntax:

SELECT * FROM emp ORDER BY deptno DESC, sal ASC;

Page 6: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

NULL• NULL: If a row lacks a value for a particular

column, then the value is said to be null. • A null value is either unavailable, unassigned,

unknown or inapplicable. • A null value is not same as zero or blank. Null

values can be inserted into the column of any data type.

• Null values are correctly handled by SQL. If any column value in an expression is null, the result is null.

Page 7: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

NULLe.g. SELECT ename, sal*12 Annual_sal FROM emp;

ENAME ANNUAL_SAL -------------------- ---------- Axay 96000 Meenal 48000 Tanu 54000 Rahul Vivek 66000 7 rows selected.If corresponding to some employees salaries are not assigned i.e. there will be NULL corresponding to those employees then the expression sal*12 will again be NULL.

Page 8: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

Queries

• If the null values in multiple records corresponding to a unique constraint type column is allowed or not?

• In case of projection of distinct values for column , only one null value is returned if column has null in multiple records.

Page 9: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

Emp_id (unique) ename

- pooja

- pooja1

1 pooja2

2 pooja3

Emp_id (distinct)

-

1

2

Page 10: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

NVL

• In order to achieve a result for all employees, it is necessary to convert a null value to a known value. We use NVL function to convert a null value to a non – null value;

• SELECT ename, NVL(sal, 5000)*12 Annual_sal FROM emp

Page 11: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

NVL

ENAME ANNUAL_SAL -------------------- ---------- Axay 96000 Ashish 60000 Sparsh 72000 Rahul 60000 Vivek 66000 7 rows selected.

Page 12: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

NVL

• NVL function assigns the value 5000 to those employees where sal is NULL and then calculates the expression.

• NVL function accepts two arguments – an expression and a non-null value. In case of date and character column we use the single quotes to assign the values to the columns as –

• NVL (date_join, ‘01-JAN-98’) and NVL (ename, ‘Axay’)

Page 13: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

IS NULL• If we want to display all those employees for whom

salary is not yet assigned. The probable query may be

• SELECT ename FROM emp WHERE sal=NULL;

• But unfortunately, it does not return any row in spite of having NULL values in sal column.

• When we compare any salary to NULL like whether 5000=NULL or 3000=NULL or even NULL=NULL, it always return false because nothing can be equal to null not even null. Null is unknown value, thus returning no record.

Page 14: SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition

IS NULL

• To overcome this problem we have SQL operator IS NULL, which is used as follows:

• SELECT ename FROM emp WHERE sal IS NULL; ENAME -------------------- Rahul 1 rows selected.