0% found this document useful (0 votes)
49 views24 pages

Lecture 12 DML Select

This document discusses the SQL SELECT statement and how it is used to filter rows from a database table. It covers the basic syntax of the SELECT statement and the WHERE clause. It also describes various operators that can be used in the WHERE clause, including comparison operators, string operators, logical operators, and NULL conditions. It provides examples of how to use each operator type to select specific rows based on column values and conditions.

Uploaded by

Test Email
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views24 pages

Lecture 12 DML Select

This document discusses the SQL SELECT statement and how it is used to filter rows from a database table. It covers the basic syntax of the SELECT statement and the WHERE clause. It also describes various operators that can be used in the WHERE clause, including comparison operators, string operators, logical operators, and NULL conditions. It provides examples of how to use each operator type to select specific rows based on column values and conditions.

Uploaded by

Test Email
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

https://sahibulsaif.wordpress.

com/wisdom/bismillah-nature/
CSC271 – DATABASE SYSTEMS

DML – SELECT STATEMENT


PREVIOUS LECTURE
Today’s Lecture

 SQL SELECT
 Row Filtering

 Operators
 Comparison operators
 String & Set operators
 Logical operators
LIMITING ROWS USING A SELECTION

EMPLOYEES

“retrieve all
employees
in department 90”
LIMITING THE ROWS SELECTED

 Restrict the rows returned by using the WHERE clause.

SELECT *|{[DISTINCT] column|expression [alias],...}


FROM table
[WHERE condition(s)];

 The WHERE clause follows the FROM clause.


USING THE WHERE CLAUSE

SELECT employee_id, last_name, job_id, department_id


FROM employees
WHERE department_id = 90 ;
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-RR.

SELECT last_name, job_id, department_id


FROM employees
WHERE last_name = 'Whalen';
COMPARISON CONDITIONS

Operator Meaning

= Equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> Not equal to


USING COMPARISON CONDITIONS

SELECT last_name, salary


FROM employees
WHERE salary <= 3000;
OTHER COMPARISON CONDITIONS

Operator Meaning

BETWEEN Between two values (inclusive),


...AND...

IN(set) Match any of a list of values

LIKE Match a character pattern

IS NULL Is a null value


USING THE BETWEEN CONDITION

Use the BETWEEN condition to display rows based on a range of values.

SELECT last_name, salary


FROM employees
WHERE salary BETWEEN 2500 AND 3500;

Lower limit Upper limit


USING THE IN CONDITION
Use the IN membership condition to test for values in a list.

SELECT employee_id, last_name, salary, manager_id


FROM employees
WHERE manager_id IN (100, 101, 201);
USING THE LIKE CONDITION

 Use the LIKE condition 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.

SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
USING THE LIKE CONDITION

 You can combine pattern-matching characters.

SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';

 You can use the ESCAPE identifier to search for the actual % and _ symbols.
USING THE NULL CONDITIONS

Test for nulls with the IS NULL operator.

SELECT last_name, manager_id


FROM employees
WHERE manager_id IS NULL;
LOGICAL CONDITIONS

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
USING THE AND OPERATOR

AND requires both conditions to be true.

SELECT employee_id, last_name, job_id, salary


FROM employees
WHERE salary >=10000
AND job_id LIKE '%MAN%';
USING THE OR OPERATOR
OR requires either conditions to be true.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%';
USING THE NOT OPERATOR

SELECT last_name, job_id


FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');
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.


RULES OF PRECEDENCE

SELECT ProductID, ProductModelID


FROM Product
WHERE ProductModelID = 20 OR
ProductModelID = 21 AND Color = 'Red'
USING PARENTHESIS

SELECT ProductID, ProductModelID


FROM Product
WHERE (ProductModelID = 20 OR ProductModelID = 21)
AND Color = 'Red'
http://www.thomasformo.com/category/qa/

You might also like