SlideShare a Scribd company logo
Restricting and Sorting Data
Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT [DISTINCT] {*|  column  [ alias ], ...} FROM  table [WHERE condition(s) ];
Using the WHERE Clause SQL> SELECT ename, job, deptno 2  FROM  emp 3  WHERE  job='CLERK'; ENAME  JOB  DEPTNO ---------- --------- --------- JAMES  CLERK  30 SMITH  CLERK  20 ADAMS  CLERK  20 MILLER  CLERK  10
Character Strings and Dates Character strings and date values are enclosed in single quotation marks. Character values are case sensitive and date values are format sensitive. The default date format is DD-MON-YY . SQL> SELECT ename, job, deptno 2  FROM  emp 3  WHERE ename =  ; 'JAMES'
Comparison Operators Operator = > >= < <= <> Meaning Equal to Greater than  Greater than or equal to  Less than  Less than or equal to Not equal to
Using the Comparison Operators SQL> SELECT ename, sal, comm 2  FROM  emp 3  WHERE  sal<=comm; ENAME  SAL  COMM ---------- --------- --------- MARTIN  1250  1400
Other Comparison Operators Operator BETWEEN ...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive) Match any of a list of values  Match a character pattern  Is a null value
Using the BETWEEN Operator Use the BETWEEN operator to display rows based on a range of values. ENAME  SAL ---------- --------- MARTIN  1250 TURNER  1500 WARD  1250 ADAMS  1100 MILLER  1300 SQL> SELECT ename, sal 2  FROM  emp 3  WHERE sal BETWEEN 1000 AND 1500; Lower limit Higher limit
Using the IN Operator SQL> SELECT empno, ename, sal, mgr 2  FROM  emp 3  WHERE mgr IN (7902, 7566, 7788); EMPNO ENAME  SAL  MGR --------- ---------- --------- --------- 7902 FORD  3000  7566 7369 SMITH  800  7902 7788 SCOTT  3000  7566 7876 ADAMS  1100  7788
Using the LIKE Operator Use the LIKE operator to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers. % denotes zero or many characters. _ denotes one character. SQL> SELECT ename 2  FROM  emp 3  WHERE ename LIKE 'S%';
Using the LIKE Operator You can combine pattern-matching characters. You can use the ESCAPE identifier to search for &quot;%&quot; or &quot;_&quot;. SQL> SELECT ename 2  FROM emp 3  WHERE ename LIKE '_A%'; ENAME ----------  MARTIN JAMES  WARD
Using the IS NULL Operator Test for null values with the IS NULL operator. SQL> SELECT  ename, mgr 2  FROM  emp 3  WHERE  mgr IS NULL; ENAME  MGR ---------- --------- KING
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>=1100 4  AND  job='CLERK'; EMPNO ENAME  JOB  SAL --------- ---------- --------- --------- 7876 ADAMS  CLERK  1100 7934 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>=1100 4  OR  job='CLERK'; EMPNO ENAME  JOB  SAL --------- ---------- --------- --------- 7839 KING  PRESIDENT  5000 7698 BLAKE  MANAGER  2850 7782 CLARK  MANAGER  2450 7566 JONES  MANAGER  2975 7654 MARTIN  SALESMAN  1250 ...  7900 JAMES  CLERK  950 ... 14 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 Evaluated Operator 1 All comparison  operators 2 NOT 3 AND 4 OR
Rules of Precedence 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;
Rules of Precedence 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;
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. SQL> SELECT   ename, job, deptno, hiredate 2  FROM   emp 3  ORDER BY hiredate; ENAME  JOB  DEPTNO HIREDATE ---------- --------- --------- --------- SMITH  CLERK  20 17-DEC-80 ALLEN  SALESMAN  30 20-FEB-81 ... 14 rows selected.
Sorting in Descending Order SQL> SELECT   ename, job, deptno, hiredate 2  FROM   emp 3  ORDER BY hiredate DESC; ENAME  JOB  DEPTNO HIREDATE ---------- --------- --------- --------- ADAMS  CLERK  20 12-JAN-83 SCOTT  ANALYST  20 09-DEC-82 MILLER  CLERK  10 23-JAN-82 JAMES  CLERK  30 03-DEC-81 FORD  ANALYST  20 03-DEC-81 KING  PRESIDENT  10 17-NOV-81 MARTIN  SALESMAN  30 28-SEP-81 ... 14 rows selected.
Sorting by Column Alias SQL> SELECT  empno, ename, sal*12 annsal 2  FROM  emp 3  ORDER BY annsal; EMPNO ENAME  ANNSAL --------- ---------- --------- 7369 SMITH  9600 7900 JAMES  11400 7876 ADAMS  13200 7654 MARTIN  15000 7521 WARD  15000 7934 MILLER  15600 7844 TURNER  18000 ... 14 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  10  5000 CLARK  10  2450 MILLER  10  1300 FORD  20  3000 ... 14 rows selected.
Summary SELECT [DISTINCT] {*|  column  [ alias ], ...} FROM  table [WHERE condition(s) ] [ORDER BY { column, expr, alias } [ASC|DESC]];
Practice Overview Selecting data and changing the order of rows displayed Restricting rows by using the WHERE clause Using the double quotation marks in column aliases

More Related Content

PPT
SQL WORKSHOP::Lecture 2
Umair Amjad
 
PPTX
Les05 Aggregating Data Using Group Function
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
PPT
Les02
arnold 7490
 
PPTX
Les04 Displaying Data From Multiple Table
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
PDF
COIS 420 - Practice02
Angel G Diaz
 

What's hot (20)

PPT
Les03
arnold 7490
 
PPT
Les01
arnold 7490
 
PPTX
12c Mini Lesson - ANSI standard TOP-N query syntax
Connor McDonald
 
PPT
SQL WORKSHOP::Lecture 12
Umair Amjad
 
PDF
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
Mohamed Moustafa
 
PPT
Les12
arnold 7490
 
PPT
Les01
Yousif Misbah
 
DOCX
It6312 dbms lab-ex2
MNM Jain Engineering College
 
PPT
Les10
arnold 7490
 
PPT
Les09
arnold 7490
 
PPT
Les11
arnold 7490
 
DOCX
Trig
alur raju
 
DOC
ORACLE NOTES
Sachin Shukla
 
DOC
Chapter 1
waqaszaidi91
 
PPT
Les08-Oracle
suman1248
 
PDF
Database Management System
Hitesh Mohapatra
 
12c Mini Lesson - ANSI standard TOP-N query syntax
Connor McDonald
 
SQL WORKSHOP::Lecture 12
Umair Amjad
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
Mohamed Moustafa
 
It6312 dbms lab-ex2
MNM Jain Engineering College
 
Trig
alur raju
 
ORACLE NOTES
Sachin Shukla
 
Chapter 1
waqaszaidi91
 
Les08-Oracle
suman1248
 
Database Management System
Hitesh Mohapatra
 
Ad

Viewers also liked (6)

DOCX
Tupen 8 1235010002
Abrianto Nugraha
 
PPT
Les02
Vijay Kumar
 
PDF
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
ux singapore
 
PDF
SEO: Getting Personal
Kirsty Hulse
 
PDF
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Stanford GSB Corporate Governance Research Initiative
 
Tupen 8 1235010002
Abrianto Nugraha
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
ux singapore
 
SEO: Getting Personal
Kirsty Hulse
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Stanford GSB Corporate Governance Research Initiative
 
Ad

Similar to Les02 (20)

PPT
Sql2
Nargis Ehsan
 
PPT
Restricting and sorting data
HuzaifaMushtaq3
 
PPT
chap2 (3).ppt
eemantariq2
 
PPTX
Les02.pptx
NishaTariq1
 
PDF
Structure query language - Data Query language for beginners.pdf
munmunitjusl
 
PPT
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
PPT
e computer notes - Restricting and sorting data
ecomputernotes
 
PPT
Day1_Structured Query Language2_To understand.ppt
consravs
 
PPT
Les02
Sudharsan S
 
PPT
Les02 (restricting and sorting data)
Achmad Solichin
 
PPT
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
PPT
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
PPT
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
PPT
Chinabankppt
newrforce
 
PPT
Les02.ppt
gfhfghfghfgh1
 
PPT
Chapter-4.ppt
CindyCuesta
 
PPT
Sql 2006
Cathie101
 
PPT
SQL- Introduction to MySQL
Vibrant Technologies & Computers
 
PPT
Restricting and sorting data
Syed Zaid Irshad
 
PPT
Sqlplus
dillip kar
 
Restricting and sorting data
HuzaifaMushtaq3
 
chap2 (3).ppt
eemantariq2
 
Les02.pptx
NishaTariq1
 
Structure query language - Data Query language for beginners.pdf
munmunitjusl
 
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
e computer notes - Restricting and sorting data
ecomputernotes
 
Day1_Structured Query Language2_To understand.ppt
consravs
 
Les02 (restricting and sorting data)
Achmad Solichin
 
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
Chinabankppt
newrforce
 
Les02.ppt
gfhfghfghfgh1
 
Chapter-4.ppt
CindyCuesta
 
Sql 2006
Cathie101
 
SQL- Introduction to MySQL
Vibrant Technologies & Computers
 
Restricting and sorting data
Syed Zaid Irshad
 
Sqlplus
dillip kar
 

Recently uploaded (20)

PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Doc9.....................................
SofiaCollazos
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
The Future of Artificial Intelligence (AI)
Mukul
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 

Les02

  • 2. Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT [DISTINCT] {*| column [ alias ], ...} FROM table [WHERE condition(s) ];
  • 3. Using the WHERE Clause SQL> SELECT ename, job, deptno 2 FROM emp 3 WHERE job='CLERK'; ENAME JOB DEPTNO ---------- --------- --------- JAMES CLERK 30 SMITH CLERK 20 ADAMS CLERK 20 MILLER CLERK 10
  • 4. Character Strings and Dates Character strings and date values are enclosed in single quotation marks. Character values are case sensitive and date values are format sensitive. The default date format is DD-MON-YY . SQL> SELECT ename, job, deptno 2 FROM emp 3 WHERE ename = ; 'JAMES'
  • 5. Comparison Operators Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
  • 6. Using the Comparison Operators SQL> SELECT ename, sal, comm 2 FROM emp 3 WHERE sal<=comm; ENAME SAL COMM ---------- --------- --------- MARTIN 1250 1400
  • 7. Other Comparison Operators Operator BETWEEN ...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive) Match any of a list of values Match a character pattern Is a null value
  • 8. Using the BETWEEN Operator Use the BETWEEN operator to display rows based on a range of values. ENAME SAL ---------- --------- MARTIN 1250 TURNER 1500 WARD 1250 ADAMS 1100 MILLER 1300 SQL> SELECT ename, sal 2 FROM emp 3 WHERE sal BETWEEN 1000 AND 1500; Lower limit Higher limit
  • 9. Using the IN Operator SQL> SELECT empno, ename, sal, mgr 2 FROM emp 3 WHERE mgr IN (7902, 7566, 7788); EMPNO ENAME SAL MGR --------- ---------- --------- --------- 7902 FORD 3000 7566 7369 SMITH 800 7902 7788 SCOTT 3000 7566 7876 ADAMS 1100 7788
  • 10. Using the LIKE Operator Use the LIKE operator to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers. % denotes zero or many characters. _ denotes one character. SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE 'S%';
  • 11. Using the LIKE Operator You can combine pattern-matching characters. You can use the ESCAPE identifier to search for &quot;%&quot; or &quot;_&quot;. SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE '_A%'; ENAME ---------- MARTIN JAMES WARD
  • 12. Using the IS NULL Operator Test for null values with the IS NULL operator. SQL> SELECT ename, mgr 2 FROM emp 3 WHERE mgr IS NULL; ENAME MGR ---------- --------- KING
  • 13. 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
  • 14. Using the AND Operator AND requires both conditions to be TRUE. SQL> SELECT empno, ename, job, sal 2 FROM emp 3 WHERE sal>=1100 4 AND job='CLERK'; EMPNO ENAME JOB SAL --------- ---------- --------- --------- 7876 ADAMS CLERK 1100 7934 MILLER CLERK 1300
  • 15. Using the OR Operator OR requires either condition to be TRUE. SQL> SELECT empno, ename, job, sal 2 FROM emp 3 WHERE sal>=1100 4 OR job='CLERK'; EMPNO ENAME JOB SAL --------- ---------- --------- --------- 7839 KING PRESIDENT 5000 7698 BLAKE MANAGER 2850 7782 CLARK MANAGER 2450 7566 JONES MANAGER 2975 7654 MARTIN SALESMAN 1250 ... 7900 JAMES CLERK 950 ... 14 rows selected.
  • 16. 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
  • 17. Rules of Precedence Override rules of precedence by using parentheses. Order Evaluated Operator 1 All comparison operators 2 NOT 3 AND 4 OR
  • 18. Rules of Precedence 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;
  • 19. Rules of Precedence 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;
  • 20. 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. SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate; ENAME JOB DEPTNO HIREDATE ---------- --------- --------- --------- SMITH CLERK 20 17-DEC-80 ALLEN SALESMAN 30 20-FEB-81 ... 14 rows selected.
  • 21. Sorting in Descending Order SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate DESC; ENAME JOB DEPTNO HIREDATE ---------- --------- --------- --------- ADAMS CLERK 20 12-JAN-83 SCOTT ANALYST 20 09-DEC-82 MILLER CLERK 10 23-JAN-82 JAMES CLERK 30 03-DEC-81 FORD ANALYST 20 03-DEC-81 KING PRESIDENT 10 17-NOV-81 MARTIN SALESMAN 30 28-SEP-81 ... 14 rows selected.
  • 22. Sorting by Column Alias SQL> SELECT empno, ename, sal*12 annsal 2 FROM emp 3 ORDER BY annsal; EMPNO ENAME ANNSAL --------- ---------- --------- 7369 SMITH 9600 7900 JAMES 11400 7876 ADAMS 13200 7654 MARTIN 15000 7521 WARD 15000 7934 MILLER 15600 7844 TURNER 18000 ... 14 rows selected.
  • 23. 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 10 5000 CLARK 10 2450 MILLER 10 1300 FORD 20 3000 ... 14 rows selected.
  • 24. Summary SELECT [DISTINCT] {*| column [ alias ], ...} FROM table [WHERE condition(s) ] [ORDER BY { column, expr, alias } [ASC|DESC]];
  • 25. Practice Overview Selecting data and changing the order of rows displayed Restricting rows by using the WHERE clause Using the double quotation marks in column aliases

Editor's Notes

  • #2: Schedule: Timing Topic 45 minutes Lecture 30 minutes Practice 75 minutes Total
  • #3: Limiting Rows Selected You can restrict the rows returned from the query by using the WHERE clause. A WHERE clause contains a condition that must be met, and it directly follows the FROM clause. In the syntax: WHERE restricts the query to rows that meet a condition condition is composed of column names, expressions, constants, and a comparison operator The WHERE clause can compare values in columns, literal values, arithmetic expressions, or functions. The WHERE clause consists of three elements: Column name Comparison operator Column name, constant, or list of values
  • #4: Using the WHERE Clause In the example, the SELECT statement retrieves the name, job title, and department number of all employees whose job title is CLERK. Note that the job title CLERK has been specified in uppercase to ensure that the match is made with the job column in the EMP table. Character strings are case sensitive. Instructor Note Snippet: “Processing a Query”
  • #5: Character Strings and Dates Character strings and dates in the WHERE clause must be enclosed in single quotation marks ( &apos;&apos; ). Number constants, however, should not. All character searches are case sensitive. In the following example, no rows are returned because the EMP table stores all the data in uppercase: SQL&gt; SELECT ename, empno, job, deptno 2 FROM emp 3 WHERE job=&apos;clerk&apos;; Oracle stores dates in an internal numeric format, representing the century, year, month, day, hours, minutes, and seconds. The default date display is DD-MON-YY. Note: Changing default date format will be covered in a subsequent lesson. Number values are not enclosed within quotation marks. Instructor Note Some students may ask how to override the case sensitivity. Later in the course, we will cover the use of single-row functions such as UPPER and LOWER to override the case sensitivity.
  • #6: Comparison Operators Comparison operators are used in conditions that compare one expression to another. They are used in the WHERE clause in the following format: Syntax … WHERE expr operator value Examples … WHERE hiredate=&apos;01-JAN-95&apos; … WHERE sal&gt;=1500 … WHERE ename=&apos;SMITH&apos; Instructor Note Remind students that the expr cannot be an alias. Mention in brief about the date TO_DATE function, when the date is to be entered in a format other than the default format. This is required for practice 4.
  • #7: Using the Comparison Operators In the example, the SELECT statement retrieves name, salary, and commission from the EMP table, where the employee salary is less than or equal to the commission amount. Note that there is no explicit value supplied to the WHERE clause. The two values being compared are taken from the SAL and COMM columns in the EMP table. Instructor Note Rows that have a null value in the COMM column result in a null value for the comparison expression and are effectively not part of the result.
  • #9: The BETWEEN Operator You can display rows based on a range of values using the BETWEEN operator. The range that you specify contains a lower range and an upper range. The SELECT statement on the slide returns rows from the EMP table for any employee whose salary is between $1000 and $1500. Values specified with the BETWEEN operator are inclusive. You must specify the lower limit first. Instructor Note Emphasize that the values specified with the BETWEEN operator in the example are inclusive. Point out that Turner, who earns $1500 (higher limit), is included in the output. Explain that BETWEEN … AND … are actually translated by Oracle server to a pair of AND conditions, (a &gt;= lower limit) and (a &lt;= higher limit). So using BETWEEN … AND … has no performance benefits and can be used for logical simplicity. Demo: l2betw.sql Purpose: To illustrate using the BETWEEN operator.
  • #10: The IN Operator To test for values in a specified list, use the IN operator. The slide example displays employee number, name, salary, and manager’s employee number of all the employees whose manager’s employee number is 7902, 7566, or 7788. The IN operator can be used with any datatype. The following example returns a row from the EMP table for any employee whose name is included in the list of names in the WHERE clause: SQL&gt; SELECT empno, ename, mgr, deptno 2 FROM emp 3 WHERE ename IN (&apos;FORD&apos; , &apos;ALLEN&apos;); If characters or dates are used in the list, they must be enclosed in single quotation marks ( &apos;&apos; ). Instructor Note Explain that the IN ( … ) is actually translated by Oracle server to a set of OR conditions (a = value1 OR a = value2 OR a = value3 ). So using IN ( … ) has no performance benefits and can be used for logical simplicity. Demo: l2in.sql Purpose: To illustrate using the IN operator.
  • #11: The LIKE Operator You may not always know the exact value to search for. You can select rows that match a character pattern by using the LIKE operator. The character pattern-matching operation is referred to as a wildcard search. Two symbols can be used to construct the search string. The SELECT statement above returns the employee name from the EMP table for any employee whose name begins with an “S.” Note the uppercase “S.” Names beginning with an “s” will not be returned. The LIKE operator can be used as a shortcut for some BETWEEN comparisons. The following example displays names and hire dates of all employees who joined between January 1981 and December 1981: SQL&gt; SELECT ename, hiredate 2 FROM emp 3 WHERE hiredate LIKE &apos;%1981&apos;;
  • #12: Combining Wildcard Characters The % and _ symbols can be used in any combination with literal characters. The example on the slide displays the names of all employees whose name has an “A” as the second character. The ESCAPE Option When you need to have an exact match for the actual ‘%’ and ‘_’ characters, use the ESCAPE option. This option specifies what the ESCAPE character is. If you have HEAD_QUARTERS as a department name, you would search for it using the following SQL statement: The ESCAPE option identifies the backslash (\\) as the escape character. In the pattern, the escape character precedes the underscore (_). This causes the Oracle Server to interpret the underscore literally. SQL&gt; SELECT * FROM dept 2 WHERE dname LIKE &apos; %\\_% &apos; ESCAPE &apos;\\&apos;; DEPTNO DNAME LOC --------- -------------- ------------- 50 HEAD_QUARTERS ATLANTA
  • #13: The IS NULL Operator The IS NULL operator tests for values that are null. A null value means the value is unavailable, unassigned, unknown, or inapplicable. Therefore, you cannot test with (=) because a null value cannot be equal or unequal to any value. The slide example retrieves the name and manager of all employees who do not have a manager. For example, to display name, job title, and commission for all employees who are not entitled to get a commission, use the following SQL statement: SQL&gt; SELECT ename, job, comm 2 FROM emp 3 WHERE comm IS NULL; ENAME JOB COMM -------- ----------- ------ KING PRESIDENT BLAKE MANAGER CLARK MANAGER ...
  • #14: Logical Operators A logical operator combines the result of two component conditions to produce a single result based on them or to invert the result of a single condition. Three logical operators are available in SQL: AND OR NOT All the examples so far have specified only one condition in the WHERE clause. You can use several conditions in one WHERE clause using the AND and OR operators.
  • #15: The AND Operator In the example, both conditions must be true for any record to be selected. Therefore, an employee who has a job title of CLERK and earns more than $1100 will be selected. All character searches are case sensitive. No rows are returned if CLERK is not in uppercase. Character strings must be enclosed in quotation marks. AND Truth Table The following table shows the results of combining two expressions with AND: Instructor Note Demo: l2and.sql Purpose: To illustrate using the AND operator.
  • #16: The OR Operator In the example, either condition can be true for any record to be selected. Therefore, an employee who has a job title of CLERK or earns more than $1100 will be selected. The OR Truth Table The following table shows the results of combining two expressions with OR: Instructor Note Demo: l2or.sql Purpose: To illustrate using the OR operator.
  • #17: The NOT Operator The slide example displays name and job title of all the employees whose job title is not CLERK, MANAGER, or ANALYST. The NOT Truth Table The following table shows the result of applying the NOT operator to a condition: Note: The NOT operator can also be used with other SQL operators, such as BETWEEN, LIKE, and NULL. ... WHERE job NOT IN (&apos;CLERK&apos;, &apos;ANALYST&apos;) ... WHERE sal NOT BETWEEN 1000 AND 1500 ... WHERE ename NOT LIKE &apos;%A%&apos; ... WHERE comm IS NOT NULL
  • #19: 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 1500. 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.” Instructor Note Demo: l2sal1.sql Purpose: To illustrate the rules of precedence.
  • #20: 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 1500. 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.” Instructor Note Demo: l2sal2.sql Purpose: To illustrate the rules of precedence.
  • #21: The ORDER BY Clause The order of rows returned in a query result is undefined. The ORDER BY clause can be used to sort the rows. If you use the ORDER BY clause, you must place last. You can specify an expression or an alias to sort. Syntax SELECT expr FROM table [WHERE condition(s) ] [ORDER BY { column , expr } [ASC|DESC]]; where: ORDER BY specifies the order in which the retrieved rows are displayed ASC orders the rows in ascending order (this is the default order) DESC orders the rows in descending order If the ORDER BY clause is not used, the sort order is undefined, and the Oracle Server may not fetch rows in the same order for the same query twice. Use the ORDER BY clause to display the rows in a specific order. Instructor Note Let the students know that the ORDER BY clause is executed last in query execution.
  • #22: 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. Instructor Note Let the students know that you can also sort by a column number in the SELECT list. The example below will sort the output in the descending order by salary. SQL&gt; SELECT ename, sal 2 FROM emp 3 ORDER BY 2 DESC;
  • #23: Sorting by Column Aliases You can use a column alias in the ORDER BY clause. The slide example sorts the data by annual salary.
  • #24: 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. Example Display name and salary of all employees. Order the result by department number and then descending order by salary. Instructor Note Show that the DEPTNO column is sorted in ascending order and the SAL column in descending order. SQL&gt; SELECT ename, sal 2 FROM emp 3 ORDER BY deptno, sal DESC;
  • #25: Summary In this lesson, you have learned about restricting and sorting rows returned by the SELECT statement. You have also learned how to implement various operators.
  • #26: Practice Overview This practice gives you a variety of exercises using the WHERE clause and the ORDER BY clause.