Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

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.
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
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.
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query.
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.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
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.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
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.
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.
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.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
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.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
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.
Structured Query Language
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
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.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
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.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Basic select statement
Restricting and Sorting Data
Using Subqueries to Solve Queries
Restricting and Sorting Data
Aggregating Data Using Group Functions
Writing Basic SQL SELECT Statements
(SQL) Aggregating Data Using Group Functions
Using the Set Operators
Restricting and Sorting Data
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Using Subqueries to Solve Queries
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Using CASE Value expression
Restricting and Sorting Data
‘ORDER BY’ Order by clause allows sorting of query results by one or more columns. Sorting can be done in ascending or descending. Default order is ascending.
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Restricting and Sorting Data
MySQL SQL for MySQL (I) Salim Mail :
Aggregating Data Using Group Functions
Lab 2: Retrieving Data from the Database
Presentation transcript:

Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order of precedence. You can override the default order by using parentheses around the expressions you want to calculate first.

Rules of Precedence Order Evaluated Operator 1 Arithmetic operators 2 Concatenation operator 3 Comparison conditions 4 IS [NOT] NULL, LIKE, [NOT] IN 5 [NOT] BETWEEN 6 NOT logical condition 7 AND logical condition 8 OR logical condition

Rules of Precedence (Example)

Rules of Precedence (Example) In the previews slide there are two conditions: The first condition is that the job ID is AD_PRES and the salary is greater than $15,000. The second condition is that the job ID is SA_REP. Based on the precedence rules, the SELECT statement reads as follows: “Select the row if an employee is a president and earns more than $15,000, or if the employee is a sales representative.”

Rules of Precedence (Example)

Rules of Precedence (Example) In thepreviews example, there are two conditions: The first condition is that the job ID is AD_PRES or SA_REP. The second condition is that salary is greater than $15,000. Based on the precedence rules, the SELECT statement reads as follows: “Select the rows if the employee is president or sales representative, and if the employee earn more than 15,000$ ”

Order by caluse

Sorting resulted rows SQL allows sorting resulted rows by using the ORDER BY clause in: ASC: ascending order (the default order) .(see Example 10) DESC: descending order.(see Example11) The ORDER BY clause comes last in the SELECT statement

Example 10

Example 11

Sorting by Column Alias

Sorting by Multiple Columns

Select statement syntax with the (Where & order by clauses)

Comments on Using Logical operator (NOT) In term of syntax, generally, NOT comes between exper and comparison operator E.g Select fname, age Feom emp_table Where dept_num NOT IN(1,2);

Comments on Using Logical operator (NOT) This syntax is right for the operators (IN, Between.. And .., LIKE, IS NULl) BUT In case the symbolic comparison operator (>,<, >=,<=,=,<>) there will be an error E.g Select fname, age Feom emp_table Where dept_num NOT >3;

Comments on Using Logical operator (NOT) The Solution is to use NOT pefore the whole comparsion condition i.e. NOT(exper comparison operator) E.g Select fname, age Feom emp_table Where NOT (dept_num >3);

Comments on ordering table using more than one column Assume that we’ve created the following table: Then fill it with the values Create table test2( col1 number(2), col2 number(2)); col2 Col1 9 1 8 2 10 3 5 4

Comments on ordering table using more than one column The result of the query Select * From test2 Oreder by col2,col1; col2 Col1 4 3 5 8 2 9 1 10 col2 Col1 4 3 5 8 2 9 1 10 And it’s NOT the same as ordering based on the last column (col2) which is: