(SQL)
Restricting and Sorting Data
Asif Sohail
University of the Punjab
Punjab University College of Information Technology (PUCIT)
Database Systems Slide 1
Objectives
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
Database Systems Slide 2
Limiting Rows Using a Selection
EMP
EMPNO ENAME JOB ... DEPTNO
"…retrieve all
7839 KING PRESIDENT 10 employees
7698 BLAKE MANAGER 30 in department 10"
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
EMP
EMPNO ENAME JOB ... DEPTNO
7839 KING PRESIDENT 10
7782 CLARK MANAGER 10
7934 MILLER CLERK 10
Database Systems Slide 3
Limiting Rows Selected
– Restrict the rows returned by using the WHERE
clause.
SELECT [DISTINCT] {*| column [alias], ...}
FROM table
[WHERE condition(s)];
– The WHERE clause follows the FROM clause.
SQL> SELECT ename, job, deptno
2 FROM emp
3 WHERE job='CLERK';
Database Systems Slide 4
Comparison Operators
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
Database Systems Slide 5
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 or
mm/dd/yyyy.
SQL>
SQL> SELECT
SELECT ename,
ename, job,
job, deptno
deptno
22 FROM
FROM emp
emp
33 WHERE
WHERE ename
ename == 'JAMES';
'JAMES';
Database Systems Slide 6
Logical Operators
Operator Meaning
AND Returns TRUE if both component
conditions are TRUE
OR Returns TRUE if either component
condition is TRUE
NOT Returns TRUE if the following
condition is FALSE
Database Systems Slide 7
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
Database Systems Slide 8
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.
Database Systems Slide 9
Rules of Precedence
Order Evaluated Operator
1 All comparison
operators
2 NOT
3 AND
4 OR
• Override rules of precedence by using
parentheses.
Database Systems Slide 10
Rules of Precedence
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE job='SALESMAN'
4 OR job='PRESIDENT'
5 AND sal>1500;
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
KING
KING PRESIDENT
PRESIDENT 5000
5000
MARTIN
MARTIN SALESMAN
SALESMAN 1250
1250
ALLEN
ALLEN SALESMAN
SALESMAN 1600
1600
TURNER
TURNER SALESMAN
SALESMAN 1500
1500
WARD
WARD SALESMAN
SALESMAN 1250
1250
Database Systems Slide 11
Rules of Precedence
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;
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
KING
KING PRESIDENT
PRESIDENT 5000
5000
ALLEN
ALLEN SALESMAN
SALESMAN 1600
1600
Database Systems Slide 12
Other Comparison Operators / Special Functions
Operator Meaning
BETWEEN Between two values (inclusive)
...AND...
IN(list) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
Database Systems Slide 13
Using the BETWEEN Operator
• Use the BETWEEN operator to display
rows based on a range of values.
SQL> SELECT ename, sal
2 FROM emp
3 WHERE sal BETWEEN 1000 AND 1500;
ENAME SAL
---------- --------- Lower Higher
MARTIN 1250 limit limit
TURNER 1500
WARD 1250
ADAMS 1100
MILLER 1300
Database Systems Slide 14
Using the IN Operator
• Use the IN operator to test for values in a
list.
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
Database Systems Slide 15
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%';
SQL> SELECT ename
2 FROM emp
3 WHERE ename LIKE '_A%';
Database Systems Slide 16
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
SQL> SELECT empno, ename, comm
2 FROM emp
3 WHERE comm IS NOT NULL;
Database Systems Slide 17
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
• Override rules of precedence by using parentheses.
Database Systems Slide 18
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.
SELECT [DISTINCT] {*| column [alias], ...}
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, alias} [ASC|DESC]];
Database Systems Slide 19
ORDER BY Clause
SQL> SELECT ename, job, deptno, hiredate
2 FROM emp
3 ORDER BY hiredate;
SQL> SELECT ename, job, deptno, hiredate
2 FROM emp
3 ORDER BY hiredate DESC;
SQL> SELECT empno, ename, sal*12 annsal
2 FROM emp
3 ORDER BY annsal;
SQL> SELECT ename, deptno, sal
2 FROM emp
3 ORDER BY deptno, sal DESC;
Database Systems Slide 20
Sorting by Column Alias
SQL> SELECT empno, ename, sal*12 annsal
2 FROM emp
3 ORDER BY annsal;
SQL> SELECT ename, deptno, sal
2 FROM emp
3 ORDER BY 2;
Database Systems Slide 21
TOP Clause
– It is used to specify the number of records the query
should return.
– Useful on large databases with thousands of records.
SELECT [DISTINCT] {*| column [alias], ...}
FROM table
[WHERE rownum <=10;
Database Systems Slide 22
Summary
In this lesson, you should have learned how to:
• Use the WHERE clause to restrict rows of output
– Use the comparison conditions
– Use the BETWEEN, IN, LIKE, and NULL conditions
– Apply the logical AND, OR, and NOT operators
• Use the ORDER BY clause to sort rows of output
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, alias} [ASC|DESC]];
Database Systems Slide 23
Thank you for your attention.
Asif Sohail
Assistant Professor
University of the Punjab
Punjab University College of Information Technology (PUCIT)
Allama Iqbal (Old) Campus, Anarkali
Lahore, Pakistan
Tel: +92-(0)42-111-923-923 Ext. 154
E-mail: [email protected]
Database Systems Slide 24