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.

Slides:



Advertisements
Similar presentations
Sorting Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Construct a query to sort a results set in ascending.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
SQL (DDL & DML Commands)
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
Copyright س Oracle Corporation, All rights reserved. I Introduction.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Information Resource Engineering SQL3. Recap- SQL Rules and Conventions  Certain ‘keywords’ are ‘reserved’ and have special meaning in SQL e.g.: SELECT,
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Restricting and Sorting Data
Aggregating Data Using Group Functions
Basic select statement
ATS Application Programming: Java Programming
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Section 4 - Sorting/Functions
Restricting and Sorting Data
Restricting and Sorting Data
Lab 2: Retrieving Data from the Database
Presentation transcript:

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 specified in the select statement. In increasing order, lowest numeric values, earliest date and alphabetical characters will be displayed first. SELECT * FROM ORDER BY [sort order]

Sorting data in a table  Sort order can be: Ascending (ASC) Descending (DESC) By Default: Sort order is ascending.

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;

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;

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.

NULL e.g. SELECT ename, sal*12 Annual_sal FROM emp; ENAME ANNUAL_SAL Axay Meenal Tanu Rahul Vivek 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.

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.

Emp_id (unique)ename -pooja -pooja1 1pooja2 2pooja3 Emp_id (distinct) - 1 2

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

NVL ENAME ANNUAL_SAL Axay Ashish Sparsh Rahul Vivek rows selected.

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’)

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.

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.