Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
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.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
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.
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.
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.
Copyright  Oracle Corporation, All rights reserved. Introduction.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Dr. Philip Cannata 1 Programming Languages Prolog Part 3 SQL & Prolog.
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.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
Subqueries.
Subqueries.
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. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL. Relating Multiple Tables Relational Database Terminology Row PK Column FK Field NULL.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
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. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
SQL: Part 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Review SQL Advanced. Capabilities of SQL SELECT Statements Selection Projection Table 1 Table 2 Table 1 Join.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Oracle CONNECT BY function JAVA WEB Programming. Emp 테이블의 내용 ( 상 / 하급자 계층구조 ) SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
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.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Defining a Column Alias
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
亿阳信通股份有限公司 SQL 以oracle环境为基础.
Restricting and Sorting Data
Relational Normalization Theory
Enhanced Guide to Oracle 10g
Aggregating Data Using Group Functions
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Aggregating Data Using Group Functions
Structured Query Language (SQL)
Subqueries Schedule: Timing Topic 25 minutes Lecture
Review SQL Advanced.
Restricting and Sorting Data
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
Presentation transcript:

Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns TRUE if the following condition is FALSE

Using the AND Operator AND requires both conditions to be TRUE. SQL> SELECT empno, ename, job, sal 2 FROM emp 3 WHERE sal>= AND job='CLERK'; EMPNO ENAME JOB SAL ADAMS CLERK MILLER CLERK 1300

Using the OR Operator OR requires either condition to be TRUE. SQL> SELECT empno, ename, job, sal 2 FROM emp 3 WHERE sal>= OR job='CLERK'; EMPNO ENAME JOB SAL KING PRESIDENT BLAKE MANAGER CLARK MANAGER JONES MANAGER MARTIN SALESMAN JAMES CLERK rows selected.

Using the NOT Operator SQL> SELECT ename, job 2 FROM emp 3 WHERE job NOT IN ('CLERK','MANAGER','ANALYST'); ENAME JOB KING PRESIDENT MARTIN SALESMAN ALLEN SALESMAN TURNER SALESMAN WARD SALESMAN

Rules of Precedence Override rules of precedence by using parentheses. Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR

Rules of Precedence ENAME JOB SAL KING PRESIDENT 5000 MARTIN SALESMAN 1250 ALLEN SALESMAN 1600 TURNER SALESMAN 1500 WARD SALESMAN 1250 ENAME JOB SAL KING PRESIDENT 5000 MARTIN SALESMAN 1250 ALLEN SALESMAN 1600 TURNER SALESMAN 1500 WARD SALESMAN 1250 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE job='SALESMAN' 4 OR job='PRESIDENT' 5 AND sal>1500;

Example of Precedence of AND Operator In the slide example, there are two conditions: The first condition is that job is PRESIDENT and salary is greater than The second condition is that job is SALESMAN. Therefore, the SELECT statement reads as follows: “Select the row if an employee is a PRESIDENT and earns more than $1500 or if the employee is a SALESMAN.”

Rules of Precedence ENAME JOB SAL KING PRESIDENT 5000 ALLEN SALESMAN 1600 ENAME JOB SAL KING PRESIDENT 5000 ALLEN SALESMAN 1600 Use parentheses to force priority. SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE (job='SALESMAN' 4 OR job='PRESIDENT') 5 AND sal>1500;

Using Parentheses In the example, there are two conditions: The first condition is that job is PRESIDENT or SALESMAN. The second condition is that salary is greater than Therefore, the SELECT statement reads as follows: “Select the row if an employee is a PRESIDENT or a SALESMAN and if the employee earns more than $1500.”

ORDER BY Clause Sort rows with the ORDER BY clause ASC: ascending order, default DESC: descending order The ORDER BY clause comes last in the SELECT statement. Sort rows with the ORDER BY clause ASC: ascending order, default DESC: descending order The ORDER BY clause comes last in the SELECT statement. SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate; ENAME JOB DEPTNO HIREDATE SMITH CLERK DEC-80 ALLEN SALESMAN FEB rows selected.

Syntax SELECT expr FROM table [WHERE condition(s)] [ORDER BY{column, expr} [ASC|DESC]]; where:ORDER BYspecifies the order in which the retrieved rows are displayed ASCorders the rows in ascending order (this is the default order) DESCorders the rows in descending order

Sorting in Descending Order SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate DESC; ENAME JOB DEPTNO HIREDATE ADAMS CLERK JAN-83 SCOTT ANALYST DEC-82 MILLER CLERK JAN-82 JAMES CLERK DEC-81 FORD ANALYST DEC-81 KING PRESIDENT NOV-81 MARTIN SALESMAN SEP rows selected.

Default Ordering of Data The default sort order is ascending: Numeric values are displayed with the lowest values first—for example, 1–999. Date values are displayed with the earliest value first—for example, 01-JAN-92 before 01-JAN-95. Character values are displayed in alphabetical order—for example, A first and Z last. Null values are displayed last for ascending sequences and first for descending sequences. Reversing the Default Order To reverse the order in which rows are displayed, specify the keyword DESC after the column name in the ORDER BY clause. The slide example sorts the result by the most recently hired employee.

Sorting by Column Alias SQL> SELECT empno, ename, sal*12 annsal 2 FROM emp 3 ORDER BY annsal; EMPNO ENAME ANNSAL SMITH JAMES ADAMS MARTIN WARD MILLER TURNER rows selected.

Sorting by Multiple Columns The order of ORDER BY list is the order of sort. You can sort by a column that is not in the SELECT list. SQL> SELECT ename, deptno, sal 2 FROM emp 3 ORDER BY deptno, sal DESC; ENAME DEPTNO SAL KING CLARK MILLER FORD rows selected.

Sorting by Multiple Columns You can sort query results by more than one column. The sort limit is the number of columns in the given table. In the ORDER BY clause, specify the columns, and separate the column names using commas. If you want to reverse the order of a column, specify DESC after its name. You can order by columns that are not included in the SELECT clause.

Summary SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];